jscript: Make String_slice generic.
[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 PARENT_FULL_SEQ_INDEX  1
31 #define LISTVIEW_SEQ_INDEX     2
32 #define EDITBOX_SEQ_INDEX      3
33 #define NUM_MSG_SEQUENCES      4
34
35 #define LISTVIEW_ID 0
36 #define HEADER_ID   1
37
38 #define expect(expected, got) ok(got == expected, "Expected %d, got %d\n", expected, got)
39 #define expect2(expected1, expected2, got1, got2) ok(expected1 == got1 && expected2 == got2, \
40        "expected (%d,%d), got (%d,%d)\n", expected1, expected2, got1, got2)
41
42 #ifdef __i386__
43 #define ARCH "x86"
44 #elif defined __x86_64__
45 #define ARCH "amd64"
46 #else
47 #define ARCH "none"
48 #endif
49
50 static const CHAR manifest_name[] = "cc6.manifest";
51
52 static const CHAR manifest[] =
53     "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
54     "<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\" manifestVersion=\"1.0\">\n"
55     "  <assemblyIdentity\n"
56     "      type=\"win32\"\n"
57     "      name=\"Wine.ComCtl32.Tests\"\n"
58     "      version=\"1.0.0.0\"\n"
59     "      processorArchitecture=\"" ARCH "\"\n"
60     "  />\n"
61     "<description>Wine comctl32 test suite</description>\n"
62     "<dependency>\n"
63     "  <dependentAssembly>\n"
64     "    <assemblyIdentity\n"
65     "        type=\"win32\"\n"
66     "        name=\"microsoft.windows.common-controls\"\n"
67     "        version=\"6.0.0.0\"\n"
68     "        processorArchitecture=\"" ARCH "\"\n"
69     "        publicKeyToken=\"6595b64144ccf1df\"\n"
70     "        language=\"*\"\n"
71     "    />\n"
72     "</dependentAssembly>\n"
73     "</dependency>\n"
74     "</assembly>\n";
75
76 static const WCHAR testparentclassW[] =
77     {'L','i','s','t','v','i','e','w',' ','t','e','s','t',' ','p','a','r','e','n','t','W', 0};
78
79 HWND hwndparent, hwndparentW;
80 /* prevents edit box creation, LVN_BEGINLABELEDIT return value */
81 BOOL blockEdit;
82 /* format reported to control:
83    -1 falls to defproc, anything else returned */
84 INT  notifyFormat;
85 /* indicates we're running < 5.80 version */
86 BOOL g_is_below_5;
87
88 static HWND subclass_editbox(HWND hwndListview);
89
90 static struct msg_sequence *sequences[NUM_MSG_SEQUENCES];
91
92 static const struct message create_ownerdrawfixed_parent_seq[] = {
93     { WM_NOTIFYFORMAT, sent },
94     { WM_QUERYUISTATE, sent|optional }, /* Win2K and higher */
95     { WM_MEASUREITEM, sent },
96     { WM_PARENTNOTIFY, sent },
97     { 0 }
98 };
99
100 static const struct message redraw_listview_seq[] = {
101     { WM_PAINT,      sent|id,            0, 0, LISTVIEW_ID },
102     { WM_PAINT,      sent|id,            0, 0, HEADER_ID },
103     { WM_NCPAINT,    sent|id|defwinproc, 0, 0, HEADER_ID },
104     { WM_ERASEBKGND, sent|id|defwinproc, 0, 0, HEADER_ID },
105     { WM_NOTIFY,     sent|id|defwinproc, 0, 0, LISTVIEW_ID },
106     { WM_NCPAINT,    sent|id|defwinproc, 0, 0, LISTVIEW_ID },
107     { WM_ERASEBKGND, sent|id|defwinproc, 0, 0, LISTVIEW_ID },
108     { 0 }
109 };
110
111 static const struct message listview_icon_spacing_seq[] = {
112     { LVM_SETICONSPACING, sent|lparam, 0, MAKELPARAM(20, 30) },
113     { LVM_SETICONSPACING, sent|lparam, 0, MAKELPARAM(25, 35) },
114     { LVM_SETICONSPACING, sent|lparam, 0, MAKELPARAM(-1, -1) },
115     { 0 }
116 };
117
118 static const struct message listview_color_seq[] = {
119     { LVM_SETBKCOLOR,     sent|lparam, 0, RGB(0,0,0) },
120     { LVM_GETBKCOLOR,     sent },
121     { LVM_SETTEXTCOLOR,   sent|lparam, 0, RGB(0,0,0) },
122     { LVM_GETTEXTCOLOR,   sent },
123     { LVM_SETTEXTBKCOLOR, sent|lparam, 0, RGB(0,0,0) },
124     { LVM_GETTEXTBKCOLOR, sent },
125
126     { LVM_SETBKCOLOR,     sent|lparam, 0, RGB(100,50,200) },
127     { LVM_GETBKCOLOR,     sent },
128     { LVM_SETTEXTCOLOR,   sent|lparam, 0, RGB(100,50,200) },
129     { LVM_GETTEXTCOLOR,   sent },
130     { LVM_SETTEXTBKCOLOR, sent|lparam, 0, RGB(100,50,200) },
131     { LVM_GETTEXTBKCOLOR, sent },
132
133     { LVM_SETBKCOLOR,     sent|lparam, 0, CLR_NONE },
134     { LVM_GETBKCOLOR,     sent },
135     { LVM_SETTEXTCOLOR,   sent|lparam, 0, CLR_NONE },
136     { LVM_GETTEXTCOLOR,   sent },
137     { LVM_SETTEXTBKCOLOR, sent|lparam, 0, CLR_NONE },
138     { LVM_GETTEXTBKCOLOR, sent },
139
140     { LVM_SETBKCOLOR,     sent|lparam, 0, RGB(255,255,255) },
141     { LVM_GETBKCOLOR,     sent },
142     { LVM_SETTEXTCOLOR,   sent|lparam, 0, RGB(255,255,255) },
143     { LVM_GETTEXTCOLOR,   sent },
144     { LVM_SETTEXTBKCOLOR, sent|lparam, 0, RGB(255,255,255) },
145     { LVM_GETTEXTBKCOLOR, sent },
146     { 0 }
147 };
148
149 static const struct message listview_item_count_seq[] = {
150     { LVM_GETITEMCOUNT,   sent },
151     { LVM_INSERTITEM,     sent },
152     { LVM_INSERTITEM,     sent },
153     { LVM_INSERTITEM,     sent },
154     { LVM_GETITEMCOUNT,   sent },
155     { LVM_DELETEITEM,     sent|wparam, 2 },
156     { LVM_GETITEMCOUNT,   sent },
157     { LVM_DELETEALLITEMS, sent },
158     { LVM_GETITEMCOUNT,   sent },
159     { LVM_INSERTITEM,     sent },
160     { LVM_INSERTITEM,     sent },
161     { LVM_GETITEMCOUNT,   sent },
162     { LVM_INSERTITEM,     sent },
163     { LVM_GETITEMCOUNT,   sent },
164     { 0 }
165 };
166
167 static const struct message listview_itempos_seq[] = {
168     { LVM_INSERTITEM,      sent },
169     { LVM_INSERTITEM,      sent },
170     { LVM_INSERTITEM,      sent },
171     { LVM_SETITEMPOSITION, sent|wparam|lparam, 1, MAKELPARAM(10,5) },
172     { LVM_GETITEMPOSITION, sent|wparam,        1 },
173     { LVM_SETITEMPOSITION, sent|wparam|lparam, 2, MAKELPARAM(0,0) },
174     { LVM_GETITEMPOSITION, sent|wparam,        2 },
175     { LVM_SETITEMPOSITION, sent|wparam|lparam, 0, MAKELPARAM(20,20) },
176     { LVM_GETITEMPOSITION, sent|wparam,        0 },
177     { 0 }
178 };
179
180 static const struct message listview_ownerdata_switchto_seq[] = {
181     { WM_STYLECHANGING,    sent },
182     { WM_STYLECHANGED,     sent },
183     { 0 }
184 };
185
186 static const struct message listview_getorderarray_seq[] = {
187     { LVM_GETCOLUMNORDERARRAY, sent|id|wparam, 2, 0, LISTVIEW_ID },
188     { HDM_GETORDERARRAY,       sent|id|wparam, 2, 0, HEADER_ID },
189     { 0 }
190 };
191
192 static const struct message empty_seq[] = {
193     { 0 }
194 };
195
196 static const struct message forward_erasebkgnd_parent_seq[] = {
197     { WM_ERASEBKGND, sent },
198     { 0 }
199 };
200
201 static const struct message ownderdata_select_focus_parent_seq[] = {
202     { WM_NOTIFY, sent|id, 0, 0, LVN_ITEMCHANGED },
203     { WM_NOTIFY, sent|id, 0, 0, LVN_GETDISPINFOA },
204     { WM_NOTIFY, sent|id|optional, 0, 0, LVN_GETDISPINFOA }, /* version 4.7x */
205     { 0 }
206 };
207
208 static const struct message textcallback_set_again_parent_seq[] = {
209     { WM_NOTIFY, sent|id, 0, 0, LVN_ITEMCHANGING },
210     { WM_NOTIFY, sent|id, 0, 0, LVN_ITEMCHANGED  },
211     { 0 }
212 };
213
214 static const struct message single_getdispinfo_parent_seq[] = {
215     { WM_NOTIFY, sent|id, 0, 0, LVN_GETDISPINFOA },
216     { 0 }
217 };
218
219 static const struct message getitemposition_seq1[] = {
220     { LVM_GETITEMPOSITION, sent|id, 0, 0, LISTVIEW_ID },
221     { 0 }
222 };
223
224 static const struct message getitemposition_seq2[] = {
225     { LVM_GETITEMPOSITION, sent|id, 0, 0, LISTVIEW_ID },
226     { HDM_GETITEMRECT, sent|id, 0, 0, HEADER_ID },
227     { 0 }
228 };
229
230 static const struct message editbox_create_pos[] = {
231     /* sequence sent after LVN_BEGINLABELEDIT */
232     { WM_WINDOWPOSCHANGING, sent },
233     { WM_NCCALCSIZE, sent },
234     { WM_WINDOWPOSCHANGED, sent },
235     { WM_MOVE, sent|defwinproc },
236     { WM_SIZE, sent|defwinproc },
237     /* the rest is todo */
238     { WM_WINDOWPOSCHANGING, sent },
239     { WM_WINDOWPOSCHANGED, sent },
240     { 0 }
241 };
242
243 static const struct message scroll_parent_seq[] = {
244     { WM_NOTIFY, sent|id, 0, 0, LVN_BEGINSCROLL },
245     { WM_NOTIFY, sent|id, 0, 0, LVN_ENDSCROLL },
246     { 0 }
247 };
248
249 struct subclass_info
250 {
251     WNDPROC oldproc;
252 };
253
254 static LRESULT WINAPI parent_wnd_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
255 {
256     static LONG defwndproc_counter = 0;
257     LRESULT ret;
258     struct message msg;
259
260     msg.message = message;
261     msg.flags = sent|wparam|lparam;
262     if (defwndproc_counter) msg.flags |= defwinproc;
263     msg.wParam = wParam;
264     msg.lParam = lParam;
265     if (message == WM_NOTIFY && lParam) msg.id = ((NMHDR*)lParam)->code;
266
267     /* log system messages, except for painting */
268     if (message < WM_USER &&
269         message != WM_PAINT &&
270         message != WM_ERASEBKGND &&
271         message != WM_NCPAINT &&
272         message != WM_NCHITTEST &&
273         message != WM_GETTEXT &&
274         message != WM_GETICON &&
275         message != WM_DEVICECHANGE)
276     {
277         trace("parent: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
278
279         add_message(sequences, PARENT_SEQ_INDEX, &msg);
280     }
281     add_message(sequences, PARENT_FULL_SEQ_INDEX, &msg);
282
283     switch (message)
284     {
285       case WM_NOTIFY:
286       {
287           switch (((NMHDR*)lParam)->code)
288           {
289           case LVN_BEGINLABELEDIT:
290               /* subclass edit box */
291               if (!blockEdit)
292                   subclass_editbox(((NMHDR*)lParam)->hwndFrom);
293
294               return blockEdit;
295
296           case LVN_ENDLABELEDIT:
297               /* always accept new item text */
298               return TRUE;
299           case LVN_BEGINSCROLL:
300           case LVN_ENDSCROLL:
301               {
302               NMLVSCROLL *pScroll = (NMLVSCROLL*)lParam;
303
304               trace("LVN_%sSCROLL: (%d,%d)\n", pScroll->hdr.code == LVN_BEGINSCROLL ?
305                                                "BEGIN" : "END", pScroll->dx, pScroll->dy);
306               }
307               break;
308           }
309           break;
310       }
311       case WM_NOTIFYFORMAT:
312       {
313           /* force to return format */
314           if (lParam == NF_QUERY && notifyFormat != -1) return notifyFormat;
315           break;
316       }
317     }
318
319     defwndproc_counter++;
320     ret = DefWindowProcA(hwnd, message, wParam, lParam);
321     defwndproc_counter--;
322
323     return ret;
324 }
325
326 static BOOL register_parent_wnd_class(BOOL Unicode)
327 {
328     WNDCLASSA clsA;
329     WNDCLASSW clsW;
330
331     if (Unicode)
332     {
333         clsW.style = 0;
334         clsW.lpfnWndProc = parent_wnd_proc;
335         clsW.cbClsExtra = 0;
336         clsW.cbWndExtra = 0;
337         clsW.hInstance = GetModuleHandleW(NULL);
338         clsW.hIcon = 0;
339         clsW.hCursor = LoadCursorA(0, IDC_ARROW);
340         clsW.hbrBackground = GetStockObject(WHITE_BRUSH);
341         clsW.lpszMenuName = NULL;
342         clsW.lpszClassName = testparentclassW;
343     }
344     else
345     {
346         clsA.style = 0;
347         clsA.lpfnWndProc = parent_wnd_proc;
348         clsA.cbClsExtra = 0;
349         clsA.cbWndExtra = 0;
350         clsA.hInstance = GetModuleHandleA(NULL);
351         clsA.hIcon = 0;
352         clsA.hCursor = LoadCursorA(0, IDC_ARROW);
353         clsA.hbrBackground = GetStockObject(WHITE_BRUSH);
354         clsA.lpszMenuName = NULL;
355         clsA.lpszClassName = "Listview test parent class";
356     }
357
358     return Unicode ? RegisterClassW(&clsW) : RegisterClassA(&clsA);
359 }
360
361 static HWND create_parent_window(BOOL Unicode)
362 {
363     static const WCHAR nameW[] = {'t','e','s','t','p','a','r','e','n','t','n','a','m','e','W'};
364
365     if (!register_parent_wnd_class(Unicode))
366         return NULL;
367
368     blockEdit = FALSE;
369     notifyFormat = -1;
370
371     if (Unicode)
372         return CreateWindowExW(0, testparentclassW, nameW,
373                                WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
374                                WS_MAXIMIZEBOX | WS_VISIBLE,
375                                0, 0, 100, 100,
376                                GetDesktopWindow(), NULL, GetModuleHandleW(NULL), NULL);
377     else
378         return CreateWindowExA(0, "Listview test parent class",
379                                "Listview test parent window",
380                                WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
381                                WS_MAXIMIZEBOX | WS_VISIBLE,
382                                0, 0, 100, 100,
383                                GetDesktopWindow(), NULL, GetModuleHandleA(NULL), NULL);
384 }
385
386 static LRESULT WINAPI listview_subclass_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
387 {
388     struct subclass_info *info = (struct subclass_info *)GetWindowLongPtrA(hwnd, GWLP_USERDATA);
389     static LONG defwndproc_counter = 0;
390     LRESULT ret;
391     struct message msg;
392
393     trace("listview: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
394
395     /* some debug output for style changing */
396     if ((message == WM_STYLECHANGING ||
397          message == WM_STYLECHANGED) && lParam)
398     {
399         STYLESTRUCT *style = (STYLESTRUCT*)lParam;
400         trace("\told style: 0x%08x, new style: 0x%08x\n", style->styleOld, style->styleNew);
401     }
402
403     msg.message = message;
404     msg.flags = sent|wparam|lparam;
405     if (defwndproc_counter) msg.flags |= defwinproc;
406     msg.wParam = wParam;
407     msg.lParam = lParam;
408     msg.id = LISTVIEW_ID;
409     add_message(sequences, LISTVIEW_SEQ_INDEX, &msg);
410
411     defwndproc_counter++;
412     ret = CallWindowProcA(info->oldproc, hwnd, message, wParam, lParam);
413     defwndproc_counter--;
414     return ret;
415 }
416
417 static HWND create_listview_control(DWORD style)
418 {
419     struct subclass_info *info;
420     HWND hwnd;
421     RECT rect;
422
423     info = HeapAlloc(GetProcessHeap(), 0, sizeof(struct subclass_info));
424     if (!info)
425         return NULL;
426
427     GetClientRect(hwndparent, &rect);
428     hwnd = CreateWindowExA(0, WC_LISTVIEW, "foo",
429                            WS_CHILD | WS_BORDER | WS_VISIBLE | LVS_REPORT | style,
430                            0, 0, rect.right, rect.bottom,
431                            hwndparent, NULL, GetModuleHandleA(NULL), NULL);
432     ok(hwnd != NULL, "gle=%d\n", GetLastError());
433
434     if (!hwnd)
435     {
436         HeapFree(GetProcessHeap(), 0, info);
437         return NULL;
438     }
439
440     info->oldproc = (WNDPROC)SetWindowLongPtrA(hwnd, GWLP_WNDPROC,
441                                             (LONG_PTR)listview_subclass_proc);
442     SetWindowLongPtrA(hwnd, GWLP_USERDATA, (LONG_PTR)info);
443
444     return hwnd;
445 }
446
447 /* unicode listview window with specified parent */
448 static HWND create_listview_controlW(DWORD style, HWND parent)
449 {
450     struct subclass_info *info;
451     HWND hwnd;
452     RECT rect;
453     static const WCHAR nameW[] = {'f','o','o',0};
454
455     info = HeapAlloc(GetProcessHeap(), 0, sizeof(struct subclass_info));
456     if (!info)
457         return NULL;
458
459     GetClientRect(parent, &rect);
460     hwnd = CreateWindowExW(0, WC_LISTVIEWW, nameW,
461                            WS_CHILD | WS_BORDER | WS_VISIBLE | LVS_REPORT | style,
462                            0, 0, rect.right, rect.bottom,
463                            parent, NULL, GetModuleHandleW(NULL), NULL);
464     ok(hwnd != NULL, "gle=%d\n", GetLastError());
465
466     if (!hwnd)
467     {
468         HeapFree(GetProcessHeap(), 0, info);
469         return NULL;
470     }
471
472     info->oldproc = (WNDPROC)SetWindowLongPtrW(hwnd, GWLP_WNDPROC,
473                                             (LONG_PTR)listview_subclass_proc);
474     SetWindowLongPtrW(hwnd, GWLP_USERDATA, (LONG_PTR)info);
475
476     return hwnd;
477 }
478
479 static HWND create_custom_listview_control(DWORD style)
480 {
481     struct subclass_info *info;
482     HWND hwnd;
483     RECT rect;
484
485     info = HeapAlloc(GetProcessHeap(), 0, sizeof(struct subclass_info));
486     if (!info)
487         return NULL;
488
489     GetClientRect(hwndparent, &rect);
490     hwnd = CreateWindowExA(0, WC_LISTVIEW, "foo",
491                            WS_CHILD | WS_BORDER | WS_VISIBLE | style,
492                            0, 0, rect.right, rect.bottom,
493                            hwndparent, NULL, GetModuleHandleA(NULL), NULL);
494     ok(hwnd != NULL, "gle=%d\n", GetLastError());
495
496     if (!hwnd)
497     {
498         HeapFree(GetProcessHeap(), 0, info);
499         return NULL;
500     }
501
502     info->oldproc = (WNDPROC)SetWindowLongPtrA(hwnd, GWLP_WNDPROC,
503                                             (LONG_PTR)listview_subclass_proc);
504     SetWindowLongPtrA(hwnd, GWLP_USERDATA, (LONG_PTR)info);
505
506     return hwnd;
507 }
508
509 static LRESULT WINAPI header_subclass_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
510 {
511     struct subclass_info *info = (struct subclass_info *)GetWindowLongPtrA(hwnd, GWLP_USERDATA);
512     static LONG defwndproc_counter = 0;
513     LRESULT ret;
514     struct message msg;
515
516     trace("header: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
517
518     msg.message = message;
519     msg.flags = sent|wparam|lparam;
520     if (defwndproc_counter) msg.flags |= defwinproc;
521     msg.wParam = wParam;
522     msg.lParam = lParam;
523     msg.id = HEADER_ID;
524     add_message(sequences, LISTVIEW_SEQ_INDEX, &msg);
525
526     defwndproc_counter++;
527     ret = CallWindowProcA(info->oldproc, hwnd, message, wParam, lParam);
528     defwndproc_counter--;
529     return ret;
530 }
531
532 static HWND subclass_header(HWND hwndListview)
533 {
534     struct subclass_info *info;
535     HWND hwnd;
536
537     info = HeapAlloc(GetProcessHeap(), 0, sizeof(struct subclass_info));
538     if (!info)
539         return NULL;
540
541     hwnd = ListView_GetHeader(hwndListview);
542     info->oldproc = (WNDPROC)SetWindowLongPtrA(hwnd, GWLP_WNDPROC,
543                                             (LONG_PTR)header_subclass_proc);
544     SetWindowLongPtrA(hwnd, GWLP_USERDATA, (LONG_PTR)info);
545
546     return hwnd;
547 }
548
549 static LRESULT WINAPI editbox_subclass_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
550 {
551     struct subclass_info *info = (struct subclass_info *)GetWindowLongPtrA(hwnd, GWLP_USERDATA);
552     static LONG defwndproc_counter = 0;
553     LRESULT ret;
554     struct message msg;
555
556     msg.message = message;
557     msg.flags = sent|wparam|lparam;
558     if (defwndproc_counter) msg.flags |= defwinproc;
559     msg.wParam = wParam;
560     msg.lParam = lParam;
561
562     /* all we need is sizing */
563     if (message == WM_WINDOWPOSCHANGING ||
564         message == WM_NCCALCSIZE ||
565         message == WM_WINDOWPOSCHANGED ||
566         message == WM_MOVE ||
567         message == WM_SIZE)
568     {
569         add_message(sequences, EDITBOX_SEQ_INDEX, &msg);
570     }
571
572     defwndproc_counter++;
573     ret = CallWindowProcA(info->oldproc, hwnd, message, wParam, lParam);
574     defwndproc_counter--;
575     return ret;
576 }
577
578 static HWND subclass_editbox(HWND hwndListview)
579 {
580     struct subclass_info *info;
581     HWND hwnd;
582
583     info = HeapAlloc(GetProcessHeap(), 0, sizeof(struct subclass_info));
584     if (!info)
585         return NULL;
586
587     hwnd = (HWND)SendMessage(hwndListview, LVM_GETEDITCONTROL, 0, 0);
588     info->oldproc = (WNDPROC)SetWindowLongPtrA(hwnd, GWLP_WNDPROC,
589                                             (LONG_PTR)editbox_subclass_proc);
590     SetWindowLongPtrA(hwnd, GWLP_USERDATA, (LONG_PTR)info);
591
592     return hwnd;
593 }
594
595 /* Performs a single LVM_HITTEST test */
596 static void test_lvm_hittest(HWND hwnd, INT x, INT y, INT item, UINT flags,
597                              BOOL todo_item, BOOL todo_flags, int line)
598 {
599     LVHITTESTINFO lpht;
600     DWORD ret;
601
602     lpht.pt.x = x;
603     lpht.pt.y = y;
604     lpht.iSubItem = 10;
605
606     trace("hittesting pt=(%d,%d)\n", lpht.pt.x, lpht.pt.y);
607     ret = SendMessage(hwnd, LVM_HITTEST, 0, (LPARAM)&lpht);
608
609     if (todo_item)
610     {
611         todo_wine
612         {
613             ok_(__FILE__, line)(ret == item, "Expected %d item, got %d\n", item, ret);
614             ok_(__FILE__, line)(lpht.iItem == item, "Expected %d item, got %d\n", item, lpht.iItem);
615             ok_(__FILE__, line)(lpht.iSubItem == 10, "Expected subitem not overwrited\n");
616         }
617     }
618     else
619     {
620         ok_(__FILE__, line)(ret == item, "Expected %d item, got %d\n", item, ret);
621         ok_(__FILE__, line)(lpht.iItem == item, "Expected %d item, got %d\n", item, lpht.iItem);
622         ok_(__FILE__, line)(lpht.iSubItem == 10, "Expected subitem not overwrited\n");
623     }
624
625     if (todo_flags)
626     {
627         todo_wine
628             ok_(__FILE__, line)(lpht.flags == flags, "Expected flags %x, got %x\n", flags, lpht.flags);
629     }
630     else
631         ok_(__FILE__, line)(lpht.flags == flags, "Expected flags %x, got %x\n", flags, lpht.flags);
632 }
633
634 /* Performs a single LVM_SUBITEMHITTEST test */
635 static void test_lvm_subitemhittest(HWND hwnd, INT x, INT y, INT item, INT subitem, UINT flags,
636                                     BOOL todo_item, BOOL todo_subitem, BOOL todo_flags, int line)
637 {
638     LVHITTESTINFO lpht;
639     DWORD ret;
640
641     lpht.pt.x = x;
642     lpht.pt.y = y;
643
644     trace("subhittesting pt=(%d,%d)\n", lpht.pt.x, lpht.pt.y);
645     ret = SendMessage(hwnd, LVM_SUBITEMHITTEST, 0, (LPARAM)&lpht);
646
647     if (todo_item)
648     {
649         todo_wine
650         {
651             ok_(__FILE__, line)(ret == item, "Expected %d item, got %d\n", item, ret);
652             ok_(__FILE__, line)(lpht.iItem == item, "Expected %d item, got %d\n", item, lpht.iItem);
653         }
654     }
655     else
656     {
657         ok_(__FILE__, line)(ret == item, "Expected %d item, got %d\n", item, ret);
658         ok_(__FILE__, line)(lpht.iItem == item, "Expected %d item, got %d\n", item, lpht.iItem);
659     }
660
661     if (todo_subitem)
662     {
663         todo_wine
664             ok_(__FILE__, line)(lpht.iSubItem == subitem, "Expected subitem %d, got %d\n", subitem, lpht.iSubItem);
665     }
666     else
667         ok_(__FILE__, line)(lpht.iSubItem == subitem, "Expected subitem %d, got %d\n", subitem, lpht.iSubItem);
668
669     if (todo_flags)
670     {
671         todo_wine
672             ok_(__FILE__, line)(lpht.flags == flags, "Expected flags %x, got %x\n", flags, lpht.flags);
673     }
674     else
675         ok_(__FILE__, line)(lpht.flags == flags, "Expected flags %x, got %x\n", flags, lpht.flags);
676 }
677
678 static void test_images(void)
679 {
680     HWND hwnd;
681     DWORD r;
682     LVITEM item;
683     HIMAGELIST himl;
684     HBITMAP hbmp;
685     RECT r1, r2;
686     static CHAR hello[] = "hello";
687
688     himl = ImageList_Create(40, 40, 0, 4, 4);
689     ok(himl != NULL, "failed to create imagelist\n");
690
691     hbmp = CreateBitmap(40, 40, 1, 1, NULL);
692     ok(hbmp != NULL, "failed to create bitmap\n");
693
694     r = ImageList_Add(himl, hbmp, 0);
695     ok(r == 0, "should be zero\n");
696
697     hwnd = CreateWindowEx(0, "SysListView32", "foo", LVS_OWNERDRAWFIXED, 
698                 10, 10, 100, 200, hwndparent, NULL, NULL, NULL);
699     ok(hwnd != NULL, "failed to create listview window\n");
700
701     r = SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, 0,
702                     LVS_EX_UNDERLINEHOT | LVS_EX_FLATSB | LVS_EX_ONECLICKACTIVATE);
703
704     ok(r == 0, "should return zero\n");
705
706     r = SendMessage(hwnd, LVM_SETIMAGELIST, 0, (LPARAM)himl);
707     ok(r == 0, "should return zero\n");
708
709     r = SendMessage(hwnd, LVM_SETICONSPACING, 0, MAKELONG(100,50));
710     /* returns dimensions */
711
712     r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
713     ok(r == 0, "should be zero items\n");
714
715     item.mask = LVIF_IMAGE | LVIF_TEXT;
716     item.iItem = 0;
717     item.iSubItem = 1;
718     item.iImage = 0;
719     item.pszText = 0;
720     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
721     ok(r == -1, "should fail\n");
722
723     item.iSubItem = 0;
724     item.pszText = hello;
725     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
726     ok(r == 0, "should not fail\n");
727
728     memset(&r1, 0, sizeof r1);
729     r1.left = LVIR_ICON;
730     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM) &r1);
731
732     r = SendMessage(hwnd, LVM_DELETEALLITEMS, 0, 0);
733     ok(r == TRUE, "should not fail\n");
734
735     item.iSubItem = 0;
736     item.pszText = hello;
737     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
738     ok(r == 0, "should not fail\n");
739
740     memset(&r2, 0, sizeof r2);
741     r2.left = LVIR_ICON;
742     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM) &r2);
743
744     ok(!memcmp(&r1, &r2, sizeof r1), "rectangle should be the same\n");
745
746     DestroyWindow(hwnd);
747 }
748
749 static void test_checkboxes(void)
750 {
751     HWND hwnd;
752     LVITEMA item;
753     DWORD r;
754     static CHAR text[]  = "Text",
755                 text2[] = "Text2",
756                 text3[] = "Text3";
757
758     hwnd = CreateWindowEx(0, "SysListView32", "foo", LVS_REPORT, 
759                 10, 10, 100, 200, hwndparent, NULL, NULL, NULL);
760     ok(hwnd != NULL, "failed to create listview window\n");
761
762     /* first without LVS_EX_CHECKBOXES set and an item and check that state is preserved */
763     item.mask = LVIF_TEXT | LVIF_STATE;
764     item.stateMask = 0xffff;
765     item.state = 0xfccc;
766     item.iItem = 0;
767     item.iSubItem = 0;
768     item.pszText = text;
769     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
770     ok(r == 0, "ret %d\n", r);
771
772     item.iItem = 0;
773     item.mask = LVIF_STATE;
774     item.stateMask = 0xffff;
775     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
776     ok(item.state == 0xfccc, "state %x\n", item.state);
777
778     /* Don't set LVIF_STATE */
779     item.mask = LVIF_TEXT;
780     item.stateMask = 0xffff;
781     item.state = 0xfccc;
782     item.iItem = 1;
783     item.iSubItem = 0;
784     item.pszText = text;
785     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
786     ok(r == 1, "ret %d\n", r);
787
788     item.iItem = 1;
789     item.mask = LVIF_STATE;
790     item.stateMask = 0xffff;
791     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
792     ok(item.state == 0, "state %x\n", item.state);
793
794     r = SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_CHECKBOXES, LVS_EX_CHECKBOXES);
795     ok(r == 0, "should return zero\n");
796
797     /* Having turned on checkboxes, check that all existing items are set to 0x1000 (unchecked) */
798     item.iItem = 0;
799     item.mask = LVIF_STATE;
800     item.stateMask = 0xffff;
801     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
802     ok(item.state == 0x1ccc, "state %x\n", item.state);
803
804     /* Now add an item without specifying a state and check that its state goes to 0x1000 */
805     item.iItem = 2;
806     item.mask = LVIF_TEXT;
807     item.state = 0;
808     item.pszText = text2;
809     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
810     ok(r == 2, "ret %d\n", r);
811
812     item.iItem = 2;
813     item.mask = LVIF_STATE;
814     item.stateMask = 0xffff;
815     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
816     ok(item.state == 0x1000, "state %x\n", item.state);
817
818     /* Add a further item this time specifying a state and still its state goes to 0x1000 */
819     item.iItem = 3;
820     item.mask = LVIF_TEXT | LVIF_STATE;
821     item.stateMask = 0xffff;
822     item.state = 0x2aaa;
823     item.pszText = text3;
824     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
825     ok(r == 3, "ret %d\n", r);
826
827     item.iItem = 3;
828     item.mask = LVIF_STATE;
829     item.stateMask = 0xffff;
830     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
831     ok(item.state == 0x1aaa, "state %x\n", item.state);
832
833     /* Set an item's state to checked */
834     item.iItem = 3;
835     item.mask = LVIF_STATE;
836     item.stateMask = 0xf000;
837     item.state = 0x2000;
838     r = SendMessage(hwnd, LVM_SETITEMA, 0, (LPARAM) &item);
839
840     item.iItem = 3;
841     item.mask = LVIF_STATE;
842     item.stateMask = 0xffff;
843     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
844     ok(item.state == 0x2aaa, "state %x\n", item.state);
845
846     /* Check that only the bits we asked for are returned,
847      * and that all the others are set to zero
848      */
849     item.iItem = 3;
850     item.mask = LVIF_STATE;
851     item.stateMask = 0xf000;
852     item.state = 0xffff;
853     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
854     ok(item.state == 0x2000, "state %x\n", item.state);
855
856     /* Set the style again and check that doesn't change an item's state */
857     r = SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_CHECKBOXES, LVS_EX_CHECKBOXES);
858     ok(r == LVS_EX_CHECKBOXES, "ret %x\n", r);
859
860     item.iItem = 3;
861     item.mask = LVIF_STATE;
862     item.stateMask = 0xffff;
863     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
864     ok(item.state == 0x2aaa, "state %x\n", item.state);
865
866     /* Unsetting the checkbox extended style doesn't change an item's state */
867     r = SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_CHECKBOXES, 0);
868     ok(r == LVS_EX_CHECKBOXES, "ret %x\n", r);
869
870     item.iItem = 3;
871     item.mask = LVIF_STATE;
872     item.stateMask = 0xffff;
873     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
874     ok(item.state == 0x2aaa, "state %x\n", item.state);
875
876     /* Now setting the style again will change an item's state */
877     r = SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_CHECKBOXES, LVS_EX_CHECKBOXES);
878     ok(r == 0, "ret %x\n", r);
879
880     item.iItem = 3;
881     item.mask = LVIF_STATE;
882     item.stateMask = 0xffff;
883     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
884     ok(item.state == 0x1aaa, "state %x\n", item.state);
885
886     /* Toggle checkbox tests (bug 9934) */
887     memset (&item, 0xcc, sizeof(item));
888     item.mask = LVIF_STATE;
889     item.iItem = 3;
890     item.iSubItem = 0;
891     item.state = LVIS_FOCUSED;
892     item.stateMask = LVIS_FOCUSED;
893     r = SendMessage(hwnd, LVM_SETITEM, 0, (LPARAM) &item);
894     expect(1, r);
895
896     item.iItem = 3;
897     item.mask = LVIF_STATE;
898     item.stateMask = 0xffff;
899     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
900     ok(item.state == 0x1aab, "state %x\n", item.state);
901
902     r = SendMessage(hwnd, WM_KEYDOWN, VK_SPACE, 0);
903     expect(0, r);
904     r = SendMessage(hwnd, WM_KEYUP, VK_SPACE, 0);
905     expect(0, r);
906
907     item.iItem = 3;
908     item.mask = LVIF_STATE;
909     item.stateMask = 0xffff;
910     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
911     ok(item.state == 0x2aab, "state %x\n", item.state);
912
913     r = SendMessage(hwnd, WM_KEYDOWN, VK_SPACE, 0);
914     expect(0, r);
915     r = SendMessage(hwnd, WM_KEYUP, VK_SPACE, 0);
916     expect(0, r);
917
918     item.iItem = 3;
919     item.mask = LVIF_STATE;
920     item.stateMask = 0xffff;
921     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
922     ok(item.state == 0x1aab, "state %x\n", item.state);
923
924     DestroyWindow(hwnd);
925 }
926
927 static void insert_column(HWND hwnd, int idx)
928 {
929     LVCOLUMN column;
930     DWORD rc;
931
932     memset(&column, 0xcc, sizeof(column));
933     column.mask = LVCF_SUBITEM;
934     column.iSubItem = idx;
935
936     rc = ListView_InsertColumn(hwnd, idx, &column);
937     expect(idx, rc);
938 }
939
940 static void insert_item(HWND hwnd, int idx)
941 {
942     static CHAR text[] = "foo";
943
944     LVITEMA item;
945     DWORD rc;
946
947     memset(&item, 0xcc, sizeof (item));
948     item.mask = LVIF_TEXT;
949     item.iItem = idx;
950     item.iSubItem = 0;
951     item.pszText = text;
952
953     rc = ListView_InsertItem(hwnd, &item);
954     expect(idx, rc);
955 }
956
957 static void test_items(void)
958 {
959     const LPARAM lparamTest = 0x42;
960     HWND hwnd;
961     LVITEMA item;
962     DWORD r;
963     static CHAR text[] = "Text";
964
965     hwnd = CreateWindowEx(0, "SysListView32", "foo", LVS_REPORT,
966                 10, 10, 100, 200, hwndparent, NULL, NULL, NULL);
967     ok(hwnd != NULL, "failed to create listview window\n");
968
969     /*
970      * Test setting/getting item params
971      */
972
973     /* Set up two columns */
974     insert_column(hwnd, 0);
975     insert_column(hwnd, 1);
976
977     /* LVIS_SELECTED with zero stateMask */
978     /* set */
979     memset (&item, 0, sizeof (item));
980     item.mask = LVIF_STATE;
981     item.state = LVIS_SELECTED;
982     item.stateMask = 0;
983     item.iItem = 0;
984     item.iSubItem = 0;
985     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
986     ok(r == 0, "ret %d\n", r);
987     /* get */
988     memset (&item, 0xcc, sizeof (item));
989     item.mask = LVIF_STATE;
990     item.stateMask = LVIS_SELECTED;
991     item.state = 0;
992     item.iItem = 0;
993     item.iSubItem = 0;
994     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
995     ok(r != 0, "ret %d\n", r);
996     ok(item.state & LVIS_SELECTED, "Expected LVIS_SELECTED\n");
997     SendMessage(hwnd, LVM_DELETEITEM, 0, 0);
998
999     /* LVIS_SELECTED with zero stateMask */
1000     /* set */
1001     memset (&item, 0, sizeof (item));
1002     item.mask = LVIF_STATE;
1003     item.state = LVIS_FOCUSED;
1004     item.stateMask = 0;
1005     item.iItem = 0;
1006     item.iSubItem = 0;
1007     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
1008     ok(r == 0, "ret %d\n", r);
1009     /* get */
1010     memset (&item, 0xcc, sizeof (item));
1011     item.mask = LVIF_STATE;
1012     item.stateMask = LVIS_FOCUSED;
1013     item.state = 0;
1014     item.iItem = 0;
1015     item.iSubItem = 0;
1016     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
1017     ok(r != 0, "ret %d\n", r);
1018     ok(item.state & LVIS_FOCUSED, "Expected LVIS_FOCUSED\n");
1019     SendMessage(hwnd, LVM_DELETEITEM, 0, 0);
1020
1021     /* LVIS_CUT with LVIS_FOCUSED stateMask */
1022     /* set */
1023     memset (&item, 0, sizeof (item));
1024     item.mask = LVIF_STATE;
1025     item.state = LVIS_CUT;
1026     item.stateMask = LVIS_FOCUSED;
1027     item.iItem = 0;
1028     item.iSubItem = 0;
1029     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
1030     ok(r == 0, "ret %d\n", r);
1031     /* get */
1032     memset (&item, 0xcc, sizeof (item));
1033     item.mask = LVIF_STATE;
1034     item.stateMask = LVIS_CUT;
1035     item.state = 0;
1036     item.iItem = 0;
1037     item.iSubItem = 0;
1038     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
1039     ok(r != 0, "ret %d\n", r);
1040     ok(item.state & LVIS_CUT, "Expected LVIS_CUT\n");
1041     SendMessage(hwnd, LVM_DELETEITEM, 0, 0);
1042
1043     /* Insert an item with just a param */
1044     memset (&item, 0xcc, sizeof (item));
1045     item.mask = LVIF_PARAM;
1046     item.iItem = 0;
1047     item.iSubItem = 0;
1048     item.lParam = lparamTest;
1049     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
1050     ok(r == 0, "ret %d\n", r);
1051
1052     /* Test getting of the param */
1053     memset (&item, 0xcc, sizeof (item));
1054     item.mask = LVIF_PARAM;
1055     item.iItem = 0;
1056     item.iSubItem = 0;
1057     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
1058     ok(r != 0, "ret %d\n", r);
1059     ok(item.lParam == lparamTest, "got lParam %lx, expected %lx\n", item.lParam, lparamTest);
1060
1061     /* Set up a subitem */
1062     memset (&item, 0xcc, sizeof (item));
1063     item.mask = LVIF_TEXT;
1064     item.iItem = 0;
1065     item.iSubItem = 1;
1066     item.pszText = text;
1067     r = SendMessage(hwnd, LVM_SETITEMA, 0, (LPARAM) &item);
1068     ok(r != 0, "ret %d\n", r);
1069
1070     /* Query param from subitem: returns main item param */
1071     memset (&item, 0xcc, sizeof (item));
1072     item.mask = LVIF_PARAM;
1073     item.iItem = 0;
1074     item.iSubItem = 1;
1075     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
1076     ok(r != 0, "ret %d\n", r);
1077     ok(item.lParam == lparamTest, "got lParam %lx, expected %lx\n", item.lParam, lparamTest);
1078
1079     /* Set up param on first subitem: no effect */
1080     memset (&item, 0xcc, sizeof (item));
1081     item.mask = LVIF_PARAM;
1082     item.iItem = 0;
1083     item.iSubItem = 1;
1084     item.lParam = lparamTest+1;
1085     r = SendMessage(hwnd, LVM_SETITEMA, 0, (LPARAM) &item);
1086     ok(r == 0, "ret %d\n", r);
1087
1088     /* Query param from subitem again: should still return main item param */
1089     memset (&item, 0xcc, sizeof (item));
1090     item.mask = LVIF_PARAM;
1091     item.iItem = 0;
1092     item.iSubItem = 1;
1093     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
1094     ok(r != 0, "ret %d\n", r);
1095     ok(item.lParam == lparamTest, "got lParam %lx, expected %lx\n", item.lParam, lparamTest);
1096
1097     /**** Some tests of state highlighting ****/
1098     memset (&item, 0xcc, sizeof (item));
1099     item.mask = LVIF_STATE;
1100     item.iItem = 0;
1101     item.iSubItem = 0;
1102     item.state = LVIS_SELECTED;
1103     item.stateMask = LVIS_SELECTED | LVIS_DROPHILITED;
1104     r = SendMessage(hwnd, LVM_SETITEM, 0, (LPARAM) &item);
1105     ok(r != 0, "ret %d\n", r);
1106     item.iSubItem = 1;
1107     item.state = LVIS_DROPHILITED;
1108     r = SendMessage(hwnd, LVM_SETITEM, 0, (LPARAM) &item);
1109     ok(r != 0, "ret %d\n", r);
1110
1111     memset (&item, 0xcc, sizeof (item));
1112     item.mask = LVIF_STATE;
1113     item.iItem = 0;
1114     item.iSubItem = 0;
1115     item.stateMask = -1;
1116     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
1117     ok(r != 0, "ret %d\n", r);
1118     ok(item.state == LVIS_SELECTED, "got state %x, expected %x\n", item.state, LVIS_SELECTED);
1119     item.iSubItem = 1;
1120     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
1121     ok(r != 0, "ret %d\n", r);
1122     todo_wine ok(item.state == LVIS_DROPHILITED, "got state %x, expected %x\n", item.state, LVIS_DROPHILITED);
1123
1124     /* some notnull but meaningless masks */
1125     memset (&item, 0, sizeof(item));
1126     item.mask = LVIF_NORECOMPUTE;
1127     item.iItem = 0;
1128     item.iSubItem = 0;
1129     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
1130     ok(r != 0, "ret %d\n", r);
1131     memset (&item, 0, sizeof(item));
1132     item.mask = LVIF_DI_SETITEM;
1133     item.iItem = 0;
1134     item.iSubItem = 0;
1135     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
1136     ok(r != 0, "ret %d\n", r);
1137
1138     /* set text to callback value already having it */
1139     r = SendMessage(hwnd, LVM_DELETEALLITEMS, 0, 0);
1140     expect(TRUE, r);
1141     memset (&item, 0, sizeof (item));
1142     item.mask  = LVIF_TEXT;
1143     item.pszText = LPSTR_TEXTCALLBACK;
1144     item.iItem = 0;
1145     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
1146     ok(r == 0, "ret %d\n", r);
1147     memset (&item, 0, sizeof (item));
1148
1149     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1150
1151     item.pszText = LPSTR_TEXTCALLBACK;
1152     r = SendMessage(hwnd, LVM_SETITEMTEXT, 0 , (LPARAM) &item);
1153     expect(TRUE, r);
1154
1155     ok_sequence(sequences, PARENT_SEQ_INDEX, textcallback_set_again_parent_seq,
1156                 "check callback text comparison rule", FALSE);
1157
1158     DestroyWindow(hwnd);
1159 }
1160
1161 static void test_columns(void)
1162 {
1163     HWND hwnd, hwndheader;
1164     LVCOLUMN column;
1165     DWORD rc;
1166     INT order[2];
1167
1168     hwnd = CreateWindowEx(0, "SysListView32", "foo", LVS_REPORT,
1169                 10, 10, 100, 200, hwndparent, NULL, NULL, NULL);
1170     ok(hwnd != NULL, "failed to create listview window\n");
1171
1172     /* Add a column with no mask */
1173     memset(&column, 0xcc, sizeof(column));
1174     column.mask = 0;
1175     rc = ListView_InsertColumn(hwnd, 0, &column);
1176     ok(rc==0, "Inserting column with no mask failed with %d\n", rc);
1177
1178     /* Check its width */
1179     rc = ListView_GetColumnWidth(hwnd, 0);
1180     ok(rc==10 ||
1181        broken(rc==0), /* win9x */
1182        "Inserting column with no mask failed to set width to 10 with %d\n", rc);
1183
1184     DestroyWindow(hwnd);
1185
1186     /* LVM_GETCOLUMNORDERARRAY */
1187     hwnd = create_listview_control(0);
1188     hwndheader = subclass_header(hwnd);
1189
1190     memset(&column, 0, sizeof(column));
1191     column.mask = LVCF_WIDTH;
1192     column.cx = 100;
1193     rc = ListView_InsertColumn(hwnd, 0, &column);
1194     ok(rc == 0, "Inserting column failed with %d\n", rc);
1195
1196     column.cx = 200;
1197     rc = ListView_InsertColumn(hwnd, 1, &column);
1198     ok(rc == 1, "Inserting column failed with %d\n", rc);
1199
1200     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1201
1202     rc = SendMessage(hwnd, LVM_GETCOLUMNORDERARRAY, 2, (LPARAM)&order);
1203     ok(rc != 0, "Expected LVM_GETCOLUMNORDERARRAY to succeed\n");
1204     ok(order[0] == 0, "Expected order 0, got %d\n", order[0]);
1205     ok(order[1] == 1, "Expected order 1, got %d\n", order[1]);
1206
1207     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_getorderarray_seq, "get order array", FALSE);
1208
1209     DestroyWindow(hwnd);
1210 }
1211 /* test setting imagelist between WM_NCCREATE and WM_CREATE */
1212 static WNDPROC listviewWndProc;
1213 static HIMAGELIST test_create_imagelist;
1214
1215 static LRESULT CALLBACK create_test_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1216 {
1217     LRESULT ret;
1218
1219     if (uMsg == WM_CREATE)
1220     {
1221         LPCREATESTRUCT lpcs = (LPCREATESTRUCT)lParam;
1222         lpcs->style |= LVS_REPORT;
1223     }
1224     ret = CallWindowProc(listviewWndProc, hwnd, uMsg, wParam, lParam);
1225     if (uMsg == WM_CREATE) SendMessage(hwnd, LVM_SETIMAGELIST, 0, (LPARAM)test_create_imagelist);
1226     return ret;
1227 }
1228
1229 static void test_create(void)
1230 {
1231     HWND hList;
1232     HWND hHeader;
1233     LONG_PTR ret;
1234     LONG r;
1235     LVCOLUMNA col;
1236     RECT rect;
1237     WNDCLASSEX cls;
1238     cls.cbSize = sizeof(WNDCLASSEX);
1239     ok(GetClassInfoEx(GetModuleHandle(NULL), "SysListView32", &cls), "GetClassInfoEx failed\n");
1240     listviewWndProc = cls.lpfnWndProc;
1241     cls.lpfnWndProc = create_test_wndproc;
1242     cls.lpszClassName = "MyListView32";
1243     ok(RegisterClassEx(&cls), "RegisterClassEx failed\n");
1244
1245     test_create_imagelist = ImageList_Create(16, 16, 0, 5, 10);
1246     hList = CreateWindow("MyListView32", "Test", WS_VISIBLE, 0, 0, 100, 100, NULL, NULL, GetModuleHandle(NULL), 0);
1247     ok((HIMAGELIST)SendMessage(hList, LVM_GETIMAGELIST, 0, 0) == test_create_imagelist, "Image list not obtained\n");
1248     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1249     ok(IsWindow(hHeader) && IsWindowVisible(hHeader), "Listview not in report mode\n");
1250     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
1251     DestroyWindow(hList);
1252
1253     /* header isn't created on LVS_ICON and LVS_LIST styles */
1254     hList = CreateWindow("SysListView32", "Test", WS_VISIBLE, 0, 0, 100, 100, NULL, NULL,
1255                           GetModuleHandle(NULL), 0);
1256     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1257     ok(!IsWindow(hHeader), "Header shouldn't be created\n");
1258     ok(NULL == GetDlgItem(hList, 0), "NULL dialog item expected\n");
1259     /* insert column */
1260     memset(&col, 0, sizeof(LVCOLUMNA));
1261     col.mask = LVCF_WIDTH;
1262     col.cx = 100;
1263     r = SendMessage(hList, LVM_INSERTCOLUMN, 0, (LPARAM)&col);
1264     ok(r == 0, "Expected 0 column's inserted\n");
1265     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1266     ok(IsWindow(hHeader), "Header should be created\n");
1267     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
1268     DestroyWindow(hList);
1269
1270     hList = CreateWindow("SysListView32", "Test", WS_VISIBLE|LVS_LIST, 0, 0, 100, 100, NULL, NULL,
1271                           GetModuleHandle(NULL), 0);
1272     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1273     ok(!IsWindow(hHeader), "Header shouldn't be created\n");
1274     ok(NULL == GetDlgItem(hList, 0), "NULL dialog item expected\n");
1275     /* insert column */
1276     memset(&col, 0, sizeof(LVCOLUMNA));
1277     col.mask = LVCF_WIDTH;
1278     col.cx = 100;
1279     r = SendMessage(hList, LVM_INSERTCOLUMN, 0, (LPARAM)&col);
1280     ok(r == 0, "Expected 0 column's inserted\n");
1281     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1282     ok(IsWindow(hHeader), "Header should be created\n");
1283     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
1284     DestroyWindow(hList);
1285
1286     /* try to switch LVS_ICON -> LVS_REPORT and back LVS_ICON -> LVS_REPORT */
1287     hList = CreateWindow("SysListView32", "Test", WS_VISIBLE, 0, 0, 100, 100, NULL, NULL,
1288                           GetModuleHandle(NULL), 0);
1289     ret = SetWindowLongPtr(hList, GWL_STYLE, GetWindowLongPtr(hList, GWL_STYLE) | LVS_REPORT);
1290     ok(ret & WS_VISIBLE, "Style wrong, should have WS_VISIBLE\n");
1291     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1292     ok(IsWindow(hHeader), "Header should be created\n");
1293     ret = SetWindowLongPtr(hList, GWL_STYLE, GetWindowLong(hList, GWL_STYLE) & ~LVS_REPORT);
1294     ok((ret & WS_VISIBLE) && (ret & LVS_REPORT), "Style wrong, should have WS_VISIBLE|LVS_REPORT\n");
1295     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1296     ok(IsWindow(hHeader), "Header should be created\n");
1297     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
1298     DestroyWindow(hList);
1299
1300     /* try to switch LVS_LIST -> LVS_REPORT and back LVS_LIST -> LVS_REPORT */
1301     hList = CreateWindow("SysListView32", "Test", WS_VISIBLE|LVS_LIST, 0, 0, 100, 100, NULL, NULL,
1302                           GetModuleHandle(NULL), 0);
1303     ret = SetWindowLongPtr(hList, GWL_STYLE,
1304                           (GetWindowLongPtr(hList, GWL_STYLE) & ~LVS_LIST) | LVS_REPORT);
1305     ok(((ret & WS_VISIBLE) && (ret & LVS_LIST)), "Style wrong, should have WS_VISIBLE|LVS_LIST\n");
1306     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1307     ok(IsWindow(hHeader), "Header should be created\n");
1308     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
1309     ret = SetWindowLongPtr(hList, GWL_STYLE,
1310                           (GetWindowLongPtr(hList, GWL_STYLE) & ~LVS_REPORT) | LVS_LIST);
1311     ok(((ret & WS_VISIBLE) && (ret & LVS_REPORT)), "Style wrong, should have WS_VISIBLE|LVS_REPORT\n");
1312     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1313     ok(IsWindow(hHeader), "Header should be created\n");
1314     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
1315     DestroyWindow(hList);
1316
1317     /* LVS_REPORT without WS_VISIBLE */
1318     hList = CreateWindow("SysListView32", "Test", LVS_REPORT, 0, 0, 100, 100, NULL, NULL,
1319                           GetModuleHandle(NULL), 0);
1320     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1321     ok(!IsWindow(hHeader), "Header shouldn't be created\n");
1322     ok(NULL == GetDlgItem(hList, 0), "NULL dialog item expected\n");
1323     /* insert column */
1324     memset(&col, 0, sizeof(LVCOLUMNA));
1325     col.mask = LVCF_WIDTH;
1326     col.cx = 100;
1327     r = SendMessage(hList, LVM_INSERTCOLUMN, 0, (LPARAM)&col);
1328     ok(r == 0, "Expected 0 column's inserted\n");
1329     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1330     ok(IsWindow(hHeader), "Header should be created\n");
1331     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
1332     DestroyWindow(hList);
1333
1334     /* LVS_REPORT without WS_VISIBLE, try to show it */
1335     hList = CreateWindow("SysListView32", "Test", LVS_REPORT, 0, 0, 100, 100, NULL, NULL,
1336                           GetModuleHandle(NULL), 0);
1337     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1338     ok(!IsWindow(hHeader), "Header shouldn't be created\n");
1339     ok(NULL == GetDlgItem(hList, 0), "NULL dialog item expected\n");
1340     ShowWindow(hList, SW_SHOW);
1341     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1342     ok(IsWindow(hHeader), "Header should be created\n");
1343     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
1344     DestroyWindow(hList);
1345
1346     /* LVS_REPORT with LVS_NOCOLUMNHEADER */
1347     hList = CreateWindow("SysListView32", "Test", LVS_REPORT|LVS_NOCOLUMNHEADER|WS_VISIBLE,
1348                           0, 0, 100, 100, NULL, NULL, GetModuleHandle(NULL), 0);
1349     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1350     ok(IsWindow(hHeader), "Header should be created\n");
1351     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
1352     /* HDS_DRAGDROP set by default */
1353     ok(GetWindowLongPtr(hHeader, GWL_STYLE) & HDS_DRAGDROP, "Expected header to have HDS_DRAGDROP\n");
1354     DestroyWindow(hList);
1355
1356     /* setting LVS_EX_HEADERDRAGDROP creates header */
1357     hList = CreateWindow("SysListView32", "Test", LVS_REPORT, 0, 0, 100, 100, NULL, NULL,
1358                           GetModuleHandle(NULL), 0);
1359     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1360     ok(!IsWindow(hHeader), "Header shouldn't be created\n");
1361     ok(NULL == GetDlgItem(hList, 0), "NULL dialog item expected\n");
1362     SendMessage(hList, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_HEADERDRAGDROP);
1363     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1364     ok(IsWindow(hHeader) ||
1365        broken(!IsWindow(hHeader)), /* 4.7x common controls */
1366        "Header should be created\n");
1367     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
1368     DestroyWindow(hList);
1369
1370     /* not report style accepts LVS_EX_HEADERDRAGDROP too */
1371     hList = create_custom_listview_control(0);
1372     SendMessage(hList, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_HEADERDRAGDROP);
1373     r = SendMessage(hList, LVM_GETEXTENDEDLISTVIEWSTYLE, 0, 0);
1374     ok(r & LVS_EX_HEADERDRAGDROP, "Expected LVS_EX_HEADERDRAGDROP to be set\n");
1375     DestroyWindow(hList);
1376
1377     /* requesting header info with LVM_GETSUBITEMRECT doesn't create it */
1378     hList = CreateWindow("SysListView32", "Test", LVS_REPORT, 0, 0, 100, 100, NULL, NULL,
1379                           GetModuleHandle(NULL), 0);
1380     ok(!IsWindow(hHeader), "Header shouldn't be created\n");
1381     ok(NULL == GetDlgItem(hList, 0), "NULL dialog item expected\n");
1382
1383     rect.left = LVIR_BOUNDS;
1384     rect.top  = 1;
1385     rect.right = rect.bottom = -10;
1386     r = SendMessage(hList, LVM_GETSUBITEMRECT, -1, (LPARAM)&rect);
1387     ok(r != 0, "Expected not-null LRESULT\n");
1388
1389     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1390     ok(!IsWindow(hHeader), "Header shouldn't be created\n");
1391     ok(NULL == GetDlgItem(hList, 0), "NULL dialog item expected\n");
1392
1393     DestroyWindow(hList);
1394
1395     /* WM_MEASUREITEM should be sent when created with LVS_OWNERDRAWFIXED */
1396     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1397     hList = create_listview_control(LVS_OWNERDRAWFIXED);
1398     ok_sequence(sequences, PARENT_SEQ_INDEX, create_ownerdrawfixed_parent_seq,
1399                 "created with LVS_OWNERDRAWFIXED|LVS_REPORT - parent seq", FALSE);
1400     DestroyWindow(hList);
1401 }
1402
1403 static void test_redraw(void)
1404 {
1405     HWND hwnd, hwndheader;
1406     HDC hdc;
1407     BOOL res;
1408     DWORD r;
1409
1410     hwnd = create_listview_control(0);
1411     hwndheader = subclass_header(hwnd);
1412
1413     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1414
1415     trace("invalidate & update\n");
1416     InvalidateRect(hwnd, NULL, TRUE);
1417     UpdateWindow(hwnd);
1418     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, redraw_listview_seq, "redraw listview", FALSE);
1419
1420     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1421
1422     /* forward WM_ERASEBKGND to parent on CLR_NONE background color */
1423     /* 1. Without backbuffer */
1424     res = ListView_SetBkColor(hwnd, CLR_NONE);
1425     expect(TRUE, res);
1426
1427     hdc = GetWindowDC(hwndparent);
1428
1429     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1430     r = SendMessageA(hwnd, WM_ERASEBKGND, (WPARAM)hdc, 0);
1431     ok(r != 0, "Expected not zero result\n");
1432     ok_sequence(sequences, PARENT_FULL_SEQ_INDEX, forward_erasebkgnd_parent_seq,
1433                 "forward WM_ERASEBKGND on CLR_NONE", FALSE);
1434
1435     res = ListView_SetBkColor(hwnd, CLR_DEFAULT);
1436     expect(TRUE, res);
1437
1438     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1439     r = SendMessageA(hwnd, WM_ERASEBKGND, (WPARAM)hdc, 0);
1440     ok(r != 0, "Expected not zero result\n");
1441     ok_sequence(sequences, PARENT_FULL_SEQ_INDEX, empty_seq,
1442                 "don't forward WM_ERASEBKGND on non-CLR_NONE", FALSE);
1443
1444     /* 2. With backbuffer */
1445     SendMessageA(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_DOUBLEBUFFER,
1446                                                      LVS_EX_DOUBLEBUFFER);
1447     res = ListView_SetBkColor(hwnd, CLR_NONE);
1448     expect(TRUE, res);
1449
1450     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1451     r = SendMessageA(hwnd, WM_ERASEBKGND, (WPARAM)hdc, 0);
1452     ok(r != 0, "Expected not zero result\n");
1453     ok_sequence(sequences, PARENT_FULL_SEQ_INDEX, forward_erasebkgnd_parent_seq,
1454                 "forward WM_ERASEBKGND on CLR_NONE", FALSE);
1455
1456     res = ListView_SetBkColor(hwnd, CLR_DEFAULT);
1457     expect(TRUE, res);
1458
1459     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1460     r = SendMessageA(hwnd, WM_ERASEBKGND, (WPARAM)hdc, 0);
1461     todo_wine ok(r != 0, "Expected not zero result\n");
1462     ok_sequence(sequences, PARENT_FULL_SEQ_INDEX, empty_seq,
1463                 "don't forward WM_ERASEBKGND on non-CLR_NONE", FALSE);
1464
1465     ReleaseDC(hwndparent, hdc);
1466
1467     DestroyWindow(hwnd);
1468 }
1469
1470 static LRESULT WINAPI cd_wndproc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
1471 {
1472     COLORREF clr, c0ffee = RGB(0xc0, 0xff, 0xee);
1473
1474     if(msg == WM_NOTIFY) {
1475         NMHDR *nmhdr = (PVOID)lp;
1476         if(nmhdr->code == NM_CUSTOMDRAW) {
1477             NMLVCUSTOMDRAW *nmlvcd = (PVOID)nmhdr;
1478             trace("NMCUSTOMDRAW (0x%.8x)\n", nmlvcd->nmcd.dwDrawStage);
1479             switch(nmlvcd->nmcd.dwDrawStage) {
1480             case CDDS_PREPAINT:
1481                 SetBkColor(nmlvcd->nmcd.hdc, c0ffee);
1482                 return CDRF_NOTIFYITEMDRAW;
1483             case CDDS_ITEMPREPAINT:
1484                 nmlvcd->clrTextBk = CLR_DEFAULT;
1485                 return CDRF_NOTIFYSUBITEMDRAW;
1486             case CDDS_ITEMPREPAINT | CDDS_SUBITEM:
1487                 clr = GetBkColor(nmlvcd->nmcd.hdc);
1488                 todo_wine ok(clr == c0ffee, "clr=%.8x\n", clr);
1489                 return CDRF_NOTIFYPOSTPAINT;
1490             case CDDS_ITEMPOSTPAINT | CDDS_SUBITEM:
1491                 clr = GetBkColor(nmlvcd->nmcd.hdc);
1492                 todo_wine ok(clr == c0ffee, "clr=%.8x\n", clr);
1493                 return CDRF_DODEFAULT;
1494             }
1495             return CDRF_DODEFAULT;
1496         }
1497     }
1498
1499     return DefWindowProcA(hwnd, msg, wp, lp);
1500 }
1501
1502 static void test_customdraw(void)
1503 {
1504     HWND hwnd;
1505     WNDPROC oldwndproc;
1506
1507     hwnd = create_listview_control(0);
1508
1509     insert_column(hwnd, 0);
1510     insert_column(hwnd, 1);
1511     insert_item(hwnd, 0);
1512
1513     oldwndproc = (WNDPROC)SetWindowLongPtr(hwndparent, GWLP_WNDPROC,
1514                                            (LONG_PTR)cd_wndproc);
1515
1516     InvalidateRect(hwnd, NULL, TRUE);
1517     UpdateWindow(hwnd);
1518
1519     SetWindowLongPtr(hwndparent, GWLP_WNDPROC, (LONG_PTR)oldwndproc);
1520
1521     DestroyWindow(hwnd);
1522 }
1523
1524 static void test_icon_spacing(void)
1525 {
1526     /* LVM_SETICONSPACING */
1527     /* note: LVM_SETICONSPACING returns the previous icon spacing if successful */
1528
1529     HWND hwnd;
1530     WORD w, h;
1531     DWORD r;
1532
1533     hwnd = create_custom_listview_control(LVS_ICON);
1534     ok(hwnd != NULL, "failed to create a listview window\n");
1535
1536     r = SendMessage(hwnd, WM_NOTIFYFORMAT, (WPARAM)hwndparent, (LPARAM)NF_REQUERY);
1537     expect(NFR_ANSI, r);
1538
1539     /* reset the icon spacing to defaults */
1540     SendMessage(hwnd, LVM_SETICONSPACING, 0, MAKELPARAM(-1, -1));
1541
1542     /* now we can request what the defaults are */
1543     r = SendMessage(hwnd, LVM_SETICONSPACING, 0, MAKELPARAM(-1, -1));
1544     w = LOWORD(r);
1545     h = HIWORD(r);
1546
1547     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1548
1549     trace("test icon spacing\n");
1550
1551     r = SendMessage(hwnd, LVM_SETICONSPACING, 0, MAKELPARAM(20, 30));
1552     ok(r == MAKELONG(w, h) ||
1553        broken(r == MAKELONG(w, w)), /* win98 */
1554        "Expected %d, got %d\n", MAKELONG(w, h), r);
1555
1556     r = SendMessage(hwnd, LVM_SETICONSPACING, 0, MAKELPARAM(25, 35));
1557     expect(MAKELONG(20,30), r);
1558
1559     r = SendMessage(hwnd, LVM_SETICONSPACING, 0, MAKELPARAM(-1,-1));
1560     expect(MAKELONG(25,35), r);
1561
1562     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_icon_spacing_seq, "test icon spacing seq", FALSE);
1563
1564     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1565     DestroyWindow(hwnd);
1566 }
1567
1568 static void test_color(void)
1569 {
1570     /* SETBKCOLOR/GETBKCOLOR, SETTEXTCOLOR/GETTEXTCOLOR, SETTEXTBKCOLOR/GETTEXTBKCOLOR */
1571
1572     HWND hwnd;
1573     DWORD r;
1574     int i;
1575
1576     COLORREF color;
1577     COLORREF colors[4] = {RGB(0,0,0), RGB(100,50,200), CLR_NONE, RGB(255,255,255)};
1578
1579     hwnd = create_listview_control(0);
1580     ok(hwnd != NULL, "failed to create a listview window\n");
1581
1582     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1583
1584     trace("test color seq\n");
1585     for (i = 0; i < 4; i++)
1586     {
1587         color = colors[i];
1588
1589         r = SendMessage(hwnd, LVM_SETBKCOLOR, 0, color);
1590         expect(TRUE, r);
1591         r = SendMessage(hwnd, LVM_GETBKCOLOR, 0, color);
1592         expect(color, r);
1593
1594         r = SendMessage(hwnd, LVM_SETTEXTCOLOR, 0, color);
1595         expect (TRUE, r);
1596         r = SendMessage(hwnd, LVM_GETTEXTCOLOR, 0, color);
1597         expect(color, r);
1598
1599         r = SendMessage(hwnd, LVM_SETTEXTBKCOLOR, 0, color);
1600         expect(TRUE, r);
1601         r = SendMessage(hwnd, LVM_GETTEXTBKCOLOR, 0, color);
1602         expect(color, r);
1603     }
1604
1605     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_color_seq, "test color seq", FALSE);
1606
1607     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1608     DestroyWindow(hwnd);
1609 }
1610
1611 static void test_item_count(void)
1612 {
1613     /* LVM_INSERTITEM, LVM_DELETEITEM, LVM_DELETEALLITEMS, LVM_GETITEMCOUNT */
1614
1615     HWND hwnd;
1616     DWORD r;
1617     HDC hdc;
1618     HFONT hOldFont;
1619     TEXTMETRICA tm;
1620     RECT rect;
1621     INT height;
1622
1623     LVITEM item0;
1624     LVITEM item1;
1625     LVITEM item2;
1626     static CHAR item0text[] = "item0";
1627     static CHAR item1text[] = "item1";
1628     static CHAR item2text[] = "item2";
1629
1630     hwnd = create_listview_control(0);
1631     ok(hwnd != NULL, "failed to create a listview window\n");
1632
1633     /* resize in dpiaware manner to fit all 3 items added */
1634     hdc = GetDC(0);
1635     hOldFont = SelectObject(hdc, GetStockObject(SYSTEM_FONT));
1636     GetTextMetricsA(hdc, &tm);
1637     /* 2 extra pixels for bounds and header border */
1638     height = tm.tmHeight + 2;
1639     SelectObject(hdc, hOldFont);
1640     ReleaseDC(0, hdc);
1641
1642     GetWindowRect(hwnd, &rect);
1643     /* 3 items + 1 header + 1 to be sure */
1644     MoveWindow(hwnd, 0, 0, rect.right - rect.left, 5 * height, FALSE);
1645
1646     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1647
1648     trace("test item count\n");
1649
1650     r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
1651     expect(0, r);
1652
1653     /* [item0] */
1654     item0.mask = LVIF_TEXT;
1655     item0.iItem = 0;
1656     item0.iSubItem = 0;
1657     item0.pszText = item0text;
1658     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item0);
1659     expect(0, r);
1660
1661     /* [item0, item1] */
1662     item1.mask = LVIF_TEXT;
1663     item1.iItem = 1;
1664     item1.iSubItem = 0;
1665     item1.pszText = item1text;
1666     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item1);
1667     expect(1, r);
1668
1669     /* [item0, item1, item2] */
1670     item2.mask = LVIF_TEXT;
1671     item2.iItem = 2;
1672     item2.iSubItem = 0;
1673     item2.pszText = item2text;
1674     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item2);
1675     expect(2, r);
1676
1677     r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
1678     expect(3, r);
1679
1680     /* [item0, item1] */
1681     r = SendMessage(hwnd, LVM_DELETEITEM, 2, 0);
1682     expect(TRUE, r);
1683
1684     r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
1685     expect(2, r);
1686
1687     /* [] */
1688     r = SendMessage(hwnd, LVM_DELETEALLITEMS, 0, 0);
1689     expect(TRUE, r);
1690
1691     r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
1692     expect(0, r);
1693
1694     /* [item0] */
1695     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item1);
1696     expect(0, r);
1697
1698     /* [item0, item1] */
1699     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item1);
1700     expect(1, r);
1701
1702     r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
1703     expect(2, r);
1704
1705     /* [item0, item1, item2] */
1706     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item2);
1707     expect(2, r);
1708
1709     r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
1710     expect(3, r);
1711
1712     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_item_count_seq, "test item count seq", FALSE);
1713
1714     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1715     DestroyWindow(hwnd);
1716 }
1717
1718 static void test_item_position(void)
1719 {
1720     /* LVM_SETITEMPOSITION/LVM_GETITEMPOSITION */
1721
1722     HWND hwnd;
1723     DWORD r;
1724     POINT position;
1725
1726     LVITEM item0;
1727     LVITEM item1;
1728     LVITEM item2;
1729     static CHAR item0text[] = "item0";
1730     static CHAR item1text[] = "item1";
1731     static CHAR item2text[] = "item2";
1732
1733     hwnd = create_custom_listview_control(LVS_ICON);
1734     ok(hwnd != NULL, "failed to create a listview window\n");
1735
1736     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1737
1738     trace("test item position\n");
1739
1740     /* [item0] */
1741     item0.mask = LVIF_TEXT;
1742     item0.iItem = 0;
1743     item0.iSubItem = 0;
1744     item0.pszText = item0text;
1745     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item0);
1746     expect(0, r);
1747
1748     /* [item0, item1] */
1749     item1.mask = LVIF_TEXT;
1750     item1.iItem = 1;
1751     item1.iSubItem = 0;
1752     item1.pszText = item1text;
1753     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item1);
1754     expect(1, r);
1755
1756     /* [item0, item1, item2] */
1757     item2.mask = LVIF_TEXT;
1758     item2.iItem = 2;
1759     item2.iSubItem = 0;
1760     item2.pszText = item2text;
1761     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item2);
1762     expect(2, r);
1763
1764     r = SendMessage(hwnd, LVM_SETITEMPOSITION, 1, MAKELPARAM(10,5));
1765     expect(TRUE, r);
1766     r = SendMessage(hwnd, LVM_GETITEMPOSITION, 1, (LPARAM) &position);
1767     expect(TRUE, r);
1768     expect2(10, 5, position.x, position.y);
1769
1770     r = SendMessage(hwnd, LVM_SETITEMPOSITION, 2, MAKELPARAM(0,0));
1771     expect(TRUE, r);
1772     r = SendMessage(hwnd, LVM_GETITEMPOSITION, 2, (LPARAM) &position);
1773     expect(TRUE, r);
1774     expect2(0, 0, position.x, position.y);
1775
1776     r = SendMessage(hwnd, LVM_SETITEMPOSITION, 0, MAKELPARAM(20,20));
1777     expect(TRUE, r);
1778     r = SendMessage(hwnd, LVM_GETITEMPOSITION, 0, (LPARAM) &position);
1779     expect(TRUE, r);
1780     expect2(20, 20, position.x, position.y);
1781
1782     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_itempos_seq, "test item position seq", TRUE);
1783
1784     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1785     DestroyWindow(hwnd);
1786 }
1787
1788 static void test_getorigin(void)
1789 {
1790     /* LVM_GETORIGIN */
1791
1792     HWND hwnd;
1793     DWORD r;
1794     POINT position;
1795
1796     position.x = position.y = 0;
1797
1798     hwnd = create_custom_listview_control(LVS_ICON);
1799     ok(hwnd != NULL, "failed to create a listview window\n");
1800     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1801     trace("test get origin results\n");
1802     r = SendMessage(hwnd, LVM_GETORIGIN, 0, (LPARAM)&position);
1803     expect(TRUE, r);
1804     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1805     DestroyWindow(hwnd);
1806
1807     hwnd = create_custom_listview_control(LVS_SMALLICON);
1808     ok(hwnd != NULL, "failed to create a listview window\n");
1809     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1810     trace("test get origin results\n");
1811     r = SendMessage(hwnd, LVM_GETORIGIN, 0, (LPARAM)&position);
1812     expect(TRUE, r);
1813     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1814     DestroyWindow(hwnd);
1815
1816     hwnd = create_custom_listview_control(LVS_LIST);
1817     ok(hwnd != NULL, "failed to create a listview window\n");
1818     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1819     trace("test get origin results\n");
1820     r = SendMessage(hwnd, LVM_GETORIGIN, 0, (LPARAM)&position);
1821     expect(FALSE, r);
1822     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1823     DestroyWindow(hwnd);
1824
1825     hwnd = create_custom_listview_control(LVS_REPORT);
1826     ok(hwnd != NULL, "failed to create a listview window\n");
1827     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1828     trace("test get origin results\n");
1829     r = SendMessage(hwnd, LVM_GETORIGIN, 0, (LPARAM)&position);
1830     expect(FALSE, r);
1831     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1832     DestroyWindow(hwnd);
1833
1834 }
1835
1836 static void test_multiselect(void)
1837 {
1838     typedef struct t_select_task
1839     {
1840         const char *descr;
1841         int initPos;
1842         int loopVK;
1843         int count;
1844         int result;
1845     } select_task;
1846
1847     HWND hwnd;
1848     DWORD r;
1849     int i,j,item_count,selected_count;
1850     static const int items=5;
1851     BYTE kstate[256];
1852     select_task task;
1853     LONG_PTR style;
1854     LVITEMA item;
1855
1856     static struct t_select_task task_list[] = {
1857         { "using VK_DOWN", 0, VK_DOWN, -1, -1 },
1858         { "using VK_UP", -1, VK_UP, -1, -1 },
1859         { "using VK_END", 0, VK_END, 1, -1 },
1860         { "using VK_HOME", -1, VK_HOME, 1, -1 }
1861     };
1862
1863
1864     hwnd = create_listview_control(0);
1865
1866     for (i=0;i<items;i++) {
1867             insert_item(hwnd, 0);
1868     }
1869
1870     item_count = (int)SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
1871
1872     expect(items,item_count);
1873
1874     for (i=0;i<4;i++) {
1875         task = task_list[i];
1876
1877         /* deselect all items */
1878         ListView_SetItemState(hwnd, -1, 0, LVIS_SELECTED);
1879         SendMessage(hwnd, LVM_SETSELECTIONMARK, 0, -1);
1880
1881         /* set initial position */
1882         SendMessage(hwnd, LVM_SETSELECTIONMARK, 0, (task.initPos == -1 ? item_count -1 : task.initPos));
1883         ListView_SetItemState(hwnd,(task.initPos == -1 ? item_count -1 : task.initPos),LVIS_SELECTED ,LVIS_SELECTED);
1884
1885         selected_count = (int)SendMessage(hwnd, LVM_GETSELECTEDCOUNT, 0, 0);
1886
1887         ok(selected_count == 1, "There should be only one selected item at the beginning (is %d)\n",selected_count);
1888
1889         /* Set SHIFT key pressed */
1890         GetKeyboardState(kstate);
1891         kstate[VK_SHIFT]=0x80;
1892         SetKeyboardState(kstate);
1893
1894         for (j=1;j<=(task.count == -1 ? item_count : task.count);j++) {
1895             r = SendMessage(hwnd, WM_KEYDOWN, task.loopVK, 0);
1896             expect(0,r);
1897             r = SendMessage(hwnd, WM_KEYUP, task.loopVK, 0);
1898             expect(0,r);
1899         }
1900
1901         selected_count = (int)SendMessage(hwnd, LVM_GETSELECTEDCOUNT, 0, 0);
1902
1903         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);
1904
1905         /* Set SHIFT key released */
1906         GetKeyboardState(kstate);
1907         kstate[VK_SHIFT]=0x00;
1908         SetKeyboardState(kstate);
1909     }
1910     DestroyWindow(hwnd);
1911
1912     /* make multiple selection, then switch to LVS_SINGLESEL */
1913     hwnd = create_listview_control(0);
1914     for (i=0;i<items;i++) {
1915             insert_item(hwnd, 0);
1916     }
1917     item_count = (int)SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
1918     expect(items,item_count);
1919     /* deselect all items */
1920     ListView_SetItemState(hwnd, -1, 0, LVIS_SELECTED);
1921     SendMessage(hwnd, LVM_SETSELECTIONMARK, 0, -1);
1922     for (i=0;i<3;i++) {
1923         ListView_SetItemState(hwnd, i, LVIS_SELECTED, LVIS_SELECTED);
1924     }
1925
1926     r = SendMessage(hwnd, LVM_GETSELECTEDCOUNT, 0, 0);
1927     expect(3, r);
1928     r = SendMessage(hwnd, LVM_GETSELECTIONMARK, 0, 0);
1929 todo_wine
1930     expect(-1, r);
1931
1932     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
1933     ok(!(style & LVS_SINGLESEL), "LVS_SINGLESEL isn't expected\n");
1934     SetWindowLongPtrA(hwnd, GWL_STYLE, style | LVS_SINGLESEL);
1935     /* check that style is accepted */
1936     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
1937     ok(style & LVS_SINGLESEL, "LVS_SINGLESEL expected\n");
1938
1939     for (i=0;i<3;i++) {
1940         r = ListView_GetItemState(hwnd, i, LVIS_SELECTED);
1941         ok(r & LVIS_SELECTED, "Expected item %d to be selected\n", i);
1942     }
1943     r = SendMessage(hwnd, LVM_GETSELECTEDCOUNT, 0, 0);
1944     expect(3, r);
1945     SendMessage(hwnd, LVM_GETSELECTIONMARK, 0, 0);
1946     expect(3, r);
1947
1948     /* select one more */
1949     ListView_SetItemState(hwnd, 3, LVIS_SELECTED, LVIS_SELECTED);
1950
1951     for (i=0;i<3;i++) {
1952         r = ListView_GetItemState(hwnd, i, LVIS_SELECTED);
1953         ok(!(r & LVIS_SELECTED), "Expected item %d to be unselected\n", i);
1954     }
1955     r = ListView_GetItemState(hwnd, 3, LVIS_SELECTED);
1956     ok(r & LVIS_SELECTED, "Expected item %d to be selected\n", i);
1957
1958     r = SendMessage(hwnd, LVM_GETSELECTEDCOUNT, 0, 0);
1959     expect(1, r);
1960     r = SendMessage(hwnd, LVM_GETSELECTIONMARK, 0, 0);
1961 todo_wine
1962     expect(-1, r);
1963
1964     /* try to select all on LVS_SINGLESEL */
1965     memset(&item, 0, sizeof(item));
1966     item.stateMask = LVIS_SELECTED;
1967     r = SendMessage(hwnd, LVM_SETITEMSTATE, -1, (LPARAM)&item);
1968     expect(TRUE, r);
1969     SendMessage(hwnd, LVM_SETSELECTIONMARK, 0, -1);
1970
1971     item.stateMask = LVIS_SELECTED;
1972     item.state     = LVIS_SELECTED;
1973     r = SendMessage(hwnd, LVM_SETITEMSTATE, -1, (LPARAM)&item);
1974     expect(FALSE, r);
1975
1976     r = ListView_GetSelectedCount(hwnd);
1977     expect(0, r);
1978     r = ListView_GetSelectionMark(hwnd);
1979     expect(-1, r);
1980
1981     /* try to deselect all on LVS_SINGLESEL */
1982     item.stateMask = LVIS_SELECTED;
1983     item.state     = LVIS_SELECTED;
1984     r = SendMessage(hwnd, LVM_SETITEMSTATE, 0, (LPARAM)&item);
1985     expect(TRUE, r);
1986
1987     item.stateMask = LVIS_SELECTED;
1988     item.state     = 0;
1989     r = SendMessage(hwnd, LVM_SETITEMSTATE, -1, (LPARAM)&item);
1990     expect(TRUE, r);
1991     r = ListView_GetSelectedCount(hwnd);
1992     expect(0, r);
1993
1994     DestroyWindow(hwnd);
1995 }
1996
1997 static void test_subitem_rect(void)
1998 {
1999     HWND hwnd;
2000     DWORD r;
2001     LVCOLUMN col;
2002     RECT rect;
2003
2004     /* test LVM_GETSUBITEMRECT for header */
2005     hwnd = create_listview_control(0);
2006     ok(hwnd != NULL, "failed to create a listview window\n");
2007     /* add some columns */
2008     memset(&col, 0, sizeof(LVCOLUMN));
2009     col.mask = LVCF_WIDTH;
2010     col.cx = 100;
2011     r = -1;
2012     r = SendMessage(hwnd, LVM_INSERTCOLUMN, 0, (LPARAM)&col);
2013     expect(0, r);
2014     col.cx = 150;
2015     r = -1;
2016     r = SendMessage(hwnd, LVM_INSERTCOLUMN, 1, (LPARAM)&col);
2017     expect(1, r);
2018     col.cx = 200;
2019     r = -1;
2020     r = SendMessage(hwnd, LVM_INSERTCOLUMN, 2, (LPARAM)&col);
2021     expect(2, r);
2022     /* item = -1 means header, subitem index is 1 based */
2023     rect.left = LVIR_BOUNDS;
2024     rect.top  = 0;
2025     rect.right = rect.bottom = 0;
2026     r = SendMessage(hwnd, LVM_GETSUBITEMRECT, -1, (LPARAM)&rect);
2027     expect(0, r);
2028
2029     rect.left = LVIR_BOUNDS;
2030     rect.top  = 1;
2031     rect.right = rect.bottom = 0;
2032     r = SendMessage(hwnd, LVM_GETSUBITEMRECT, -1, (LPARAM)&rect);
2033
2034     ok(r != 0, "Expected not-null LRESULT\n");
2035     expect(100, rect.left);
2036     expect(250, rect.right);
2037 todo_wine
2038     expect(3, rect.top);
2039
2040     rect.left = LVIR_BOUNDS;
2041     rect.top  = 2;
2042     rect.right = rect.bottom = 0;
2043     r = SendMessage(hwnd, LVM_GETSUBITEMRECT, -1, (LPARAM)&rect);
2044
2045     ok(r != 0, "Expected not-null LRESULT\n");
2046     expect(250, rect.left);
2047     expect(450, rect.right);
2048 todo_wine
2049     expect(3, rect.top);
2050
2051     /* item LVS_REPORT padding isn't applied to subitems */
2052     insert_item(hwnd, 0);
2053
2054     rect.left = LVIR_BOUNDS;
2055     rect.top  = 1;
2056     rect.right = rect.bottom = 0;
2057     r = SendMessage(hwnd, LVM_GETSUBITEMRECT, 0, (LPARAM)&rect);
2058     ok(r != 0, "Expected not-null LRESULT\n");
2059     expect(100, rect.left);
2060     expect(250, rect.right);
2061
2062     rect.left = LVIR_ICON;
2063     rect.top  = 1;
2064     rect.right = rect.bottom = 0;
2065     r = SendMessage(hwnd, LVM_GETSUBITEMRECT, 0, (LPARAM)&rect);
2066     ok(r != 0, "Expected not-null LRESULT\n");
2067     /* no icon attached - zero width rectangle, with no left padding */
2068     expect(100, rect.left);
2069     expect(100, rect.right);
2070
2071     rect.left = LVIR_LABEL;
2072     rect.top  = 1;
2073     rect.right = rect.bottom = 0;
2074     r = SendMessage(hwnd, LVM_GETSUBITEMRECT, 0, (LPARAM)&rect);
2075     ok(r != 0, "Expected not-null LRESULT\n");
2076     /* same as full LVIR_BOUNDS */
2077     expect(100, rect.left);
2078     expect(250, rect.right);
2079
2080     DestroyWindow(hwnd);
2081
2082     /* try it for non LVS_REPORT style */
2083     hwnd = CreateWindow("SysListView32", "Test", LVS_ICON, 0, 0, 100, 100, NULL, NULL,
2084                          GetModuleHandle(NULL), 0);
2085     rect.left = LVIR_BOUNDS;
2086     rect.top  = 1;
2087     rect.right = rect.bottom = -10;
2088     r = SendMessage(hwnd, LVM_GETSUBITEMRECT, -1, (LPARAM)&rect);
2089     ok(r == 0, "Expected not-null LRESULT\n");
2090     /* rect is unchanged */
2091     expect(0, rect.left);
2092     expect(-10, rect.right);
2093     expect(1, rect.top);
2094     expect(-10, rect.bottom);
2095     DestroyWindow(hwnd);
2096 }
2097
2098 /* comparison callback for test_sorting */
2099 static INT WINAPI test_CallBackCompare(LPARAM first, LPARAM second, LPARAM lParam)
2100 {
2101     if (first == second) return 0;
2102     return (first > second ? 1 : -1);
2103 }
2104
2105 static void test_sorting(void)
2106 {
2107     HWND hwnd;
2108     LVITEMA item = {0};
2109     DWORD r;
2110     LONG_PTR style;
2111     static CHAR names[][5] = {"A", "B", "C", "D", "0"};
2112     CHAR buff[10];
2113
2114     hwnd = create_listview_control(0);
2115     ok(hwnd != NULL, "failed to create a listview window\n");
2116
2117     /* insert some items */
2118     item.mask = LVIF_PARAM | LVIF_STATE;
2119     item.state = LVIS_SELECTED;
2120     item.iItem = 0;
2121     item.iSubItem = 0;
2122     item.lParam = 3;
2123     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
2124     expect(0, r);
2125
2126     item.mask = LVIF_PARAM;
2127     item.iItem = 1;
2128     item.iSubItem = 0;
2129     item.lParam = 2;
2130     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
2131     expect(1, r);
2132
2133     item.mask = LVIF_STATE | LVIF_PARAM;
2134     item.state = LVIS_SELECTED;
2135     item.iItem = 2;
2136     item.iSubItem = 0;
2137     item.lParam = 4;
2138     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
2139     expect(2, r);
2140
2141     r = SendMessage(hwnd, LVM_GETSELECTIONMARK, 0, 0);
2142     expect(-1, r);
2143
2144     r = SendMessage(hwnd, LVM_GETSELECTEDCOUNT, 0, 0);
2145     expect(2, r);
2146
2147     r = SendMessage(hwnd, LVM_SORTITEMS, 0, (LPARAM)test_CallBackCompare);
2148     expect(TRUE, r);
2149
2150     r = SendMessage(hwnd, LVM_GETSELECTEDCOUNT, 0, 0);
2151     expect(2, r);
2152     r = SendMessage(hwnd, LVM_GETSELECTIONMARK, 0, 0);
2153     expect(-1, r);
2154     r = SendMessage(hwnd, LVM_GETITEMSTATE, 0, LVIS_SELECTED);
2155     expect(0, r);
2156     r = SendMessage(hwnd, LVM_GETITEMSTATE, 1, LVIS_SELECTED);
2157     expect(LVIS_SELECTED, r);
2158     r = SendMessage(hwnd, LVM_GETITEMSTATE, 2, LVIS_SELECTED);
2159     expect(LVIS_SELECTED, r);
2160
2161     DestroyWindow(hwnd);
2162
2163     /* switch to LVS_SORTASCENDING when some items added */
2164     hwnd = create_listview_control(0);
2165     ok(hwnd != NULL, "failed to create a listview window\n");
2166
2167     item.mask = LVIF_TEXT;
2168     item.iItem = 0;
2169     item.iSubItem = 0;
2170     item.pszText = names[1];
2171     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
2172     expect(0, r);
2173
2174     item.mask = LVIF_TEXT;
2175     item.iItem = 1;
2176     item.iSubItem = 0;
2177     item.pszText = names[2];
2178     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
2179     expect(1, r);
2180
2181     item.mask = LVIF_TEXT;
2182     item.iItem = 2;
2183     item.iSubItem = 0;
2184     item.pszText = names[0];
2185     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
2186     expect(2, r);
2187
2188     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2189     SetWindowLongPtrA(hwnd, GWL_STYLE, style | LVS_SORTASCENDING);
2190     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2191     ok(style & LVS_SORTASCENDING, "Expected LVS_SORTASCENDING to be set\n");
2192
2193     /* no sorting performed when switched to LVS_SORTASCENDING */
2194     item.mask = LVIF_TEXT;
2195     item.iItem = 0;
2196     item.pszText = buff;
2197     item.cchTextMax = sizeof(buff);
2198     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
2199     expect(TRUE, r);
2200     ok(lstrcmp(buff, names[1]) == 0, "Expected '%s', got '%s'\n", names[1], buff);
2201
2202     item.iItem = 1;
2203     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
2204     expect(TRUE, r);
2205     ok(lstrcmp(buff, names[2]) == 0, "Expected '%s', got '%s'\n", names[2], buff);
2206
2207     item.iItem = 2;
2208     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
2209     expect(TRUE, r);
2210     ok(lstrcmp(buff, names[0]) == 0, "Expected '%s', got '%s'\n", names[0], buff);
2211
2212     /* adding new item doesn't resort list */
2213     item.mask = LVIF_TEXT;
2214     item.iItem = 3;
2215     item.iSubItem = 0;
2216     item.pszText = names[3];
2217     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
2218     expect(3, r);
2219
2220     item.mask = LVIF_TEXT;
2221     item.iItem = 0;
2222     item.pszText = buff;
2223     item.cchTextMax = sizeof(buff);
2224     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
2225     expect(TRUE, r);
2226     ok(lstrcmp(buff, names[1]) == 0, "Expected '%s', got '%s'\n", names[1], buff);
2227
2228     item.iItem = 1;
2229     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
2230     expect(TRUE, r);
2231     ok(lstrcmp(buff, names[2]) == 0, "Expected '%s', got '%s'\n", names[2], buff);
2232
2233     item.iItem = 2;
2234     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
2235     expect(TRUE, r);
2236     ok(lstrcmp(buff, names[0]) == 0, "Expected '%s', got '%s'\n", names[0], buff);
2237
2238     item.iItem = 3;
2239     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
2240     expect(TRUE, r);
2241     ok(lstrcmp(buff, names[3]) == 0, "Expected '%s', got '%s'\n", names[3], buff);
2242
2243     /* corner case - item should be placed at first position */
2244     item.mask = LVIF_TEXT;
2245     item.iItem = 4;
2246     item.iSubItem = 0;
2247     item.pszText = names[4];
2248     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
2249     expect(0, r);
2250
2251     item.iItem = 0;
2252     item.pszText = buff;
2253     item.cchTextMax = sizeof(buff);
2254     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
2255     expect(TRUE, r);
2256     ok(lstrcmp(buff, names[4]) == 0, "Expected '%s', got '%s'\n", names[4], buff);
2257
2258     item.iItem = 1;
2259     item.pszText = buff;
2260     item.cchTextMax = sizeof(buff);
2261     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
2262     expect(TRUE, r);
2263     ok(lstrcmp(buff, names[1]) == 0, "Expected '%s', got '%s'\n", names[1], buff);
2264
2265     item.iItem = 2;
2266     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
2267     expect(TRUE, r);
2268     ok(lstrcmp(buff, names[2]) == 0, "Expected '%s', got '%s'\n", names[2], buff);
2269
2270     item.iItem = 3;
2271     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
2272     expect(TRUE, r);
2273     ok(lstrcmp(buff, names[0]) == 0, "Expected '%s', got '%s'\n", names[0], buff);
2274
2275     item.iItem = 4;
2276     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
2277     expect(TRUE, r);
2278     ok(lstrcmp(buff, names[3]) == 0, "Expected '%s', got '%s'\n", names[3], buff);
2279
2280     DestroyWindow(hwnd);
2281 }
2282
2283 static void test_ownerdata(void)
2284 {
2285     HWND hwnd;
2286     LONG_PTR style, ret;
2287     DWORD res;
2288     LVITEMA item;
2289
2290     /* it isn't possible to set LVS_OWNERDATA after creation */
2291     if (g_is_below_5)
2292     {
2293         win_skip("set LVS_OWNERDATA after creation leads to crash on < 5.80\n");
2294     }
2295     else
2296     {
2297         hwnd = create_listview_control(0);
2298         ok(hwnd != NULL, "failed to create a listview window\n");
2299         style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2300         ok(!(style & LVS_OWNERDATA) && style, "LVS_OWNERDATA isn't expected\n");
2301
2302         flush_sequences(sequences, NUM_MSG_SEQUENCES);
2303
2304         ret = SetWindowLongPtrA(hwnd, GWL_STYLE, style | LVS_OWNERDATA);
2305         ok(ret == style, "Expected set GWL_STYLE to succeed\n");
2306         ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_ownerdata_switchto_seq,
2307                 "try to switch to LVS_OWNERDATA seq", FALSE);
2308
2309         style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2310         ok(!(style & LVS_OWNERDATA), "LVS_OWNERDATA isn't expected\n");
2311         DestroyWindow(hwnd);
2312     }
2313
2314     /* try to set LVS_OWNERDATA after creation just having it */
2315     hwnd = create_listview_control(LVS_OWNERDATA);
2316     ok(hwnd != NULL, "failed to create a listview window\n");
2317     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2318     ok(style & LVS_OWNERDATA, "LVS_OWNERDATA is expected\n");
2319
2320     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2321
2322     ret = SetWindowLongPtrA(hwnd, GWL_STYLE, style | LVS_OWNERDATA);
2323     ok(ret == style, "Expected set GWL_STYLE to succeed\n");
2324     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_ownerdata_switchto_seq,
2325                 "try to switch to LVS_OWNERDATA seq", FALSE);
2326     DestroyWindow(hwnd);
2327
2328     /* try to remove LVS_OWNERDATA after creation just having it */
2329     if (g_is_below_5)
2330     {
2331         win_skip("remove LVS_OWNERDATA after creation leads to crash on < 5.80\n");
2332     }
2333     else
2334     {
2335         hwnd = create_listview_control(LVS_OWNERDATA);
2336         ok(hwnd != NULL, "failed to create a listview window\n");
2337         style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2338         ok(style & LVS_OWNERDATA, "LVS_OWNERDATA is expected\n");
2339
2340         flush_sequences(sequences, NUM_MSG_SEQUENCES);
2341
2342         ret = SetWindowLongPtrA(hwnd, GWL_STYLE, style & ~LVS_OWNERDATA);
2343         ok(ret == style, "Expected set GWL_STYLE to succeed\n");
2344         ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_ownerdata_switchto_seq,
2345                 "try to switch to LVS_OWNERDATA seq", FALSE);
2346         style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2347         ok(style & LVS_OWNERDATA, "LVS_OWNERDATA is expected\n");
2348         DestroyWindow(hwnd);
2349     }
2350
2351     /* try select an item */
2352     hwnd = create_listview_control(LVS_OWNERDATA);
2353     ok(hwnd != NULL, "failed to create a listview window\n");
2354     res = SendMessageA(hwnd, LVM_SETITEMCOUNT, 1, 0);
2355     ok(res != 0, "Expected LVM_SETITEMCOUNT to succeed\n");
2356     res = SendMessageA(hwnd, LVM_GETSELECTEDCOUNT, 0, 0);
2357     expect(0, res);
2358     memset(&item, 0, sizeof(item));
2359     item.stateMask = LVIS_SELECTED;
2360     item.state     = LVIS_SELECTED;
2361     res = SendMessageA(hwnd, LVM_SETITEMSTATE, 0, (LPARAM)&item);
2362     expect(TRUE, res);
2363     res = SendMessageA(hwnd, LVM_GETSELECTEDCOUNT, 0, 0);
2364     expect(1, res);
2365     res = SendMessageA(hwnd, LVM_GETITEMCOUNT, 0, 0);
2366     expect(1, res);
2367     DestroyWindow(hwnd);
2368
2369     /* LVM_SETITEM is unsupported on LVS_OWNERDATA */
2370     hwnd = create_listview_control(LVS_OWNERDATA);
2371     ok(hwnd != NULL, "failed to create a listview window\n");
2372     res = SendMessageA(hwnd, LVM_SETITEMCOUNT, 1, 0);
2373     ok(res != 0, "Expected LVM_SETITEMCOUNT to succeed\n");
2374     res = SendMessageA(hwnd, LVM_GETITEMCOUNT, 0, 0);
2375     expect(1, res);
2376     memset(&item, 0, sizeof(item));
2377     item.mask = LVIF_STATE;
2378     item.iItem = 0;
2379     item.stateMask = LVIS_SELECTED;
2380     item.state     = LVIS_SELECTED;
2381     res = SendMessageA(hwnd, LVM_SETITEM, 0, (LPARAM)&item);
2382     expect(FALSE, res);
2383     DestroyWindow(hwnd);
2384
2385     /* check notifications after focused/selected changed */
2386     hwnd = create_listview_control(LVS_OWNERDATA);
2387     ok(hwnd != NULL, "failed to create a listview window\n");
2388     res = SendMessageA(hwnd, LVM_SETITEMCOUNT, 1, 0);
2389     ok(res != 0, "Expected LVM_SETITEMCOUNT to succeed\n");
2390
2391     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2392
2393     memset(&item, 0, sizeof(item));
2394     item.stateMask = LVIS_SELECTED;
2395     item.state     = LVIS_SELECTED;
2396     res = SendMessageA(hwnd, LVM_SETITEMSTATE, 0, (LPARAM)&item);
2397     expect(TRUE, res);
2398
2399     ok_sequence(sequences, PARENT_SEQ_INDEX, ownderdata_select_focus_parent_seq,
2400                 "ownerdata select notification", TRUE);
2401
2402     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2403
2404     memset(&item, 0, sizeof(item));
2405     item.stateMask = LVIS_FOCUSED;
2406     item.state     = LVIS_FOCUSED;
2407     res = SendMessageA(hwnd, LVM_SETITEMSTATE, 0, (LPARAM)&item);
2408     expect(TRUE, res);
2409
2410     ok_sequence(sequences, PARENT_SEQ_INDEX, ownderdata_select_focus_parent_seq,
2411                 "ownerdata focus notification", TRUE);
2412     DestroyWindow(hwnd);
2413
2414     /* check notifications on LVM_GETITEM */
2415     /* zero callback mask */
2416     hwnd = create_listview_control(LVS_OWNERDATA);
2417     ok(hwnd != NULL, "failed to create a listview window\n");
2418     res = SendMessageA(hwnd, LVM_SETITEMCOUNT, 1, 0);
2419     ok(res != 0, "Expected LVM_SETITEMCOUNT to succeed\n");
2420
2421     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2422
2423     memset(&item, 0, sizeof(item));
2424     item.stateMask = LVIS_SELECTED;
2425     item.mask      = LVIF_STATE;
2426     res = SendMessageA(hwnd, LVM_GETITEMA, 0, (LPARAM)&item);
2427     expect(TRUE, res);
2428
2429     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq,
2430                 "ownerdata getitem selected state 1", FALSE);
2431
2432     /* non zero callback mask but not we asking for */
2433     res = SendMessageA(hwnd, LVM_SETCALLBACKMASK, LVIS_OVERLAYMASK, 0);
2434     expect(TRUE, res);
2435
2436     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2437
2438     memset(&item, 0, sizeof(item));
2439     item.stateMask = LVIS_SELECTED;
2440     item.mask      = LVIF_STATE;
2441     res = SendMessageA(hwnd, LVM_GETITEMA, 0, (LPARAM)&item);
2442     expect(TRUE, res);
2443
2444     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq,
2445                 "ownerdata getitem selected state 2", FALSE);
2446
2447     /* LVIS_OVERLAYMASK callback mask, asking for index */
2448     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2449
2450     memset(&item, 0, sizeof(item));
2451     item.stateMask = LVIS_OVERLAYMASK;
2452     item.mask      = LVIF_STATE;
2453     res = SendMessageA(hwnd, LVM_GETITEMA, 0, (LPARAM)&item);
2454     expect(TRUE, res);
2455
2456     ok_sequence(sequences, PARENT_SEQ_INDEX, single_getdispinfo_parent_seq,
2457                 "ownerdata getitem selected state 2", FALSE);
2458
2459     DestroyWindow(hwnd);
2460
2461     /* LVS_SORTASCENDING/LVS_SORTDESCENDING aren't compatible with LVS_OWNERDATA */
2462     hwnd = create_listview_control(LVS_OWNERDATA | LVS_SORTASCENDING);
2463     ok(hwnd != NULL, "failed to create a listview window\n");
2464     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2465     ok(style & LVS_OWNERDATA, "Expected LVS_OWNERDATA\n");
2466     ok(style & LVS_SORTASCENDING, "Expected LVS_SORTASCENDING to be set\n");
2467     SetWindowLongPtrA(hwnd, GWL_STYLE, style & ~LVS_SORTASCENDING);
2468     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2469     ok(!(style & LVS_SORTASCENDING), "Expected LVS_SORTASCENDING not set\n");
2470     DestroyWindow(hwnd);
2471     /* apparently it's allowed to switch these style on after creation */
2472     hwnd = create_listview_control(LVS_OWNERDATA);
2473     ok(hwnd != NULL, "failed to create a listview window\n");
2474     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2475     ok(style & LVS_OWNERDATA, "Expected LVS_OWNERDATA\n");
2476     SetWindowLongPtrA(hwnd, GWL_STYLE, style | LVS_SORTASCENDING);
2477     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2478     ok(style & LVS_SORTASCENDING, "Expected LVS_SORTASCENDING to be set\n");
2479     DestroyWindow(hwnd);
2480
2481     hwnd = create_listview_control(LVS_OWNERDATA);
2482     ok(hwnd != NULL, "failed to create a listview window\n");
2483     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2484     ok(style & LVS_OWNERDATA, "Expected LVS_OWNERDATA\n");
2485     SetWindowLongPtrA(hwnd, GWL_STYLE, style | LVS_SORTDESCENDING);
2486     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2487     ok(style & LVS_SORTDESCENDING, "Expected LVS_SORTDESCENDING to be set\n");
2488     DestroyWindow(hwnd);
2489 }
2490
2491 static void test_norecompute(void)
2492 {
2493     static CHAR testA[] = "test";
2494     CHAR buff[10];
2495     LVITEMA item;
2496     HWND hwnd;
2497     DWORD res;
2498
2499     /* self containing control */
2500     hwnd = create_listview_control(0);
2501     ok(hwnd != NULL, "failed to create a listview window\n");
2502     memset(&item, 0, sizeof(item));
2503     item.mask = LVIF_TEXT | LVIF_STATE;
2504     item.iItem = 0;
2505     item.stateMask = LVIS_SELECTED;
2506     item.state     = LVIS_SELECTED;
2507     item.pszText   = testA;
2508     res = SendMessageA(hwnd, LVM_INSERTITEM, 0, (LPARAM)&item);
2509     expect(0, res);
2510     /* retrieve with LVIF_NORECOMPUTE */
2511     item.mask  = LVIF_TEXT | LVIF_NORECOMPUTE;
2512     item.iItem = 0;
2513     item.pszText    = buff;
2514     item.cchTextMax = sizeof(buff)/sizeof(CHAR);
2515     res = SendMessageA(hwnd, LVM_GETITEM, 0, (LPARAM)&item);
2516     expect(TRUE, res);
2517     ok(lstrcmp(buff, testA) == 0, "Expected (%s), got (%s)\n", testA, buff);
2518
2519     item.mask = LVIF_TEXT;
2520     item.iItem = 1;
2521     item.pszText = LPSTR_TEXTCALLBACK;
2522     res = SendMessageA(hwnd, LVM_INSERTITEM, 0, (LPARAM)&item);
2523     expect(1, res);
2524
2525     item.mask  = LVIF_TEXT | LVIF_NORECOMPUTE;
2526     item.iItem = 1;
2527     item.pszText    = buff;
2528     item.cchTextMax = sizeof(buff)/sizeof(CHAR);
2529
2530     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2531     res = SendMessageA(hwnd, LVM_GETITEM, 0, (LPARAM)&item);
2532     expect(TRUE, res);
2533     ok(item.pszText == LPSTR_TEXTCALLBACK, "Expected (%p), got (%p)\n",
2534        LPSTR_TEXTCALLBACK, (VOID*)item.pszText);
2535     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq, "retrieve with LVIF_NORECOMPUTE seq", FALSE);
2536
2537     DestroyWindow(hwnd);
2538
2539     /* LVS_OWNERDATA */
2540     hwnd = create_listview_control(LVS_OWNERDATA);
2541     ok(hwnd != NULL, "failed to create a listview window\n");
2542
2543     item.mask = LVIF_STATE;
2544     item.stateMask = LVIS_SELECTED;
2545     item.state     = LVIS_SELECTED;
2546     item.iItem = 0;
2547     res = SendMessageA(hwnd, LVM_INSERTITEM, 0, (LPARAM)&item);
2548     expect(0, res);
2549
2550     item.mask  = LVIF_TEXT | LVIF_NORECOMPUTE;
2551     item.iItem = 0;
2552     item.pszText    = buff;
2553     item.cchTextMax = sizeof(buff)/sizeof(CHAR);
2554     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2555     res = SendMessageA(hwnd, LVM_GETITEM, 0, (LPARAM)&item);
2556     expect(TRUE, res);
2557     ok(item.pszText == LPSTR_TEXTCALLBACK, "Expected (%p), got (%p)\n",
2558        LPSTR_TEXTCALLBACK, (VOID*)item.pszText);
2559     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq, "retrieve with LVIF_NORECOMPUTE seq 2", FALSE);
2560
2561     DestroyWindow(hwnd);
2562 }
2563
2564 static void test_nosortheader(void)
2565 {
2566     HWND hwnd, header;
2567     LONG_PTR style;
2568
2569     hwnd = create_listview_control(0);
2570     ok(hwnd != NULL, "failed to create a listview window\n");
2571
2572     header = (HWND)SendMessageA(hwnd, LVM_GETHEADER, 0, 0);
2573     ok(IsWindow(header), "header expected\n");
2574
2575     style = GetWindowLongPtr(header, GWL_STYLE);
2576     ok(style & HDS_BUTTONS, "expected header to have HDS_BUTTONS\n");
2577
2578     style = GetWindowLongPtr(hwnd, GWL_STYLE);
2579     SetWindowLongPtr(hwnd, GWL_STYLE, style | LVS_NOSORTHEADER);
2580     /* HDS_BUTTONS retained */
2581     style = GetWindowLongPtr(header, GWL_STYLE);
2582     ok(style & HDS_BUTTONS, "expected header to retain HDS_BUTTONS\n");
2583
2584     DestroyWindow(hwnd);
2585
2586     /* create with LVS_NOSORTHEADER */
2587     hwnd = create_listview_control(LVS_NOSORTHEADER);
2588     ok(hwnd != NULL, "failed to create a listview window\n");
2589
2590     header = (HWND)SendMessageA(hwnd, LVM_GETHEADER, 0, 0);
2591     ok(IsWindow(header), "header expected\n");
2592
2593     style = GetWindowLongPtr(header, GWL_STYLE);
2594     ok(!(style & HDS_BUTTONS), "expected header to have no HDS_BUTTONS\n");
2595
2596     style = GetWindowLongPtr(hwnd, GWL_STYLE);
2597     SetWindowLongPtr(hwnd, GWL_STYLE, style & ~LVS_NOSORTHEADER);
2598     /* not changed here */
2599     style = GetWindowLongPtr(header, GWL_STYLE);
2600     ok(!(style & HDS_BUTTONS), "expected header to have no HDS_BUTTONS\n");
2601
2602     DestroyWindow(hwnd);
2603 }
2604
2605 static void test_setredraw(void)
2606 {
2607     HWND hwnd;
2608     DWORD_PTR style;
2609     DWORD ret;
2610
2611     hwnd = create_listview_control(0);
2612     ok(hwnd != NULL, "failed to create a listview window\n");
2613
2614     /* Passing WM_SETREDRAW to DefWinProc removes WS_VISIBLE.
2615        ListView seems to handle it internally without DefWinProc */
2616
2617     /* default value first */
2618     ret = SendMessage(hwnd, WM_SETREDRAW, TRUE, 0);
2619     expect(0, ret);
2620     /* disable */
2621     style = GetWindowLongPtr(hwnd, GWL_STYLE);
2622     ok(style & WS_VISIBLE, "Expected WS_VISIBLE to be set\n");
2623     ret = SendMessage(hwnd, WM_SETREDRAW, FALSE, 0);
2624     expect(0, ret);
2625     style = GetWindowLongPtr(hwnd, GWL_STYLE);
2626     ok(style & WS_VISIBLE, "Expected WS_VISIBLE to be set\n");
2627
2628     DestroyWindow(hwnd);
2629 }
2630
2631 static void test_hittest(void)
2632 {
2633     HWND hwnd;
2634     DWORD r;
2635     RECT bounds;
2636     LVITEMA item;
2637     static CHAR text[] = "1234567890ABCDEFGHIJKLMNOPQRST";
2638     POINT pos;
2639     INT x, y;
2640     HIMAGELIST himl, himl2;
2641     HBITMAP hbmp;
2642
2643     hwnd = create_listview_control(0);
2644     ok(hwnd != NULL, "failed to create a listview window\n");
2645
2646     /* LVS_REPORT with a single subitem (2 columns) */
2647     insert_column(hwnd, 0);
2648     insert_column(hwnd, 1);
2649     insert_item(hwnd, 0);
2650
2651     item.iSubItem = 0;
2652     /* the only purpose of that line is to be as long as a half item rect */
2653     item.pszText  = text;
2654     r = SendMessage(hwnd, LVM_SETITEMTEXT, 0, (LPARAM)&item);
2655     expect(TRUE, r);
2656
2657     r = SendMessage(hwnd, LVM_SETCOLUMNWIDTH, 0, MAKELPARAM(100, 0));
2658     expect(TRUE, r);
2659     r = SendMessage(hwnd, LVM_SETCOLUMNWIDTH, 1, MAKELPARAM(100, 0));
2660     expect(TRUE, r);
2661
2662     memset(&bounds, 0, sizeof(bounds));
2663     bounds.left = LVIR_BOUNDS;
2664     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&bounds);
2665     ok(bounds.bottom - bounds.top > 0, "Expected non zero item height\n");
2666     ok(bounds.right - bounds.left > 0, "Expected non zero item width\n");
2667     r = SendMessage(hwnd, LVM_GETITEMPOSITION, 0, (LPARAM)&pos);
2668     expect(TRUE, r);
2669
2670     /* LVS_EX_FULLROWSELECT not set, no icons attached */
2671     x = pos.x + 50; /* column half width */
2672     y = pos.y + (bounds.bottom - bounds.top) / 2;
2673     test_lvm_hittest(hwnd, x, y, 0, LVHT_ONITEMLABEL, FALSE, FALSE, __LINE__);
2674     test_lvm_subitemhittest(hwnd, x, y, 0, 0, LVHT_ONITEMLABEL, FALSE, FALSE, FALSE, __LINE__);
2675     x = pos.x + 150; /* outside column */
2676     y = pos.y + (bounds.bottom - bounds.top) / 2;
2677     test_lvm_hittest(hwnd, x, y, -1, LVHT_TORIGHT, FALSE, FALSE, __LINE__);
2678     test_lvm_subitemhittest(hwnd, x, y, 0, 1, LVHT_ONITEMLABEL, FALSE, FALSE, FALSE, __LINE__);
2679     y = (bounds.bottom - bounds.top) / 2;
2680     test_lvm_hittest(hwnd, x, y, -1, LVHT_TORIGHT, FALSE, TRUE, __LINE__);
2681     test_lvm_subitemhittest(hwnd, x, y, 0, 1, LVHT_ONITEMLABEL, FALSE, FALSE, FALSE, __LINE__);
2682     /* outside possible client rectangle (to right) */
2683     x = pos.x + 500;
2684     y = pos.y + (bounds.bottom - bounds.top) / 2;
2685     test_lvm_hittest(hwnd, x, y, -1, LVHT_TORIGHT, FALSE, FALSE, __LINE__);
2686     test_lvm_subitemhittest(hwnd, x, y, -1, -1, LVHT_NOWHERE, FALSE, FALSE, FALSE, __LINE__);
2687     y = (bounds.bottom - bounds.top) / 2;
2688     test_lvm_hittest(hwnd, x, y, -1, LVHT_TORIGHT, FALSE, TRUE, __LINE__);
2689     test_lvm_subitemhittest(hwnd, x, y, -1, -1, LVHT_NOWHERE, FALSE, FALSE, FALSE, __LINE__);
2690     /* subitem returned with -1 item too */
2691     x = pos.x + 150;
2692     y = -10;
2693     test_lvm_subitemhittest(hwnd, x, y, -1, 1, LVHT_NOWHERE, FALSE, FALSE, FALSE, __LINE__);
2694     /* parent client area is 100x100 by default */
2695     MoveWindow(hwnd, 0, 0, 300, 100, FALSE);
2696     x = pos.x + 150; /* outside column */
2697     y = pos.y + (bounds.bottom - bounds.top) / 2;
2698     test_lvm_hittest(hwnd, x, y, -1, LVHT_NOWHERE, FALSE, FALSE, __LINE__);
2699     test_lvm_subitemhittest(hwnd, x, y, 0, 1, LVHT_ONITEMLABEL, FALSE, FALSE, FALSE, __LINE__);
2700     y = (bounds.bottom - bounds.top) / 2;
2701     test_lvm_hittest(hwnd, x, y, -1, LVHT_NOWHERE, FALSE, TRUE, __LINE__);
2702     test_lvm_subitemhittest(hwnd, x, y, 0, 1, LVHT_ONITEMLABEL, FALSE, FALSE, FALSE, __LINE__);
2703     /* the same with LVS_EX_FULLROWSELECT */
2704     SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);
2705     x = pos.x + 150; /* outside column */
2706     y = pos.y + (bounds.bottom - bounds.top) / 2;
2707     test_lvm_hittest(hwnd, x, y, 0, LVHT_ONITEM, FALSE, FALSE, __LINE__);
2708     test_lvm_subitemhittest(hwnd, x, y, 0, 1, LVHT_ONITEMLABEL, FALSE, FALSE, FALSE, __LINE__);
2709     y = (bounds.bottom - bounds.top) / 2;
2710     test_lvm_subitemhittest(hwnd, x, y, 0, 1, LVHT_ONITEMLABEL, FALSE, FALSE, FALSE, __LINE__);
2711     MoveWindow(hwnd, 0, 0, 100, 100, FALSE);
2712     x = pos.x + 150; /* outside column */
2713     y = pos.y + (bounds.bottom - bounds.top) / 2;
2714     test_lvm_hittest(hwnd, x, y, -1, LVHT_TORIGHT, FALSE, FALSE, __LINE__);
2715     test_lvm_subitemhittest(hwnd, x, y, 0, 1, LVHT_ONITEMLABEL, FALSE, FALSE, FALSE, __LINE__);
2716     y = (bounds.bottom - bounds.top) / 2;
2717     test_lvm_hittest(hwnd, x, y, -1, LVHT_TORIGHT, FALSE, TRUE, __LINE__);
2718     test_lvm_subitemhittest(hwnd, x, y, 0, 1, LVHT_ONITEMLABEL, FALSE, FALSE, FALSE, __LINE__);
2719     /* outside possible client rectangle (to right) */
2720     x = pos.x + 500;
2721     y = pos.y + (bounds.bottom - bounds.top) / 2;
2722     test_lvm_hittest(hwnd, x, y, -1, LVHT_TORIGHT, FALSE, FALSE, __LINE__);
2723     test_lvm_subitemhittest(hwnd, x, y, -1, -1, LVHT_NOWHERE, FALSE, FALSE, FALSE, __LINE__);
2724     y = (bounds.bottom - bounds.top) / 2;
2725     test_lvm_hittest(hwnd, x, y, -1, LVHT_TORIGHT, FALSE, TRUE, __LINE__);
2726     test_lvm_subitemhittest(hwnd, x, y, -1, -1, LVHT_NOWHERE, FALSE, FALSE, FALSE, __LINE__);
2727     /* try with icons, state icons index is 1 based so at least 2 bitmaps needed */
2728     himl = ImageList_Create(16, 16, 0, 4, 4);
2729     ok(himl != NULL, "failed to create imagelist\n");
2730     hbmp = CreateBitmap(16, 16, 1, 1, NULL);
2731     ok(hbmp != NULL, "failed to create bitmap\n");
2732     r = ImageList_Add(himl, hbmp, 0);
2733     ok(r == 0, "should be zero\n");
2734     hbmp = CreateBitmap(16, 16, 1, 1, NULL);
2735     ok(hbmp != NULL, "failed to create bitmap\n");
2736     r = ImageList_Add(himl, hbmp, 0);
2737     ok(r == 1, "should be one\n");
2738
2739     r = SendMessage(hwnd, LVM_SETIMAGELIST, LVSIL_STATE, (LPARAM)himl);
2740     ok(r == 0, "should return zero\n");
2741
2742     item.mask = LVIF_IMAGE;
2743     item.iImage = 0;
2744     item.iItem = 0;
2745     item.iSubItem = 0;
2746     r = SendMessage(hwnd, LVM_SETITEM, 0, (LPARAM)&item);
2747     expect(TRUE, r);
2748     /* on state icon */
2749     x = pos.x + 8;
2750     y = pos.y + (bounds.bottom - bounds.top) / 2;
2751     test_lvm_hittest(hwnd, x, y, 0, LVHT_ONITEMSTATEICON, FALSE, FALSE, __LINE__);
2752     test_lvm_subitemhittest(hwnd, x, y, 0, 0, LVHT_ONITEMSTATEICON, FALSE, FALSE, FALSE, __LINE__);
2753     y = (bounds.bottom - bounds.top) / 2;
2754     test_lvm_subitemhittest(hwnd, x, y, 0, 0, LVHT_ONITEMSTATEICON, FALSE, FALSE, FALSE, __LINE__);
2755
2756     /* state icons indices are 1 based, check with valid index */
2757     item.mask = LVIF_STATE;
2758     item.state = INDEXTOSTATEIMAGEMASK(1);
2759     item.stateMask = LVIS_STATEIMAGEMASK;
2760     item.iItem = 0;
2761     item.iSubItem = 0;
2762     r = SendMessage(hwnd, LVM_SETITEM, 0, (LPARAM)&item);
2763     expect(TRUE, r);
2764     /* on state icon */
2765     x = pos.x + 8;
2766     y = pos.y + (bounds.bottom - bounds.top) / 2;
2767     test_lvm_hittest(hwnd, x, y, 0, LVHT_ONITEMSTATEICON, FALSE, FALSE, __LINE__);
2768     test_lvm_subitemhittest(hwnd, x, y, 0, 0, LVHT_ONITEMSTATEICON, FALSE, FALSE, FALSE, __LINE__);
2769     y = (bounds.bottom - bounds.top) / 2;
2770     test_lvm_subitemhittest(hwnd, x, y, 0, 0, LVHT_ONITEMSTATEICON, FALSE, FALSE, FALSE, __LINE__);
2771
2772     himl2 = (HIMAGELIST)SendMessage(hwnd, LVM_SETIMAGELIST, LVSIL_STATE, (LPARAM)NULL);
2773     ok(himl2 == himl, "should return handle\n");
2774
2775     r = SendMessage(hwnd, LVM_SETIMAGELIST, LVSIL_SMALL, (LPARAM)himl);
2776     ok(r == 0, "should return zero\n");
2777     /* on item icon */
2778     x = pos.x + 8;
2779     y = pos.y + (bounds.bottom - bounds.top) / 2;
2780     test_lvm_hittest(hwnd, x, y, 0, LVHT_ONITEMICON, FALSE, FALSE, __LINE__);
2781     test_lvm_subitemhittest(hwnd, x, y, 0, 0, LVHT_ONITEMICON, FALSE, FALSE, FALSE, __LINE__);
2782     y = (bounds.bottom - bounds.top) / 2;
2783     test_lvm_subitemhittest(hwnd, x, y, 0, 0, LVHT_ONITEMICON, FALSE, FALSE, FALSE, __LINE__);
2784
2785     DestroyWindow(hwnd);
2786 }
2787
2788 static void test_getviewrect(void)
2789 {
2790     HWND hwnd;
2791     DWORD r;
2792     RECT rect;
2793     LVITEMA item;
2794
2795     hwnd = create_listview_control(0);
2796     ok(hwnd != NULL, "failed to create a listview window\n");
2797
2798     /* empty */
2799     r = SendMessage(hwnd, LVM_GETVIEWRECT, 0, (LPARAM)&rect);
2800     expect(TRUE, r);
2801
2802     insert_column(hwnd, 0);
2803     insert_column(hwnd, 1);
2804
2805     memset(&item, 0, sizeof(item));
2806     item.iItem = 0;
2807     item.iSubItem = 0;
2808     SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM)&item);
2809
2810     r = SendMessage(hwnd, LVM_SETCOLUMNWIDTH, 0, MAKELPARAM(100, 0));
2811     expect(TRUE, r);
2812     r = SendMessage(hwnd, LVM_SETCOLUMNWIDTH, 1, MAKELPARAM(120, 0));
2813     expect(TRUE, r);
2814
2815     rect.left = rect.right = rect.top = rect.bottom = -1;
2816     r = SendMessage(hwnd, LVM_GETVIEWRECT, 0, (LPARAM)&rect);
2817     expect(TRUE, r);
2818     /* left is set to (2e31-1) - XP SP2 */
2819     expect(0, rect.right);
2820     expect(0, rect.top);
2821     expect(0, rect.bottom);
2822
2823     /* switch to LVS_ICON */
2824     SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~LVS_REPORT);
2825
2826     rect.left = rect.right = rect.top = rect.bottom = -1;
2827     r = SendMessage(hwnd, LVM_GETVIEWRECT, 0, (LPARAM)&rect);
2828     expect(TRUE, r);
2829     expect(0, rect.left);
2830     expect(0, rect.top);
2831     /* precise value differs for 2k, XP and Vista */
2832     ok(rect.bottom > 0, "Expected positive bottom value, got %d\n", rect.bottom);
2833     ok(rect.right  > 0, "Expected positive right value, got %d\n", rect.right);
2834
2835     DestroyWindow(hwnd);
2836 }
2837
2838 static void test_getitemposition(void)
2839 {
2840     HWND hwnd, header;
2841     DWORD r;
2842     POINT pt;
2843     RECT rect;
2844
2845     hwnd = create_listview_control(0);
2846     ok(hwnd != NULL, "failed to create a listview window\n");
2847     header = subclass_header(hwnd);
2848
2849     /* LVS_REPORT, single item, no columns added */
2850     insert_item(hwnd, 0);
2851
2852     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2853
2854     pt.x = pt.y = -1;
2855     r = SendMessage(hwnd, LVM_GETITEMPOSITION, 0, (LPARAM)&pt);
2856     expect(TRUE, r);
2857     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, getitemposition_seq1, "get item position 1", FALSE);
2858
2859     /* LVS_REPORT, single item, single column */
2860     insert_column(hwnd, 0);
2861
2862     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2863
2864     pt.x = pt.y = -1;
2865     r = SendMessage(hwnd, LVM_GETITEMPOSITION, 0, (LPARAM)&pt);
2866     expect(TRUE, r);
2867     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, getitemposition_seq2, "get item position 2", TRUE);
2868
2869     memset(&rect, 0, sizeof(rect));
2870     SendMessage(header, HDM_GETITEMRECT, 0, (LPARAM)&rect);
2871     /* some padding? */
2872     expect(2, pt.x);
2873     /* offset by header height */
2874     expect(rect.bottom - rect.top, pt.y);
2875
2876     DestroyWindow(hwnd);
2877 }
2878
2879 static void test_columnscreation(void)
2880 {
2881     HWND hwnd, header;
2882     DWORD r;
2883
2884     hwnd = create_listview_control(0);
2885     ok(hwnd != NULL, "failed to create a listview window\n");
2886
2887     insert_item(hwnd, 0);
2888
2889     /* headers columns aren't created automatically */
2890     header = (HWND)SendMessage(hwnd, LVM_GETHEADER, 0, 0);
2891     ok(IsWindow(header), "Expected header handle\n");
2892     r = SendMessage(header, HDM_GETITEMCOUNT, 0, 0);
2893     expect(0, r);
2894
2895     DestroyWindow(hwnd);
2896 }
2897
2898 static void test_getitemrect(void)
2899 {
2900     HWND hwnd;
2901     HIMAGELIST himl;
2902     HBITMAP hbm;
2903     RECT rect;
2904     DWORD r;
2905     LVITEMA item;
2906     LVCOLUMNA col;
2907     INT order[2];
2908     POINT pt;
2909
2910     hwnd = create_listview_control(0);
2911     ok(hwnd != NULL, "failed to create a listview window\n");
2912
2913     /* empty item */
2914     memset(&item, 0, sizeof(item));
2915     item.iItem = 0;
2916     item.iSubItem = 0;
2917     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM)&item);
2918     expect(0, r);
2919
2920     rect.left = LVIR_BOUNDS;
2921     rect.right = rect.top = rect.bottom = -1;
2922     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
2923     expect(TRUE, r);
2924
2925     /* zero width rectangle with no padding */
2926     expect(0, rect.left);
2927     expect(0, rect.right);
2928
2929     insert_column(hwnd, 0);
2930     insert_column(hwnd, 1);
2931
2932     col.mask = LVCF_WIDTH;
2933     col.cx   = 50;
2934     r = SendMessage(hwnd, LVM_SETCOLUMN, 0, (LPARAM)&col);
2935     expect(TRUE, r);
2936
2937     col.mask = LVCF_WIDTH;
2938     col.cx   = 100;
2939     r = SendMessage(hwnd, LVM_SETCOLUMN, 1, (LPARAM)&col);
2940     expect(TRUE, r);
2941
2942     rect.left = LVIR_BOUNDS;
2943     rect.right = rect.top = rect.bottom = -1;
2944     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
2945     expect(TRUE, r);
2946
2947     /* still no left padding */
2948     expect(0, rect.left);
2949     expect(150, rect.right);
2950
2951     rect.left = LVIR_SELECTBOUNDS;
2952     rect.right = rect.top = rect.bottom = -1;
2953     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
2954     expect(TRUE, r);
2955     /* padding */
2956     expect(2, rect.left);
2957
2958     rect.left = LVIR_LABEL;
2959     rect.right = rect.top = rect.bottom = -1;
2960     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
2961     expect(TRUE, r);
2962     /* padding, column width */
2963     expect(2, rect.left);
2964     expect(50, rect.right);
2965
2966     /* no icons attached */
2967     rect.left = LVIR_ICON;
2968     rect.right = rect.top = rect.bottom = -1;
2969     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
2970     expect(TRUE, r);
2971     /* padding */
2972     expect(2, rect.left);
2973     expect(2, rect.right);
2974
2975     /* change order */
2976     order[0] = 1; order[1] = 0;
2977     r = SendMessage(hwnd, LVM_SETCOLUMNORDERARRAY, 2, (LPARAM)&order);
2978     expect(TRUE, r);
2979     pt.x = -1;
2980     r = SendMessage(hwnd, LVM_GETITEMPOSITION, 0, (LPARAM)&pt);
2981     expect(TRUE, r);
2982     /* 1 indexed column width + padding */
2983     todo_wine expect(102, pt.x);
2984     /* rect is at zero too */
2985     rect.left = LVIR_BOUNDS;
2986     rect.right = rect.top = rect.bottom = -1;
2987     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
2988     expect(TRUE, r);
2989     expect(0, rect.left);
2990     /* just width sum */
2991     expect(150, rect.right);
2992
2993     rect.left = LVIR_SELECTBOUNDS;
2994     rect.right = rect.top = rect.bottom = -1;
2995     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
2996     expect(TRUE, r);
2997     /* column width + padding */
2998     todo_wine expect(102, rect.left);
2999
3000     /* back to initial order */
3001     order[0] = 0; order[1] = 1;
3002     r = SendMessage(hwnd, LVM_SETCOLUMNORDERARRAY, 2, (LPARAM)&order);
3003     expect(TRUE, r);
3004
3005     /* state icons */
3006     himl = ImageList_Create(16, 16, 0, 2, 2);
3007     ok(himl != NULL, "failed to create imagelist\n");
3008     hbm = CreateBitmap(16, 16, 1, 1, NULL);
3009     ok(hbm != NULL, "failed to create bitmap\n");
3010     r = ImageList_Add(himl, hbm, 0);
3011     ok(r == 0, "should be zero\n");
3012     hbm = CreateBitmap(16, 16, 1, 1, NULL);
3013     ok(hbm != NULL, "failed to create bitmap\n");
3014     r = ImageList_Add(himl, hbm, 0);
3015     ok(r == 1, "should be one\n");
3016
3017     r = SendMessage(hwnd, LVM_SETIMAGELIST, LVSIL_STATE, (LPARAM)himl);
3018     ok(r == 0, "should return zero\n");
3019
3020     item.mask = LVIF_STATE;
3021     item.state = INDEXTOSTATEIMAGEMASK(1);
3022     item.stateMask = LVIS_STATEIMAGEMASK;
3023     item.iItem = 0;
3024     item.iSubItem = 0;
3025     r = SendMessage(hwnd, LVM_SETITEM, 0, (LPARAM)&item);
3026     expect(TRUE, r);
3027
3028     /* icon bounds */
3029     rect.left = LVIR_ICON;
3030     rect.right = rect.top = rect.bottom = -1;
3031     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
3032     expect(TRUE, r);
3033     /* padding + stateicon width */
3034     expect(18, rect.left);
3035     expect(18, rect.right);
3036     /* label bounds */
3037     rect.left = LVIR_LABEL;
3038     rect.right = rect.top = rect.bottom = -1;
3039     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
3040     expect(TRUE, r);
3041     /* padding + stateicon width -> column width */
3042     expect(18, rect.left);
3043     expect(50, rect.right);
3044
3045     r = SendMessage(hwnd, LVM_SETIMAGELIST, LVSIL_STATE, (LPARAM)NULL);
3046     ok(r != 0, "should return current list handle\n");
3047
3048     r = SendMessage(hwnd, LVM_SETIMAGELIST, LVSIL_SMALL, (LPARAM)himl);
3049     ok(r == 0, "should return zero\n");
3050
3051     item.mask = LVIF_STATE | LVIF_IMAGE;
3052     item.iImage = 1;
3053     item.state = 0;
3054     item.stateMask = ~0;
3055     item.iItem = 0;
3056     item.iSubItem = 0;
3057     r = SendMessage(hwnd, LVM_SETITEM, 0, (LPARAM)&item);
3058     expect(TRUE, r);
3059
3060     /* icon bounds */
3061     rect.left = LVIR_ICON;
3062     rect.right = rect.top = rect.bottom = -1;
3063     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
3064     expect(TRUE, r);
3065     /* padding, icon width */
3066     expect(2, rect.left);
3067     expect(18, rect.right);
3068     /* label bounds */
3069     rect.left = LVIR_LABEL;
3070     rect.right = rect.top = rect.bottom = -1;
3071     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
3072     expect(TRUE, r);
3073     /* padding + icon width -> column width */
3074     expect(18, rect.left);
3075     expect(50, rect.right);
3076
3077     /* select bounds */
3078     rect.left = LVIR_SELECTBOUNDS;
3079     rect.right = rect.top = rect.bottom = -1;
3080     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
3081     expect(TRUE, r);
3082     /* padding, column width */
3083     expect(2, rect.left);
3084     expect(50, rect.right);
3085
3086     /* try with indentation */
3087     item.mask = LVIF_INDENT;
3088     item.iIndent = 1;
3089     item.iItem = 0;
3090     item.iSubItem = 0;
3091     r = SendMessage(hwnd, LVM_SETITEM, 0, (LPARAM)&item);
3092     expect(TRUE, r);
3093
3094     /* bounds */
3095     rect.left = LVIR_BOUNDS;
3096     rect.right = rect.top = rect.bottom = -1;
3097     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
3098     expect(TRUE, r);
3099     /* padding + 1 icon width, column width */
3100     expect(0, rect.left);
3101     expect(150, rect.right);
3102
3103     /* select bounds */
3104     rect.left = LVIR_SELECTBOUNDS;
3105     rect.right = rect.top = rect.bottom = -1;
3106     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
3107     expect(TRUE, r);
3108     /* padding + 1 icon width, column width */
3109     expect(2 + 16, rect.left);
3110     expect(50, rect.right);
3111
3112     /* label bounds */
3113     rect.left = LVIR_LABEL;
3114     rect.right = rect.top = rect.bottom = -1;
3115     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
3116     expect(TRUE, r);
3117     /* padding + 2 icon widths, column width */
3118     expect(2 + 16*2, rect.left);
3119     expect(50, rect.right);
3120
3121     /* icon bounds */
3122     rect.left = LVIR_ICON;
3123     rect.right = rect.top = rect.bottom = -1;
3124     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
3125     expect(TRUE, r);
3126     /* padding + 1 icon width indentation, icon width */
3127     expect(2 + 16, rect.left);
3128     expect(34, rect.right);
3129
3130
3131     DestroyWindow(hwnd);
3132 }
3133
3134 static void test_editbox(void)
3135 {
3136     HWND hwnd, hwndedit, hwndedit2;
3137     LVITEMA item;
3138     DWORD r;
3139     static CHAR testitemA[]  = "testitem";
3140     static CHAR testitem1A[] = "testitem1";
3141     static CHAR buffer[10];
3142
3143     hwnd = create_listview_control(LVS_EDITLABELS);
3144     ok(hwnd != NULL, "failed to create a listview window\n");
3145
3146     insert_column(hwnd, 0);
3147
3148     memset(&item, 0, sizeof(item));
3149     item.mask = LVIF_TEXT;
3150     item.pszText = testitemA;
3151     item.iItem = 0;
3152     item.iSubItem = 0;
3153     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM)&item);
3154     expect(0, r);
3155
3156     /* setting focus is necessary */
3157     SetFocus(hwnd);
3158     hwndedit = (HWND)SendMessage(hwnd, LVM_EDITLABEL, 0, 0);
3159     ok(IsWindow(hwndedit), "Expected Edit window to be created\n");
3160
3161     /* modify initial string */
3162     r = SendMessage(hwndedit, WM_SETTEXT, 0, (LPARAM)testitem1A);
3163     expect(TRUE, r);
3164     /* return focus to listview */
3165     SetFocus(hwnd);
3166
3167     memset(&item, 0, sizeof(item));
3168     item.mask = LVIF_TEXT;
3169     item.pszText = buffer;
3170     item.cchTextMax = 10;
3171     item.iItem = 0;
3172     item.iSubItem = 0;
3173     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM)&item);
3174     expect(TRUE, r);
3175
3176     ok(strcmp(buffer, testitem1A) == 0, "Expected item text to change\n");
3177
3178     /* send LVM_EDITLABEL on already created edit */
3179     SetFocus(hwnd);
3180     hwndedit = (HWND)SendMessage(hwnd, LVM_EDITLABEL, 0, 0);
3181     ok(IsWindow(hwndedit), "Expected Edit window to be created\n");
3182     /* focus will be set to edit */
3183     ok(GetFocus() == hwndedit, "Expected Edit window to be focused\n");
3184     hwndedit2 = (HWND)SendMessage(hwnd, LVM_EDITLABEL, 0, 0);
3185     ok(IsWindow(hwndedit2), "Expected Edit window to be created\n");
3186
3187     /* creating label disabled when control isn't focused */
3188     SetFocus(0);
3189     hwndedit = (HWND)SendMessage(hwnd, LVM_EDITLABEL, 0, 0);
3190     todo_wine ok(hwndedit == NULL, "Expected Edit window not to be created\n");
3191
3192     /* check EN_KILLFOCUS handling */
3193     memset(&item, 0, sizeof(item));
3194     item.pszText = testitemA;
3195     item.iItem = 0;
3196     item.iSubItem = 0;
3197     r = SendMessage(hwnd, LVM_SETITEMTEXTA, 0, (LPARAM)&item);
3198     expect(TRUE, r);
3199
3200     SetFocus(hwnd);
3201     hwndedit = (HWND)SendMessage(hwnd, LVM_EDITLABEL, 0, 0);
3202     ok(IsWindow(hwndedit), "Expected Edit window to be created\n");
3203     /* modify edit and notify control that it lost focus */
3204     r = SendMessage(hwndedit, WM_SETTEXT, 0, (LPARAM)testitem1A);
3205     expect(TRUE, r);
3206     r = SendMessage(hwnd, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)hwndedit);
3207     expect(0, r);
3208     memset(&item, 0, sizeof(item));
3209     item.pszText = buffer;
3210     item.cchTextMax = 10;
3211     item.iItem = 0;
3212     item.iSubItem = 0;
3213     r = SendMessage(hwnd, LVM_GETITEMTEXTA, 0, (LPARAM)&item);
3214     expect(lstrlen(item.pszText), r);
3215     ok(strcmp(buffer, testitem1A) == 0, "Expected item text to change\n");
3216     /* end edit without saving */
3217     r = SendMessage(hwndedit, WM_KEYDOWN, VK_ESCAPE, 0);
3218     expect(0, r);
3219     memset(&item, 0, sizeof(item));
3220     item.pszText = buffer;
3221     item.cchTextMax = 10;
3222     item.iItem = 0;
3223     item.iSubItem = 0;
3224     r = SendMessage(hwnd, LVM_GETITEMTEXTA, 0, (LPARAM)&item);
3225     expect(lstrlen(item.pszText), r);
3226     ok(strcmp(buffer, testitem1A) == 0, "Expected item text to change\n");
3227
3228     /* LVM_EDITLABEL with -1 destroys current edit */
3229     hwndedit = (HWND)SendMessage(hwnd, LVM_GETEDITCONTROL, 0, 0);
3230     ok(hwndedit == NULL, "Expected Edit window not to be created\n");
3231     /* no edit present */
3232     hwndedit = (HWND)SendMessage(hwnd, LVM_EDITLABEL, -1, 0);
3233     ok(hwndedit == NULL, "Expected Edit window not to be created\n");
3234     hwndedit = (HWND)SendMessage(hwnd, LVM_EDITLABEL, 0, 0);
3235     ok(IsWindow(hwndedit), "Expected Edit window to be created\n");
3236     /* edit present */
3237     ok(GetFocus() == hwndedit, "Expected Edit to be focused\n");
3238     hwndedit2 = (HWND)SendMessage(hwnd, LVM_EDITLABEL, -1, 0);
3239     ok(hwndedit2 == NULL, "Expected Edit window not to be created\n");
3240     ok(!IsWindow(hwndedit), "Expected Edit window to be destroyed\n");
3241     ok(GetFocus() == hwnd, "Expected List to be focused\n");
3242     /* check another negative value */
3243     hwndedit = (HWND)SendMessage(hwnd, LVM_EDITLABEL, 0, 0);
3244     ok(IsWindow(hwndedit), "Expected Edit window to be created\n");
3245     ok(GetFocus() == hwndedit, "Expected Edit to be focused\n");
3246     hwndedit2 = (HWND)SendMessage(hwnd, LVM_EDITLABEL, -2, 0);
3247     ok(hwndedit2 == NULL, "Expected Edit window not to be created\n");
3248     ok(!IsWindow(hwndedit), "Expected Edit window to be destroyed\n");
3249     ok(GetFocus() == hwnd, "Expected List to be focused\n");
3250     /* and value greater than max item index */
3251     hwndedit = (HWND)SendMessage(hwnd, LVM_EDITLABEL, 0, 0);
3252     ok(IsWindow(hwndedit), "Expected Edit window to be created\n");
3253     ok(GetFocus() == hwndedit, "Expected Edit to be focused\n");
3254     r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
3255     hwndedit2 = (HWND)SendMessage(hwnd, LVM_EDITLABEL, r, 0);
3256     ok(hwndedit2 == NULL, "Expected Edit window not to be created\n");
3257     ok(!IsWindow(hwndedit), "Expected Edit window to be destroyed\n");
3258     ok(GetFocus() == hwnd, "Expected List to be focused\n");
3259
3260     /* messaging tests */
3261     SetFocus(hwnd);
3262     flush_sequences(sequences, NUM_MSG_SEQUENCES);
3263     blockEdit = FALSE;
3264     hwndedit = (HWND)SendMessage(hwnd, LVM_EDITLABEL, 0, 0);
3265     ok(IsWindow(hwndedit), "Expected Edit window to be created\n");
3266     /* testing only sizing messages */
3267     ok_sequence(sequences, EDITBOX_SEQ_INDEX, editbox_create_pos,
3268                 "edit box create - sizing", TRUE);
3269
3270     DestroyWindow(hwnd);
3271 }
3272
3273 static void test_notifyformat(void)
3274 {
3275     HWND hwnd, header;
3276     DWORD r;
3277
3278     hwnd = create_listview_control(0);
3279     ok(hwnd != NULL, "failed to create a listview window\n");
3280
3281     /* CCM_GETUNICODEFORMAT == LVM_GETUNICODEFORMAT,
3282        CCM_SETUNICODEFORMAT == LVM_SETUNICODEFORMAT */
3283     r = SendMessage(hwnd, LVM_GETUNICODEFORMAT, 0, 0);
3284     expect(0, r);
3285     r = SendMessage(hwnd, WM_NOTIFYFORMAT, 0, NF_QUERY);
3286     /* set */
3287     r = SendMessage(hwnd, LVM_SETUNICODEFORMAT, 1, 0);
3288     expect(0, r);
3289     r = SendMessage(hwnd, LVM_GETUNICODEFORMAT, 0, 0);
3290     if (r == 1)
3291     {
3292         r = SendMessage(hwnd, LVM_SETUNICODEFORMAT, 0, 0);
3293         expect(1, r);
3294         r = SendMessage(hwnd, LVM_GETUNICODEFORMAT, 0, 0);
3295         expect(0, r);
3296     }
3297     else
3298     {
3299         win_skip("LVM_GETUNICODEFORMAT is unsupported\n");
3300         DestroyWindow(hwnd);
3301         return;
3302     }
3303
3304     DestroyWindow(hwnd);
3305
3306     /* test failure in parent WM_NOTIFYFORMAT  */
3307     notifyFormat = 0;
3308     hwnd = create_listview_control(0);
3309     ok(hwnd != NULL, "failed to create a listview window\n");
3310     header = (HWND)SendMessage(hwnd, LVM_GETHEADER, 0, 0);
3311     ok(IsWindow(header), "expected header to be created\n");
3312     r = SendMessage(hwnd, LVM_GETUNICODEFORMAT, 0, 0);
3313     expect(0, r);
3314     r = SendMessage(header, HDM_GETUNICODEFORMAT, 0, 0);
3315     expect(1, r);
3316     r = SendMessage(hwnd, WM_NOTIFYFORMAT, 0, NF_QUERY);
3317     ok(r != 0, "Expected valid format\n");
3318
3319     notifyFormat = NFR_UNICODE;
3320     r = SendMessage(hwnd, WM_NOTIFYFORMAT, 0, NF_REQUERY);
3321     expect(NFR_UNICODE, r);
3322     r = SendMessage(hwnd, LVM_GETUNICODEFORMAT, 0, 0);
3323     expect(1, r);
3324     r = SendMessage(header, HDM_GETUNICODEFORMAT, 0, 0);
3325     expect(1, r);
3326
3327     notifyFormat = NFR_ANSI;
3328     r = SendMessage(hwnd, WM_NOTIFYFORMAT, 0, NF_REQUERY);
3329     expect(NFR_ANSI, r);
3330     r = SendMessage(hwnd, LVM_GETUNICODEFORMAT, 0, 0);
3331     expect(0, r);
3332     r = SendMessage(header, HDM_GETUNICODEFORMAT, 0, 0);
3333     expect(1, r);
3334
3335     DestroyWindow(hwnd);
3336
3337     /* try different unicode window combination and defaults */
3338     if (!GetModuleHandleW(NULL))
3339     {
3340         win_skip("Additional notify format tests are incompatible with Win9x\n");
3341         return;
3342     }
3343
3344     hwndparentW = create_parent_window(TRUE);
3345     ok(IsWindow(hwndparentW), "Unicode parent creation failed\n");
3346     if (!IsWindow(hwndparentW))  return;
3347
3348     notifyFormat = -1;
3349     hwnd = create_listview_controlW(0, hwndparentW);
3350     ok(hwnd != NULL, "failed to create a listview window\n");
3351     header = (HWND)SendMessage(hwnd, LVM_GETHEADER, 0, 0);
3352     ok(IsWindow(header), "expected header to be created\n");
3353     r = SendMessageW(hwnd, LVM_GETUNICODEFORMAT, 0, 0);
3354     expect(1, r);
3355     r = SendMessage(header, HDM_GETUNICODEFORMAT, 0, 0);
3356     expect(1, r);
3357     DestroyWindow(hwnd);
3358     /* receiving error code defaulting to ansi */
3359     notifyFormat = 0;
3360     hwnd = create_listview_controlW(0, hwndparentW);
3361     ok(hwnd != NULL, "failed to create a listview window\n");
3362     header = (HWND)SendMessage(hwnd, LVM_GETHEADER, 0, 0);
3363     ok(IsWindow(header), "expected header to be created\n");
3364     r = SendMessageW(hwnd, LVM_GETUNICODEFORMAT, 0, 0);
3365     expect(0, r);
3366     r = SendMessage(header, HDM_GETUNICODEFORMAT, 0, 0);
3367     expect(1, r);
3368     DestroyWindow(hwnd);
3369     /* receiving ansi code from unicode window, use it */
3370     notifyFormat = NFR_ANSI;
3371     hwnd = create_listview_controlW(0, hwndparentW);
3372     ok(hwnd != NULL, "failed to create a listview window\n");
3373     header = (HWND)SendMessage(hwnd, LVM_GETHEADER, 0, 0);
3374     ok(IsWindow(header), "expected header to be created\n");
3375     r = SendMessageW(hwnd, LVM_GETUNICODEFORMAT, 0, 0);
3376     expect(0, r);
3377     r = SendMessage(header, HDM_GETUNICODEFORMAT, 0, 0);
3378     expect(1, r);
3379     DestroyWindow(hwnd);
3380     /* unicode listview with ansi parent window */
3381     notifyFormat = -1;
3382     hwnd = create_listview_controlW(0, hwndparent);
3383     ok(hwnd != NULL, "failed to create a listview window\n");
3384     header = (HWND)SendMessage(hwnd, LVM_GETHEADER, 0, 0);
3385     ok(IsWindow(header), "expected header to be created\n");
3386     r = SendMessageW(hwnd, LVM_GETUNICODEFORMAT, 0, 0);
3387     expect(0, r);
3388     r = SendMessage(header, HDM_GETUNICODEFORMAT, 0, 0);
3389     expect(1, r);
3390     DestroyWindow(hwnd);
3391     /* unicode listview with ansi parent window, return error code */
3392     notifyFormat = 0;
3393     hwnd = create_listview_controlW(0, hwndparent);
3394     ok(hwnd != NULL, "failed to create a listview window\n");
3395     header = (HWND)SendMessage(hwnd, LVM_GETHEADER, 0, 0);
3396     ok(IsWindow(header), "expected header to be created\n");
3397     r = SendMessageW(hwnd, LVM_GETUNICODEFORMAT, 0, 0);
3398     expect(0, r);
3399     r = SendMessage(header, HDM_GETUNICODEFORMAT, 0, 0);
3400     expect(1, r);
3401     DestroyWindow(hwnd);
3402
3403     DestroyWindow(hwndparentW);
3404 }
3405
3406 static void test_indentation(void)
3407 {
3408     HWND hwnd;
3409     LVITEMA item;
3410     DWORD r;
3411
3412     hwnd = create_listview_control(0);
3413     ok(hwnd != NULL, "failed to create a listview window\n");
3414
3415     memset(&item, 0, sizeof(item));
3416     item.mask = LVIF_INDENT;
3417     item.iItem = 0;
3418     item.iIndent = I_INDENTCALLBACK;
3419     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM)&item);
3420     expect(0, r);
3421
3422     flush_sequences(sequences, NUM_MSG_SEQUENCES);
3423
3424     item.iItem = 0;
3425     item.mask = LVIF_INDENT;
3426     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM)&item);
3427     expect(TRUE, r);
3428
3429     ok_sequence(sequences, PARENT_SEQ_INDEX, single_getdispinfo_parent_seq,
3430                 "get indent dispinfo", FALSE);
3431
3432     DestroyWindow(hwnd);
3433 }
3434
3435 static INT CALLBACK DummyCompareEx(LPARAM first, LPARAM second, LPARAM param)
3436 {
3437     return 0;
3438 }
3439
3440 static BOOL is_below_comctl_5(void)
3441 {
3442     HWND hwnd;
3443     BOOL ret;
3444
3445     hwnd = create_listview_control(0);
3446     ok(hwnd != NULL, "failed to create a listview window\n");
3447     insert_item(hwnd, 0);
3448
3449     ret = SendMessage(hwnd, LVM_SORTITEMSEX, 0, (LPARAM)&DummyCompareEx);
3450
3451     DestroyWindow(hwnd);
3452
3453     return !ret;
3454 }
3455
3456 static void unload_v6_module(ULONG_PTR cookie)
3457 {
3458     HANDLE hKernel32;
3459     BOOL (WINAPI *pDeactivateActCtx)(DWORD, ULONG_PTR);
3460
3461     hKernel32 = GetModuleHandleA("kernel32.dll");
3462     pDeactivateActCtx = (void*)GetProcAddress(hKernel32, "DeactivateActCtx");
3463     if (!pDeactivateActCtx)
3464     {
3465         win_skip("Activation contexts unsupported\n");
3466         return;
3467     }
3468
3469     pDeactivateActCtx(0, cookie);
3470
3471     DeleteFileA(manifest_name);
3472 }
3473
3474 static BOOL load_v6_module(ULONG_PTR *pcookie)
3475 {
3476     HANDLE hKernel32;
3477     HANDLE (WINAPI *pCreateActCtxA)(ACTCTXA*);
3478     BOOL (WINAPI *pActivateActCtx)(HANDLE, ULONG_PTR*);
3479
3480     ACTCTXA ctx;
3481     HANDLE hCtx;
3482     BOOL ret;
3483     HANDLE file;
3484     DWORD written;
3485     HWND hwnd;
3486
3487     hKernel32 = GetModuleHandleA("kernel32.dll");
3488     pCreateActCtxA = (void*)GetProcAddress(hKernel32, "CreateActCtxA");
3489     pActivateActCtx = (void*)GetProcAddress(hKernel32, "ActivateActCtx");
3490     if (!(pCreateActCtxA && pActivateActCtx))
3491     {
3492         win_skip("Activation contexts unsupported. No version 6 tests possible.\n");
3493         return FALSE;
3494     }
3495
3496     /* create manifest */
3497     file = CreateFileA( manifest_name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL );
3498     if (file != INVALID_HANDLE_VALUE)
3499     {
3500         ret = (WriteFile( file, manifest, sizeof(manifest)-1, &written, NULL ) &&
3501                written == sizeof(manifest)-1);
3502         CloseHandle( file );
3503         if (!ret)
3504         {
3505             DeleteFileA( manifest_name );
3506             skip("Failed to fill manifest file. Skipping comctl32 V6 tests.\n");
3507             return FALSE;
3508         }
3509         else
3510             trace("created %s\n", manifest_name);
3511     }
3512     else
3513     {
3514         skip("Failed to create manifest file. Skipping comctl32 V6 tests.\n");
3515         return FALSE;
3516     }
3517
3518     memset(&ctx, 0, sizeof(ctx));
3519     ctx.cbSize = sizeof(ctx);
3520     ctx.lpSource = manifest_name;
3521
3522     hCtx = pCreateActCtxA(&ctx);
3523     ok(hCtx != 0, "Expected context handle\n");
3524
3525     ret = pActivateActCtx(hCtx, pcookie);
3526     expect(TRUE, ret);
3527
3528     if (!ret)
3529     {
3530         win_skip("A problem during context activation occurred.\n");
3531         DeleteFileA(manifest_name);
3532     }
3533
3534     else
3535     {
3536         /* this is a XP SP3 failure workaround */
3537         hwnd = CreateWindowExA(0, WC_LISTVIEW, "foo",
3538                                WS_CHILD | WS_BORDER | WS_VISIBLE | LVS_REPORT,
3539                                0, 0, 100, 100,
3540                                hwndparent, NULL, GetModuleHandleA(NULL), NULL);
3541         if (!IsWindow(hwnd))
3542         {
3543             win_skip("FIXME: failed to create ListView window.\n");
3544             unload_v6_module(*pcookie);
3545             return FALSE;
3546         }
3547         else
3548             DestroyWindow(hwnd);
3549     }
3550
3551     return ret;
3552 }
3553
3554 static void test_get_set_view(void)
3555 {
3556     HWND hwnd;
3557     DWORD ret;
3558     DWORD_PTR style;
3559
3560     /* test style->view mapping */
3561     hwnd = create_listview_control(0);
3562     ok(hwnd != NULL, "failed to create a listview window\n");
3563
3564     ret = SendMessage(hwnd, LVM_GETVIEW, 0, 0);
3565     expect(LV_VIEW_DETAILS, ret);
3566
3567     style = GetWindowLongPtr(hwnd, GWL_STYLE);
3568     /* LVS_ICON == 0 */
3569     SetWindowLongPtr(hwnd, GWL_STYLE, style & ~LVS_REPORT);
3570     ret = SendMessage(hwnd, LVM_GETVIEW, 0, 0);
3571     expect(LV_VIEW_ICON, ret);
3572
3573     style = GetWindowLongPtr(hwnd, GWL_STYLE);
3574     SetWindowLongPtr(hwnd, GWL_STYLE, style | LVS_SMALLICON);
3575     ret = SendMessage(hwnd, LVM_GETVIEW, 0, 0);
3576     expect(LV_VIEW_SMALLICON, ret);
3577
3578     style = GetWindowLongPtr(hwnd, GWL_STYLE);
3579     SetWindowLongPtr(hwnd, GWL_STYLE, (style & ~LVS_SMALLICON) | LVS_LIST);
3580     ret = SendMessage(hwnd, LVM_GETVIEW, 0, 0);
3581     expect(LV_VIEW_LIST, ret);
3582
3583     /* switching view doesn't touch window style */
3584     ret = SendMessage(hwnd, LVM_SETVIEW, LV_VIEW_DETAILS, 0);
3585     expect(1, ret);
3586     style = GetWindowLongPtr(hwnd, GWL_STYLE);
3587     ok(style & LVS_LIST, "Expected style to be preserved\n");
3588     ret = SendMessage(hwnd, LVM_SETVIEW, LV_VIEW_ICON, 0);
3589     expect(1, ret);
3590     style = GetWindowLongPtr(hwnd, GWL_STYLE);
3591     ok(style & LVS_LIST, "Expected style to be preserved\n");
3592     ret = SendMessage(hwnd, LVM_SETVIEW, LV_VIEW_SMALLICON, 0);
3593     expect(1, ret);
3594     style = GetWindowLongPtr(hwnd, GWL_STYLE);
3595     ok(style & LVS_LIST, "Expected style to be preserved\n");
3596
3597     DestroyWindow(hwnd);
3598 }
3599
3600 static void test_canceleditlabel(void)
3601 {
3602     HWND hwnd, hwndedit;
3603     DWORD ret;
3604     CHAR buff[10];
3605     LVITEMA itema;
3606     static CHAR test[] = "test";
3607     static const CHAR test1[] = "test1";
3608
3609     hwnd = create_listview_control(LVS_EDITLABELS);
3610     ok(hwnd != NULL, "failed to create a listview window\n");
3611
3612     insert_item(hwnd, 0);
3613
3614     /* try without edit created */
3615     ret = SendMessage(hwnd, LVM_CANCELEDITLABEL, 0, 0);
3616     expect(TRUE, ret);
3617
3618     /* cancel without data change */
3619     SetFocus(hwnd);
3620     hwndedit = (HWND)SendMessage(hwnd, LVM_EDITLABEL, 0, 0);
3621     ok(IsWindow(hwndedit), "Expected edit control to be created\n");
3622     ret = SendMessage(hwnd, LVM_CANCELEDITLABEL, 0, 0);
3623     expect(TRUE, ret);
3624     ok(!IsWindow(hwndedit), "Expected edit control to be destroyed\n");
3625
3626     /* cancel after data change */
3627     memset(&itema, 0, sizeof(itema));
3628     itema.pszText = test;
3629     ret = SendMessage(hwnd, LVM_SETITEMTEXT, 0, (LPARAM)&itema);
3630     expect(TRUE, ret);
3631     SetFocus(hwnd);
3632     hwndedit = (HWND)SendMessage(hwnd, LVM_EDITLABEL, 0, 0);
3633     ok(IsWindow(hwndedit), "Expected edit control to be created\n");
3634     ret = SetWindowText(hwndedit, test1);
3635     ok(ret != 0, "Expected edit text to change\n");
3636     ret = SendMessage(hwnd, LVM_CANCELEDITLABEL, 0, 0);
3637     expect(TRUE, ret);
3638     ok(!IsWindow(hwndedit), "Expected edit control to be destroyed\n");
3639     memset(&itema, 0, sizeof(itema));
3640     itema.pszText = buff;
3641     itema.cchTextMax = sizeof(buff)/sizeof(CHAR);
3642     ret = SendMessage(hwnd, LVM_GETITEMTEXT, 0, (LPARAM)&itema);
3643     expect(5, ret);
3644     ok(strcmp(buff, test1) == 0, "Expected label text not to change\n");
3645
3646     DestroyWindow(hwnd);
3647 }
3648
3649 static void test_mapidindex(void)
3650 {
3651     HWND hwnd;
3652     DWORD ret;
3653
3654     /* LVM_MAPINDEXTOID unsupported with LVS_OWNERDATA */
3655     hwnd = create_listview_control(LVS_OWNERDATA);
3656     ok(hwnd != NULL, "failed to create a listview window\n");
3657     insert_item(hwnd, 0);
3658     ret = SendMessage(hwnd, LVM_MAPINDEXTOID, 0, 0);
3659     expect(-1, ret);
3660     DestroyWindow(hwnd);
3661
3662     hwnd = create_listview_control(0);
3663     ok(hwnd != NULL, "failed to create a listview window\n");
3664
3665     /* LVM_MAPINDEXTOID with invalid index */
3666     ret = SendMessage(hwnd, LVM_MAPINDEXTOID, 0, 0);
3667     expect(-1, ret);
3668
3669     insert_item(hwnd, 0);
3670     insert_item(hwnd, 1);
3671
3672     ret = SendMessage(hwnd, LVM_MAPINDEXTOID, -1, 0);
3673     expect(-1, ret);
3674     ret = SendMessage(hwnd, LVM_MAPINDEXTOID, 2, 0);
3675     expect(-1, ret);
3676
3677     ret = SendMessage(hwnd, LVM_MAPINDEXTOID, 0, 0);
3678     expect(0, ret);
3679     ret = SendMessage(hwnd, LVM_MAPINDEXTOID, 1, 0);
3680     expect(1, ret);
3681     /* remove 0 indexed item, id retained */
3682     SendMessage(hwnd, LVM_DELETEITEM, 0, 0);
3683     ret = SendMessage(hwnd, LVM_MAPINDEXTOID, 0, 0);
3684     expect(1, ret);
3685     /* new id starts from previous value */
3686     insert_item(hwnd, 1);
3687     ret = SendMessage(hwnd, LVM_MAPINDEXTOID, 1, 0);
3688     expect(2, ret);
3689
3690     /* get index by id */
3691     ret = SendMessage(hwnd, LVM_MAPIDTOINDEX, -1, 0);
3692     expect(-1, ret);
3693     ret = SendMessage(hwnd, LVM_MAPIDTOINDEX, 0, 0);
3694     expect(-1, ret);
3695     ret = SendMessage(hwnd, LVM_MAPIDTOINDEX, 1, 0);
3696     expect(0, ret);
3697     ret = SendMessage(hwnd, LVM_MAPIDTOINDEX, 2, 0);
3698     expect(1, ret);
3699
3700     DestroyWindow(hwnd);
3701 }
3702
3703 static void test_getitemspacing(void)
3704 {
3705     HWND hwnd;
3706     DWORD ret;
3707     INT cx, cy;
3708     HIMAGELIST himl;
3709     HBITMAP hbmp;
3710     LVITEMA itema;
3711
3712     cx = GetSystemMetrics(SM_CXICONSPACING) - GetSystemMetrics(SM_CXICON);
3713     cy = GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON);
3714
3715     /* LVS_ICON */
3716     hwnd = create_custom_listview_control(0);
3717     ret = SendMessage(hwnd, LVM_GETITEMSPACING, FALSE, 0);
3718 todo_wine {
3719     expect(cx, LOWORD(ret));
3720     expect(cy, HIWORD(ret));
3721 }
3722     /* now try with icons */
3723     himl = ImageList_Create(40, 40, 0, 4, 4);
3724     ok(himl != NULL, "failed to create imagelist\n");
3725     hbmp = CreateBitmap(40, 40, 1, 1, NULL);
3726     ok(hbmp != NULL, "failed to create bitmap\n");
3727     ret = ImageList_Add(himl, hbmp, 0);
3728     expect(0, ret);
3729     ret = SendMessage(hwnd, LVM_SETIMAGELIST, 0, (LPARAM)himl);
3730     expect(0, ret);
3731
3732     itema.mask = LVIF_IMAGE;
3733     itema.iImage = 0;
3734     itema.iItem = 0;
3735     itema.iSubItem = 0;
3736     ret = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM)&itema);
3737     expect(0, ret);
3738     ret = SendMessage(hwnd, LVM_GETITEMSPACING, FALSE, 0);
3739 todo_wine {
3740     /* spacing + icon size returned */
3741     expect(cx + 40, LOWORD(ret));
3742     expect(cy + 40, HIWORD(ret));
3743 }
3744     DestroyWindow(hwnd);
3745     /* LVS_SMALLICON */
3746     hwnd = create_custom_listview_control(LVS_SMALLICON);
3747     ret = SendMessage(hwnd, LVM_GETITEMSPACING, FALSE, 0);
3748 todo_wine {
3749     expect(cx, LOWORD(ret));
3750     expect(cy, HIWORD(ret));
3751 }
3752     DestroyWindow(hwnd);
3753     /* LVS_REPORT */
3754     hwnd = create_custom_listview_control(LVS_REPORT);
3755     ret = SendMessage(hwnd, LVM_GETITEMSPACING, FALSE, 0);
3756 todo_wine {
3757     expect(cx, LOWORD(ret));
3758     expect(cy, HIWORD(ret));
3759 }
3760     DestroyWindow(hwnd);
3761     /* LVS_LIST */
3762     hwnd = create_custom_listview_control(LVS_LIST);
3763     ret = SendMessage(hwnd, LVM_GETITEMSPACING, FALSE, 0);
3764 todo_wine {
3765     expect(cx, LOWORD(ret));
3766     expect(cy, HIWORD(ret));
3767 }
3768     DestroyWindow(hwnd);
3769 }
3770
3771 static void test_getcolumnwidth(void)
3772 {
3773     HWND hwnd;
3774     DWORD ret;
3775     DWORD_PTR style;
3776     LVCOLUMNA col;
3777
3778     /* default column width */
3779     hwnd = create_custom_listview_control(0);
3780     ret = SendMessage(hwnd, LVM_GETCOLUMNWIDTH, 0, 0);
3781     expect(0, ret);
3782     style = GetWindowLong(hwnd, GWL_STYLE);
3783     SetWindowLong(hwnd, GWL_STYLE, style | LVS_LIST);
3784     ret = SendMessage(hwnd, LVM_GETCOLUMNWIDTH, 0, 0);
3785     todo_wine expect(8, ret);
3786     style = GetWindowLong(hwnd, GWL_STYLE) & ~LVS_LIST;
3787     SetWindowLong(hwnd, GWL_STYLE, style | LVS_REPORT);
3788     col.mask = 0;
3789     ret = SendMessage(hwnd, LVM_INSERTCOLUMNA, 0, (LPARAM)&col);
3790     expect(0, ret);
3791     ret = SendMessage(hwnd, LVM_GETCOLUMNWIDTH, 0, 0);
3792     expect(10, ret);
3793     DestroyWindow(hwnd);
3794 }
3795
3796 static void test_scrollnotify(void)
3797 {
3798     HWND hwnd;
3799     DWORD ret;
3800
3801     hwnd = create_listview_control(0);
3802
3803     insert_column(hwnd, 0);
3804     insert_column(hwnd, 1);
3805     insert_item(hwnd, 0);
3806
3807     /* make it scrollable - resize */
3808     ret = SendMessage(hwnd, LVM_SETCOLUMNWIDTH, 0, MAKELPARAM(100, 0));
3809     expect(TRUE, ret);
3810     ret = SendMessage(hwnd, LVM_SETCOLUMNWIDTH, 1, MAKELPARAM(100, 0));
3811     expect(TRUE, ret);
3812
3813     /* try with dummy call */
3814     flush_sequences(sequences, NUM_MSG_SEQUENCES);
3815     ret = SendMessage(hwnd, LVM_SCROLL, 0, 0);
3816     expect(TRUE, ret);
3817     ok_sequence(sequences, PARENT_SEQ_INDEX, scroll_parent_seq,
3818                 "scroll notify 1", TRUE);
3819
3820     flush_sequences(sequences, NUM_MSG_SEQUENCES);
3821     ret = SendMessage(hwnd, LVM_SCROLL, 1, 0);
3822     expect(TRUE, ret);
3823     ok_sequence(sequences, PARENT_SEQ_INDEX, scroll_parent_seq,
3824                 "scroll notify 2", TRUE);
3825
3826     flush_sequences(sequences, NUM_MSG_SEQUENCES);
3827     ret = SendMessage(hwnd, LVM_SCROLL, 1, 1);
3828     expect(TRUE, ret);
3829     ok_sequence(sequences, PARENT_SEQ_INDEX, scroll_parent_seq,
3830                 "scroll notify 3", TRUE);
3831
3832     DestroyWindow(hwnd);
3833 }
3834
3835 START_TEST(listview)
3836 {
3837     HMODULE hComctl32;
3838     BOOL (WINAPI *pInitCommonControlsEx)(const INITCOMMONCONTROLSEX*);
3839
3840     ULONG_PTR ctx_cookie;
3841
3842     hComctl32 = GetModuleHandleA("comctl32.dll");
3843     pInitCommonControlsEx = (void*)GetProcAddress(hComctl32, "InitCommonControlsEx");
3844     if (pInitCommonControlsEx)
3845     {
3846         INITCOMMONCONTROLSEX iccex;
3847         iccex.dwSize = sizeof(iccex);
3848         iccex.dwICC  = ICC_LISTVIEW_CLASSES;
3849         pInitCommonControlsEx(&iccex);
3850     }
3851     else
3852         InitCommonControls();
3853
3854     init_msg_sequences(sequences, NUM_MSG_SEQUENCES);
3855
3856     hwndparent = create_parent_window(FALSE);
3857     flush_sequences(sequences, NUM_MSG_SEQUENCES);
3858
3859     g_is_below_5 = is_below_comctl_5();
3860
3861     test_images();
3862     test_checkboxes();
3863     test_items();
3864     test_create();
3865     test_redraw();
3866     test_customdraw();
3867     test_icon_spacing();
3868     test_color();
3869     test_item_count();
3870     test_item_position();
3871     test_columns();
3872     test_getorigin();
3873     test_multiselect();
3874     test_getitemrect();
3875     test_subitem_rect();
3876     test_sorting();
3877     test_ownerdata();
3878     test_norecompute();
3879     test_nosortheader();
3880     test_setredraw();
3881     test_hittest();
3882     test_getviewrect();
3883     test_getitemposition();
3884     test_columnscreation();
3885     test_editbox();
3886     test_notifyformat();
3887     test_indentation();
3888     test_getitemspacing();
3889     test_getcolumnwidth();
3890
3891     if (!load_v6_module(&ctx_cookie))
3892     {
3893         DestroyWindow(hwndparent);
3894         return;
3895     }
3896
3897     /* comctl32 version 6 tests start here */
3898     test_get_set_view();
3899     test_canceleditlabel();
3900     test_mapidindex();
3901     test_scrollnotify();
3902
3903     unload_v6_module(ctx_cookie);
3904
3905     DestroyWindow(hwndparent);
3906 }