wined3d: Recognize Nvidia GT520 cards.
[wine] / dlls / comctl32 / tests / listview.c
1 /*
2  * ListView tests
3  *
4  * Copyright 2006 Mike McCormack for CodeWeavers
5  * Copyright 2007 George Gov
6  * Copyright 2009-2012 Nikolay Sivov
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include <stdio.h>
24 #include <windows.h>
25 #include <commctrl.h>
26
27 #include "wine/test.h"
28 #include "v6util.h"
29 #include "msg.h"
30
31 #define PARENT_SEQ_INDEX       0
32 #define PARENT_FULL_SEQ_INDEX  1
33 #define LISTVIEW_SEQ_INDEX     2
34 #define EDITBOX_SEQ_INDEX      3
35 #define COMBINED_SEQ_INDEX     4
36 #define NUM_MSG_SEQUENCES      5
37
38 #define LISTVIEW_ID 0
39 #define HEADER_ID   1
40
41 #define expect(expected, got) ok(got == expected, "Expected %d, got %d\n", expected, got)
42 #define expect2(expected1, expected2, got1, got2) ok(expected1 == got1 && expected2 == got2, \
43        "expected (%d,%d), got (%d,%d)\n", expected1, expected2, got1, got2)
44
45 static const WCHAR testparentclassW[] =
46     {'L','i','s','t','v','i','e','w',' ','t','e','s','t',' ','p','a','r','e','n','t','W', 0};
47
48 static HWND hwndparent, hwndparentW;
49 /* prevents edit box creation, LVN_BEGINLABELEDIT return value */
50 static BOOL blockEdit;
51 /* return nonzero on NM_HOVER */
52 static BOOL g_block_hover;
53 /* notification data for LVN_ITEMCHANGED */
54 static NMLISTVIEW g_nmlistview;
55 /* format reported to control:
56    -1 falls to defproc, anything else returned */
57 static INT notifyFormat;
58 /* indicates we're running < 5.80 version */
59 static BOOL g_is_below_5;
60 /* item data passed to LVN_GETDISPINFOA */
61 static LVITEMA g_itema;
62 /* alter notification code A->W */
63 static BOOL g_disp_A_to_W;
64 /* dispinfo data sent with LVN_LVN_ENDLABELEDIT */
65 static NMLVDISPINFO g_editbox_disp_info;
66
67 static HWND subclass_editbox(HWND hwndListview);
68
69 static struct msg_sequence *sequences[NUM_MSG_SEQUENCES];
70
71 static const struct message create_ownerdrawfixed_parent_seq[] = {
72     { WM_NOTIFYFORMAT, sent },
73     { WM_QUERYUISTATE, sent|optional }, /* Win2K and higher */
74     { WM_MEASUREITEM, sent },
75     { WM_PARENTNOTIFY, sent },
76     { 0 }
77 };
78
79 static const struct message redraw_listview_seq[] = {
80     { WM_PAINT,      sent|id,            0, 0, LISTVIEW_ID },
81     { WM_PAINT,      sent|id,            0, 0, HEADER_ID },
82     { WM_NCPAINT,    sent|id|defwinproc, 0, 0, HEADER_ID },
83     { WM_ERASEBKGND, sent|id|defwinproc|optional, 0, 0, HEADER_ID },
84     { WM_NOTIFY,     sent|id|defwinproc, 0, 0, LISTVIEW_ID },
85     { WM_NCPAINT,    sent|id|defwinproc, 0, 0, LISTVIEW_ID },
86     { WM_ERASEBKGND, sent|id|defwinproc|optional, 0, 0, LISTVIEW_ID },
87     { 0 }
88 };
89
90 static const struct message listview_icon_spacing_seq[] = {
91     { LVM_SETICONSPACING, sent|lparam, 0, MAKELPARAM(20, 30) },
92     { LVM_SETICONSPACING, sent|lparam, 0, MAKELPARAM(25, 35) },
93     { LVM_SETICONSPACING, sent|lparam, 0, MAKELPARAM(-1, -1) },
94     { 0 }
95 };
96
97 static const struct message listview_color_seq[] = {
98     { LVM_SETBKCOLOR,     sent|lparam, 0, RGB(0,0,0) },
99     { LVM_GETBKCOLOR,     sent },
100     { LVM_SETTEXTCOLOR,   sent|lparam, 0, RGB(0,0,0) },
101     { LVM_GETTEXTCOLOR,   sent },
102     { LVM_SETTEXTBKCOLOR, sent|lparam, 0, RGB(0,0,0) },
103     { LVM_GETTEXTBKCOLOR, sent },
104
105     { LVM_SETBKCOLOR,     sent|lparam, 0, RGB(100,50,200) },
106     { LVM_GETBKCOLOR,     sent },
107     { LVM_SETTEXTCOLOR,   sent|lparam, 0, RGB(100,50,200) },
108     { LVM_GETTEXTCOLOR,   sent },
109     { LVM_SETTEXTBKCOLOR, sent|lparam, 0, RGB(100,50,200) },
110     { LVM_GETTEXTBKCOLOR, sent },
111
112     { LVM_SETBKCOLOR,     sent|lparam, 0, CLR_NONE },
113     { LVM_GETBKCOLOR,     sent },
114     { LVM_SETTEXTCOLOR,   sent|lparam, 0, CLR_NONE },
115     { LVM_GETTEXTCOLOR,   sent },
116     { LVM_SETTEXTBKCOLOR, sent|lparam, 0, CLR_NONE },
117     { LVM_GETTEXTBKCOLOR, sent },
118
119     { LVM_SETBKCOLOR,     sent|lparam, 0, RGB(255,255,255) },
120     { LVM_GETBKCOLOR,     sent },
121     { LVM_SETTEXTCOLOR,   sent|lparam, 0, RGB(255,255,255) },
122     { LVM_GETTEXTCOLOR,   sent },
123     { LVM_SETTEXTBKCOLOR, sent|lparam, 0, RGB(255,255,255) },
124     { LVM_GETTEXTBKCOLOR, sent },
125     { 0 }
126 };
127
128 static const struct message listview_item_count_seq[] = {
129     { LVM_GETITEMCOUNT,   sent },
130     { LVM_INSERTITEM,     sent },
131     { LVM_INSERTITEM,     sent },
132     { LVM_INSERTITEM,     sent },
133     { LVM_GETITEMCOUNT,   sent },
134     { LVM_DELETEITEM,     sent|wparam, 2 },
135     { WM_NCPAINT,         sent|optional },
136     { WM_ERASEBKGND,      sent|optional },
137     { LVM_GETITEMCOUNT,   sent },
138     { LVM_DELETEALLITEMS, sent },
139     { LVM_GETITEMCOUNT,   sent },
140     { LVM_INSERTITEM,     sent },
141     { LVM_INSERTITEM,     sent },
142     { LVM_GETITEMCOUNT,   sent },
143     { LVM_INSERTITEM,     sent },
144     { LVM_GETITEMCOUNT,   sent },
145     { 0 }
146 };
147
148 static const struct message listview_itempos_seq[] = {
149     { LVM_INSERTITEM,      sent },
150     { LVM_INSERTITEM,      sent },
151     { LVM_INSERTITEM,      sent },
152     { LVM_SETITEMPOSITION, sent|wparam|lparam, 1, MAKELPARAM(10,5) },
153     { WM_NCPAINT,          sent|optional },
154     { WM_ERASEBKGND,       sent|optional },
155     { LVM_GETITEMPOSITION, sent|wparam,        1 },
156     { LVM_SETITEMPOSITION, sent|wparam|lparam, 2, MAKELPARAM(0,0) },
157     { LVM_GETITEMPOSITION, sent|wparam,        2 },
158     { LVM_SETITEMPOSITION, sent|wparam|lparam, 0, MAKELPARAM(20,20) },
159     { LVM_GETITEMPOSITION, sent|wparam,        0 },
160     { 0 }
161 };
162
163 static const struct message listview_ownerdata_switchto_seq[] = {
164     { WM_STYLECHANGING,    sent },
165     { WM_STYLECHANGED,     sent },
166     { 0 }
167 };
168
169 static const struct message listview_getorderarray_seq[] = {
170     { LVM_GETCOLUMNORDERARRAY, sent|id|wparam, 2, 0, LISTVIEW_ID },
171     { HDM_GETORDERARRAY,       sent|id|wparam, 2, 0, HEADER_ID },
172     { 0 }
173 };
174
175 static const struct message empty_seq[] = {
176     { 0 }
177 };
178
179 static const struct message forward_erasebkgnd_parent_seq[] = {
180     { WM_ERASEBKGND, sent },
181     { 0 }
182 };
183
184 static const struct message ownderdata_select_focus_parent_seq[] = {
185     { WM_NOTIFY, sent|id, 0, 0, LVN_ITEMCHANGED },
186     { WM_NOTIFY, sent|id, 0, 0, LVN_GETDISPINFOA },
187     { WM_NOTIFY, sent|id|optional, 0, 0, LVN_GETDISPINFOA }, /* version 4.7x */
188     { 0 }
189 };
190
191 static const struct message ownerdata_setstate_all_parent_seq[] = {
192     { WM_NOTIFY, sent|id, 0, 0, LVN_ITEMCHANGED },
193     { 0 }
194 };
195
196 static const struct message ownerdata_defocus_all_parent_seq[] = {
197     { WM_NOTIFY, sent|id, 0, 0, LVN_ITEMCHANGED },
198     { WM_NOTIFY, sent|id, 0, 0, LVN_GETDISPINFOA },
199     { WM_NOTIFY, sent|id|optional, 0, 0, LVN_GETDISPINFOA },
200     { WM_NOTIFY, sent|id, 0, 0, LVN_ITEMCHANGED },
201     { 0 }
202 };
203
204 static const struct message ownerdata_deselect_all_parent_seq[] = {
205     { WM_NOTIFY, sent|id, 0, 0, LVN_ODCACHEHINT },
206     { WM_NOTIFY, sent|id, 0, 0, LVN_ITEMCHANGED },
207     { 0 }
208 };
209
210 static const struct message select_all_parent_seq[] = {
211     { WM_NOTIFY, sent|id, 0, 0, LVN_ITEMCHANGING },
212     { WM_NOTIFY, sent|id, 0, 0, LVN_ITEMCHANGED },
213
214     { WM_NOTIFY, sent|id, 0, 0, LVN_ITEMCHANGING },
215     { WM_NOTIFY, sent|id, 0, 0, LVN_ITEMCHANGED },
216
217     { WM_NOTIFY, sent|id, 0, 0, LVN_ITEMCHANGING },
218     { WM_NOTIFY, sent|id, 0, 0, LVN_ITEMCHANGED },
219
220     { WM_NOTIFY, sent|id, 0, 0, LVN_ITEMCHANGING },
221     { WM_NOTIFY, sent|id, 0, 0, LVN_ITEMCHANGED },
222
223     { WM_NOTIFY, sent|id, 0, 0, LVN_ITEMCHANGING },
224     { WM_NOTIFY, sent|id, 0, 0, LVN_ITEMCHANGED },
225     { 0 }
226 };
227
228 static const struct message textcallback_set_again_parent_seq[] = {
229     { WM_NOTIFY, sent|id, 0, 0, LVN_ITEMCHANGING },
230     { WM_NOTIFY, sent|id, 0, 0, LVN_ITEMCHANGED  },
231     { 0 }
232 };
233
234 static const struct message single_getdispinfo_parent_seq[] = {
235     { WM_NOTIFY, sent|id, 0, 0, LVN_GETDISPINFOA },
236     { 0 }
237 };
238
239 static const struct message getitemposition_seq1[] = {
240     { LVM_GETITEMPOSITION, sent|id, 0, 0, LISTVIEW_ID },
241     { 0 }
242 };
243
244 static const struct message getitemposition_seq2[] = {
245     { LVM_GETITEMPOSITION, sent|id, 0, 0, LISTVIEW_ID },
246     { HDM_GETITEMRECT, sent|id, 0, 0, HEADER_ID },
247     { 0 }
248 };
249
250 static const struct message editbox_create_pos[] = {
251     /* sequence sent after LVN_BEGINLABELEDIT */
252     /* next two are 4.7x specific */
253     { WM_WINDOWPOSCHANGING, sent },
254     { WM_WINDOWPOSCHANGED, sent|optional },
255
256     { WM_WINDOWPOSCHANGING, sent|optional },
257     { WM_NCCALCSIZE, sent },
258     { WM_WINDOWPOSCHANGED, sent },
259     { WM_MOVE, sent|defwinproc },
260     { WM_SIZE, sent|defwinproc },
261     /* the rest is todo, skipped in 4.7x */
262     { WM_WINDOWPOSCHANGING, sent|optional },
263     { WM_WINDOWPOSCHANGED, sent|optional },
264     { 0 }
265 };
266
267 static const struct message scroll_parent_seq[] = {
268     { WM_NOTIFY, sent|id, 0, 0, LVN_BEGINSCROLL },
269     { WM_NOTIFY, sent|id, 0, 0, LVN_ENDSCROLL },
270     { 0 }
271 };
272
273 static const struct message setredraw_seq[] = {
274     { WM_SETREDRAW, sent|id|wparam, FALSE, 0, LISTVIEW_ID },
275     { 0 }
276 };
277
278 static const struct message lvs_ex_transparentbkgnd_seq[] = {
279     { WM_PRINTCLIENT, sent|lparam, 0, PRF_ERASEBKGND },
280     { 0 }
281 };
282
283 static const struct message edit_end_nochange[] = {
284     { WM_NOTIFY, sent|id, 0, 0, LVN_ENDLABELEDITA },
285     { WM_NOTIFY, sent|id, 0, 0, NM_CUSTOMDRAW },     /* todo */
286     { WM_NOTIFY, sent|id, 0, 0, NM_SETFOCUS },
287     { 0 }
288 };
289
290 static const struct message hover_parent[] = {
291     { WM_GETDLGCODE, sent }, /* todo_wine */
292     { WM_NOTIFY, sent|id, 0, 0, NM_HOVER },
293     { 0 }
294 };
295
296 static const struct message listview_destroy[] = {
297     { 0x0090, sent|optional }, /* Vista */
298     { WM_PARENTNOTIFY, sent },
299     { WM_SHOWWINDOW, sent },
300     { WM_WINDOWPOSCHANGING, sent },
301     { WM_WINDOWPOSCHANGED, sent|optional },
302     { WM_DESTROY, sent },
303     { WM_NOTIFY, sent|id, 0, 0, LVN_DELETEALLITEMS },
304     { WM_NCDESTROY, sent },
305     { 0 }
306 };
307
308 static const struct message listview_header_changed_seq[] = {
309     { LVM_SETCOLUMNA, sent },
310     { WM_NOTIFY, sent|id|defwinproc, 0, 0, LISTVIEW_ID },
311     { WM_NOTIFY, sent|id|defwinproc, 0, 0, LISTVIEW_ID },
312     { 0 }
313 };
314
315 static const struct message parent_header_click_seq[] = {
316     { WM_NOTIFY, sent|id, 0, 0, LVN_COLUMNCLICK },
317     { WM_NOTIFY, sent|id, 0, 0, HDN_ITEMCLICKA },
318     { 0 }
319 };
320
321 static const struct message parent_header_divider_dclick_seq[] = {
322     { WM_NOTIFY, sent|id, 0, 0, HDN_ITEMCHANGINGA },
323     { WM_NOTIFY, sent|id, 0, 0, NM_CUSTOMDRAW },
324     { WM_NOTIFY, sent|id, 0, 0, NM_CUSTOMDRAW },
325     { WM_NOTIFY, sent|id, 0, 0, HDN_ITEMCHANGEDA },
326     { WM_NOTIFY, sent|id, 0, 0, HDN_DIVIDERDBLCLICKA },
327     { 0 }
328 };
329
330 static LRESULT WINAPI parent_wnd_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
331 {
332     static LONG defwndproc_counter = 0;
333     LRESULT ret;
334     struct message msg;
335
336     msg.message = message;
337     msg.flags = sent|wparam|lparam;
338     if (defwndproc_counter) msg.flags |= defwinproc;
339     msg.wParam = wParam;
340     msg.lParam = lParam;
341     if (message == WM_NOTIFY && lParam) msg.id = ((NMHDR*)lParam)->code;
342
343     /* log system messages, except for painting */
344     if (message < WM_USER &&
345         message != WM_PAINT &&
346         message != WM_ERASEBKGND &&
347         message != WM_NCPAINT &&
348         message != WM_NCHITTEST &&
349         message != WM_GETTEXT &&
350         message != WM_GETICON &&
351         message != WM_DEVICECHANGE)
352     {
353         trace("parent: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
354
355         add_message(sequences, PARENT_SEQ_INDEX, &msg);
356         add_message(sequences, COMBINED_SEQ_INDEX, &msg);
357     }
358     add_message(sequences, PARENT_FULL_SEQ_INDEX, &msg);
359
360     switch (message)
361     {
362       case WM_NOTIFY:
363       {
364           switch (((NMHDR*)lParam)->code)
365           {
366           case LVN_BEGINLABELEDIT:
367           {
368               HWND edit = NULL;
369
370               /* subclass edit box */
371               if (!blockEdit)
372                   edit = subclass_editbox(((NMHDR*)lParam)->hwndFrom);
373
374               if (edit)
375               {
376                   INT len = SendMessageA(edit, EM_GETLIMITTEXT, 0, 0);
377                   ok(len == 259 || broken(len == 260) /* includes NULL in NT4 */,
378                       "text limit %d, expected 259\n", len);
379               }
380
381               return blockEdit;
382           }
383           case LVN_ENDLABELEDIT:
384               {
385               HWND edit;
386
387               /* always accept new item text */
388               NMLVDISPINFO *di = (NMLVDISPINFO*)lParam;
389               g_editbox_disp_info = *di;
390               trace("LVN_ENDLABELEDIT: text=%s\n", di->item.pszText ? di->item.pszText : "(null)");
391
392               /* edit control still available from this notification */
393               edit = (HWND)SendMessageA(((NMHDR*)lParam)->hwndFrom, LVM_GETEDITCONTROL, 0, 0);
394               ok(IsWindow(edit), "expected valid edit control handle\n");
395               ok((GetWindowLongA(edit, GWL_STYLE) & ES_MULTILINE) == 0, "edit is multiline\n");
396
397               return TRUE;
398               }
399           case LVN_BEGINSCROLL:
400           case LVN_ENDSCROLL:
401               {
402               NMLVSCROLL *pScroll = (NMLVSCROLL*)lParam;
403
404               trace("LVN_%sSCROLL: (%d,%d)\n", pScroll->hdr.code == LVN_BEGINSCROLL ?
405                                                "BEGIN" : "END", pScroll->dx, pScroll->dy);
406               }
407               break;
408           case LVN_ITEMCHANGED:
409               {
410                   NMLISTVIEW *nmlv = (NMLISTVIEW*)lParam;
411                   g_nmlistview = *nmlv;
412               }
413               break;
414           case LVN_GETDISPINFOA:
415               {
416                   NMLVDISPINFOA *dispinfo = (NMLVDISPINFOA*)lParam;
417                   g_itema = dispinfo->item;
418
419                   if (g_disp_A_to_W && (dispinfo->item.mask & LVIF_TEXT))
420                   {
421                       static const WCHAR testW[] = {'T','E','S','T',0};
422                       dispinfo->hdr.code = LVN_GETDISPINFOW;
423                       memcpy(dispinfo->item.pszText, testW, sizeof(testW));
424                   }
425
426                   /* test control buffer size for text, 10 used to mask cases when control
427                      is using caller buffer to process LVM_GETITEM for example */
428                   if (dispinfo->item.mask & LVIF_TEXT && dispinfo->item.cchTextMax > 10)
429                       ok(dispinfo->item.cchTextMax == 260 ||
430                          broken(dispinfo->item.cchTextMax == 264) /* NT4 reports aligned size */,
431                       "buffer size %d\n", dispinfo->item.cchTextMax);
432               }
433               break;
434           case NM_HOVER:
435               if (g_block_hover) return 1;
436               break;
437           }
438           break;
439       }
440       case WM_NOTIFYFORMAT:
441       {
442           /* force to return format */
443           if (lParam == NF_QUERY && notifyFormat != -1) return notifyFormat;
444           break;
445       }
446     }
447
448     defwndproc_counter++;
449     ret = DefWindowProcA(hwnd, message, wParam, lParam);
450     defwndproc_counter--;
451
452     return ret;
453 }
454
455 static BOOL register_parent_wnd_class(BOOL Unicode)
456 {
457     WNDCLASSA clsA;
458     WNDCLASSW clsW;
459
460     if (Unicode)
461     {
462         clsW.style = 0;
463         clsW.lpfnWndProc = parent_wnd_proc;
464         clsW.cbClsExtra = 0;
465         clsW.cbWndExtra = 0;
466         clsW.hInstance = GetModuleHandleW(NULL);
467         clsW.hIcon = 0;
468         clsW.hCursor = LoadCursorA(0, IDC_ARROW);
469         clsW.hbrBackground = GetStockObject(WHITE_BRUSH);
470         clsW.lpszMenuName = NULL;
471         clsW.lpszClassName = testparentclassW;
472     }
473     else
474     {
475         clsA.style = 0;
476         clsA.lpfnWndProc = parent_wnd_proc;
477         clsA.cbClsExtra = 0;
478         clsA.cbWndExtra = 0;
479         clsA.hInstance = GetModuleHandleA(NULL);
480         clsA.hIcon = 0;
481         clsA.hCursor = LoadCursorA(0, IDC_ARROW);
482         clsA.hbrBackground = GetStockObject(WHITE_BRUSH);
483         clsA.lpszMenuName = NULL;
484         clsA.lpszClassName = "Listview test parent class";
485     }
486
487     return Unicode ? RegisterClassW(&clsW) : RegisterClassA(&clsA);
488 }
489
490 static HWND create_parent_window(BOOL Unicode)
491 {
492     static const WCHAR nameW[] = {'t','e','s','t','p','a','r','e','n','t','n','a','m','e','W',0};
493     HWND hwnd;
494
495     if (!register_parent_wnd_class(Unicode))
496         return NULL;
497
498     blockEdit = FALSE;
499     notifyFormat = -1;
500
501     if (Unicode)
502         hwnd = CreateWindowExW(0, testparentclassW, nameW,
503                                WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
504                                WS_MAXIMIZEBOX | WS_VISIBLE,
505                                0, 0, 100, 100,
506                                GetDesktopWindow(), NULL, GetModuleHandleW(NULL), NULL);
507     else
508         hwnd = CreateWindowExA(0, "Listview test parent class",
509                                "Listview test parent window",
510                                WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
511                                WS_MAXIMIZEBOX | WS_VISIBLE,
512                                0, 0, 100, 100,
513                                GetDesktopWindow(), NULL, GetModuleHandleA(NULL), NULL);
514     SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE );
515     return hwnd;
516 }
517
518 static LRESULT WINAPI listview_subclass_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
519 {
520     WNDPROC oldproc = (WNDPROC)GetWindowLongPtrA(hwnd, GWLP_USERDATA);
521     static LONG defwndproc_counter = 0;
522     LRESULT ret;
523     struct message msg;
524
525     trace("listview: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
526
527     /* some debug output for style changing */
528     if ((message == WM_STYLECHANGING ||
529          message == WM_STYLECHANGED) && lParam)
530     {
531         STYLESTRUCT *style = (STYLESTRUCT*)lParam;
532         trace("\told style: 0x%08x, new style: 0x%08x\n", style->styleOld, style->styleNew);
533     }
534
535     msg.message = message;
536     msg.flags = sent|wparam|lparam;
537     if (defwndproc_counter) msg.flags |= defwinproc;
538     msg.wParam = wParam;
539     msg.lParam = lParam;
540     msg.id = LISTVIEW_ID;
541     add_message(sequences, LISTVIEW_SEQ_INDEX, &msg);
542     add_message(sequences, COMBINED_SEQ_INDEX, &msg);
543
544     defwndproc_counter++;
545     ret = CallWindowProcA(oldproc, hwnd, message, wParam, lParam);
546     defwndproc_counter--;
547     return ret;
548 }
549
550 static HWND create_listview_control(DWORD style)
551 {
552     WNDPROC oldproc;
553     HWND hwnd;
554     RECT rect;
555
556     GetClientRect(hwndparent, &rect);
557     hwnd = CreateWindowExA(0, WC_LISTVIEW, "foo",
558                            WS_CHILD | WS_BORDER | WS_VISIBLE | style,
559                            0, 0, rect.right, rect.bottom,
560                            hwndparent, NULL, GetModuleHandleA(NULL), NULL);
561     ok(hwnd != NULL, "gle=%d\n", GetLastError());
562
563     if (!hwnd) return NULL;
564
565     oldproc = (WNDPROC)SetWindowLongPtrA(hwnd, GWLP_WNDPROC,
566                                         (LONG_PTR)listview_subclass_proc);
567     SetWindowLongPtrA(hwnd, GWLP_USERDATA, (LONG_PTR)oldproc);
568
569     return hwnd;
570 }
571
572 /* unicode listview window with specified parent */
573 static HWND create_listview_controlW(DWORD style, HWND parent)
574 {
575     WNDPROC oldproc;
576     HWND hwnd;
577     RECT rect;
578     static const WCHAR nameW[] = {'f','o','o',0};
579
580     GetClientRect(parent, &rect);
581     hwnd = CreateWindowExW(0, WC_LISTVIEWW, nameW,
582                            WS_CHILD | WS_BORDER | WS_VISIBLE | style,
583                            0, 0, rect.right, rect.bottom,
584                            parent, NULL, GetModuleHandleW(NULL), NULL);
585     ok(hwnd != NULL, "gle=%d\n", GetLastError());
586
587     if (!hwnd) return NULL;
588
589     oldproc = (WNDPROC)SetWindowLongPtrW(hwnd, GWLP_WNDPROC,
590                                         (LONG_PTR)listview_subclass_proc);
591     SetWindowLongPtrW(hwnd, GWLP_USERDATA, (LONG_PTR)oldproc);
592
593     return hwnd;
594 }
595
596 static LRESULT WINAPI header_subclass_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
597 {
598     WNDPROC oldproc = (WNDPROC)GetWindowLongPtrA(hwnd, GWLP_USERDATA);
599     static LONG defwndproc_counter = 0;
600     LRESULT ret;
601     struct message msg;
602
603     trace("header: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
604
605     msg.message = message;
606     msg.flags = sent|wparam|lparam;
607     if (defwndproc_counter) msg.flags |= defwinproc;
608     msg.wParam = wParam;
609     msg.lParam = lParam;
610     msg.id = HEADER_ID;
611     add_message(sequences, LISTVIEW_SEQ_INDEX, &msg);
612
613     defwndproc_counter++;
614     ret = CallWindowProcA(oldproc, hwnd, message, wParam, lParam);
615     defwndproc_counter--;
616     return ret;
617 }
618
619 static HWND subclass_header(HWND hwndListview)
620 {
621     WNDPROC oldproc;
622     HWND hwnd;
623
624     hwnd = ListView_GetHeader(hwndListview);
625     oldproc = (WNDPROC)SetWindowLongPtrA(hwnd, GWLP_WNDPROC,
626                                          (LONG_PTR)header_subclass_proc);
627     SetWindowLongPtrA(hwnd, GWLP_USERDATA, (LONG_PTR)oldproc);
628
629     return hwnd;
630 }
631
632 static LRESULT WINAPI editbox_subclass_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
633 {
634     WNDPROC oldproc = (WNDPROC)GetWindowLongPtrA(hwnd, GWLP_USERDATA);
635     static LONG defwndproc_counter = 0;
636     LRESULT ret;
637     struct message msg;
638
639     msg.message = message;
640     msg.flags = sent|wparam|lparam;
641     if (defwndproc_counter) msg.flags |= defwinproc;
642     msg.wParam = wParam;
643     msg.lParam = lParam;
644     msg.id = 0;
645
646     /* all we need is sizing */
647     if (message == WM_WINDOWPOSCHANGING ||
648         message == WM_NCCALCSIZE ||
649         message == WM_WINDOWPOSCHANGED ||
650         message == WM_MOVE ||
651         message == WM_SIZE)
652     {
653         add_message(sequences, EDITBOX_SEQ_INDEX, &msg);
654     }
655
656     defwndproc_counter++;
657     ret = CallWindowProcA(oldproc, hwnd, message, wParam, lParam);
658     defwndproc_counter--;
659     return ret;
660 }
661
662 static HWND subclass_editbox(HWND hwndListview)
663 {
664     WNDPROC oldproc;
665     HWND hwnd;
666
667     hwnd = (HWND)SendMessage(hwndListview, LVM_GETEDITCONTROL, 0, 0);
668     oldproc = (WNDPROC)SetWindowLongPtrA(hwnd, GWLP_WNDPROC,
669                                          (LONG_PTR)editbox_subclass_proc);
670     SetWindowLongPtrA(hwnd, GWLP_USERDATA, (LONG_PTR)oldproc);
671
672     return hwnd;
673 }
674
675 /* Performs a single LVM_HITTEST test */
676 static void test_lvm_hittest_(HWND hwnd, INT x, INT y, INT item, UINT flags, UINT broken_flags,
677                               BOOL todo_item, BOOL todo_flags, int line)
678 {
679     LVHITTESTINFO lpht;
680     INT ret;
681
682     lpht.pt.x = x;
683     lpht.pt.y = y;
684     lpht.iSubItem = 10;
685
686     ret = SendMessage(hwnd, LVM_HITTEST, 0, (LPARAM)&lpht);
687
688     if (todo_item)
689     {
690         todo_wine
691         {
692             ok_(__FILE__, line)(ret == item, "Expected %d retval, got %d\n", item, ret);
693             ok_(__FILE__, line)(lpht.iItem == item, "Expected %d item, got %d\n", item, lpht.iItem);
694             ok_(__FILE__, line)(lpht.iSubItem == 10, "Expected subitem not overwrited\n");
695         }
696     }
697     else
698     {
699         ok_(__FILE__, line)(ret == item, "Expected %d retval, got %d\n", item, ret);
700         ok_(__FILE__, line)(lpht.iItem == item, "Expected %d item, got %d\n", item, lpht.iItem);
701         ok_(__FILE__, line)(lpht.iSubItem == 10, "Expected subitem not overwrited\n");
702     }
703
704     if (todo_flags)
705     {
706         todo_wine
707             ok_(__FILE__, line)(lpht.flags == flags, "Expected flags 0x%x, got 0x%x\n", flags, lpht.flags);
708     }
709     else if (broken_flags)
710         ok_(__FILE__, line)(lpht.flags == flags || broken(lpht.flags == broken_flags),
711                             "Expected flags %x, got %x\n", flags, lpht.flags);
712     else
713         ok_(__FILE__, line)(lpht.flags == flags, "Expected flags 0x%x, got 0x%x\n", flags, lpht.flags);
714 }
715
716 #define test_lvm_hittest(a,b,c,d,e,f,g,h) test_lvm_hittest_(a,b,c,d,e,f,g,h,__LINE__)
717
718 /* Performs a single LVM_SUBITEMHITTEST test */
719 static void test_lvm_subitemhittest_(HWND hwnd, INT x, INT y, INT item, INT subitem, UINT flags,
720                                      BOOL todo_item, BOOL todo_subitem, BOOL todo_flags, int line)
721 {
722     LVHITTESTINFO lpht;
723     INT ret;
724
725     lpht.pt.x = x;
726     lpht.pt.y = y;
727
728     ret = SendMessage(hwnd, LVM_SUBITEMHITTEST, 0, (LPARAM)&lpht);
729
730     if (todo_item)
731     {
732         todo_wine
733         {
734             ok_(__FILE__, line)(ret == item, "Expected %d retval, got %d\n", item, ret);
735             ok_(__FILE__, line)(lpht.iItem == item, "Expected %d item, got %d\n", item, lpht.iItem);
736         }
737     }
738     else
739     {
740         ok_(__FILE__, line)(ret == item, "Expected %d retval, got %d\n", item, ret);
741         ok_(__FILE__, line)(lpht.iItem == item, "Expected %d item, got %d\n", item, lpht.iItem);
742     }
743
744     if (todo_subitem)
745     {
746         todo_wine
747             ok_(__FILE__, line)(lpht.iSubItem == subitem, "Expected subitem %d, got %d\n", subitem, lpht.iSubItem);
748     }
749     else
750         ok_(__FILE__, line)(lpht.iSubItem == subitem, "Expected subitem %d, got %d\n", subitem, lpht.iSubItem);
751
752     if (todo_flags)
753     {
754         todo_wine
755             ok_(__FILE__, line)(lpht.flags == flags, "Expected flags 0x%x, got 0x%x\n", flags, lpht.flags);
756     }
757     else
758         ok_(__FILE__, line)(lpht.flags == flags, "Expected flags 0x%x, got 0x%x\n", flags, lpht.flags);
759 }
760
761 #define test_lvm_subitemhittest(a,b,c,d,e,f,g,h,i) test_lvm_subitemhittest_(a,b,c,d,e,f,g,h,i,__LINE__)
762
763 static void test_images(void)
764 {
765     HWND hwnd;
766     INT r;
767     LVITEM item;
768     HIMAGELIST himl;
769     HBITMAP hbmp;
770     RECT r1, r2;
771     static CHAR hello[] = "hello";
772
773     himl = ImageList_Create(40, 40, 0, 4, 4);
774     ok(himl != NULL, "failed to create imagelist\n");
775
776     hbmp = CreateBitmap(40, 40, 1, 1, NULL);
777     ok(hbmp != NULL, "failed to create bitmap\n");
778
779     r = ImageList_Add(himl, hbmp, 0);
780     ok(r == 0, "should be zero\n");
781
782     hwnd = CreateWindowEx(0, "SysListView32", "foo", LVS_OWNERDRAWFIXED, 
783                 10, 10, 100, 200, hwndparent, NULL, NULL, NULL);
784     ok(hwnd != NULL, "failed to create listview window\n");
785
786     r = SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, 0,
787                     LVS_EX_UNDERLINEHOT | LVS_EX_FLATSB | LVS_EX_ONECLICKACTIVATE);
788
789     ok(r == 0, "should return zero\n");
790
791     r = SendMessage(hwnd, LVM_SETIMAGELIST, 0, (LPARAM)himl);
792     ok(r == 0, "should return zero\n");
793
794     r = SendMessage(hwnd, LVM_SETICONSPACING, 0, MAKELONG(100,50));
795     ok(r != 0, "got 0\n");
796
797     /* returns dimensions */
798
799     r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
800     ok(r == 0, "should be zero items\n");
801
802     item.mask = LVIF_IMAGE | LVIF_TEXT;
803     item.iItem = 0;
804     item.iSubItem = 1;
805     item.iImage = 0;
806     item.pszText = 0;
807     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
808     ok(r == -1, "should fail\n");
809
810     item.iSubItem = 0;
811     item.pszText = hello;
812     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
813     ok(r == 0, "should not fail\n");
814
815     memset(&r1, 0, sizeof r1);
816     r1.left = LVIR_ICON;
817     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM) &r1);
818     expect(1, r);
819
820     r = SendMessage(hwnd, LVM_DELETEALLITEMS, 0, 0);
821     ok(r == TRUE, "should not fail\n");
822
823     item.iSubItem = 0;
824     item.pszText = hello;
825     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
826     ok(r == 0, "should not fail\n");
827
828     memset(&r2, 0, sizeof r2);
829     r2.left = LVIR_ICON;
830     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM) &r2);
831     expect(1, r);
832
833     ok(!memcmp(&r1, &r2, sizeof r1), "rectangle should be the same\n");
834
835     DestroyWindow(hwnd);
836 }
837
838 static void test_checkboxes(void)
839 {
840     HWND hwnd;
841     LVITEMA item;
842     DWORD r;
843     static CHAR text[]  = "Text",
844                 text2[] = "Text2",
845                 text3[] = "Text3";
846
847     hwnd = CreateWindowEx(0, "SysListView32", "foo", LVS_REPORT, 
848                 10, 10, 100, 200, hwndparent, NULL, NULL, NULL);
849     ok(hwnd != NULL, "failed to create listview window\n");
850
851     /* first without LVS_EX_CHECKBOXES set and an item and check that state is preserved */
852     item.mask = LVIF_TEXT | LVIF_STATE;
853     item.stateMask = 0xffff;
854     item.state = 0xfccc;
855     item.iItem = 0;
856     item.iSubItem = 0;
857     item.pszText = text;
858     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
859     expect(0, r);
860
861     item.iItem = 0;
862     item.mask = LVIF_STATE;
863     item.stateMask = 0xffff;
864     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
865     expect(1, r);
866     ok(item.state == 0xfccc, "state %x\n", item.state);
867
868     /* Don't set LVIF_STATE */
869     item.mask = LVIF_TEXT;
870     item.stateMask = 0xffff;
871     item.state = 0xfccc;
872     item.iItem = 1;
873     item.iSubItem = 0;
874     item.pszText = text;
875     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
876     expect(1, r);
877
878     item.iItem = 1;
879     item.mask = LVIF_STATE;
880     item.stateMask = 0xffff;
881     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
882     expect(1, r);
883     ok(item.state == 0, "state %x\n", item.state);
884
885     r = SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_CHECKBOXES, LVS_EX_CHECKBOXES);
886     expect(0, r);
887
888     /* Having turned on checkboxes, check that all existing items are set to 0x1000 (unchecked) */
889     item.iItem = 0;
890     item.mask = LVIF_STATE;
891     item.stateMask = 0xffff;
892     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
893     expect(1, r);
894     if (item.state != 0x1ccc)
895     {
896         win_skip("LVS_EX_CHECKBOXES style is unavailable. Skipping.\n");
897         DestroyWindow(hwnd);
898         return;
899     }
900
901     /* Now add an item without specifying a state and check that its state goes to 0x1000 */
902     item.iItem = 2;
903     item.mask = LVIF_TEXT;
904     item.state = 0;
905     item.pszText = text2;
906     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
907     expect(2, r);
908
909     item.iItem = 2;
910     item.mask = LVIF_STATE;
911     item.stateMask = 0xffff;
912     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
913     expect(1, r);
914     ok(item.state == 0x1000, "state %x\n", item.state);
915
916     /* Add a further item this time specifying a state and still its state goes to 0x1000 */
917     item.iItem = 3;
918     item.mask = LVIF_TEXT | LVIF_STATE;
919     item.stateMask = 0xffff;
920     item.state = 0x2aaa;
921     item.pszText = text3;
922     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
923     expect(3, r);
924
925     item.iItem = 3;
926     item.mask = LVIF_STATE;
927     item.stateMask = 0xffff;
928     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
929     expect(1, r);
930     ok(item.state == 0x1aaa, "state %x\n", item.state);
931
932     /* Set an item's state to checked */
933     item.iItem = 3;
934     item.mask = LVIF_STATE;
935     item.stateMask = 0xf000;
936     item.state = 0x2000;
937     r = SendMessage(hwnd, LVM_SETITEMA, 0, (LPARAM) &item);
938     expect(1, r);
939
940     item.iItem = 3;
941     item.mask = LVIF_STATE;
942     item.stateMask = 0xffff;
943     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
944     expect(1, r);
945     ok(item.state == 0x2aaa, "state %x\n", item.state);
946
947     /* Check that only the bits we asked for are returned,
948      * and that all the others are set to zero
949      */
950     item.iItem = 3;
951     item.mask = LVIF_STATE;
952     item.stateMask = 0xf000;
953     item.state = 0xffff;
954     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
955     expect(1, r);
956     ok(item.state == 0x2000, "state %x\n", item.state);
957
958     /* Set the style again and check that doesn't change an item's state */
959     r = SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_CHECKBOXES, LVS_EX_CHECKBOXES);
960     ok(r == LVS_EX_CHECKBOXES, "ret %x\n", r);
961
962     item.iItem = 3;
963     item.mask = LVIF_STATE;
964     item.stateMask = 0xffff;
965     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
966     expect(1, r);
967     ok(item.state == 0x2aaa, "state %x\n", item.state);
968
969     /* Unsetting the checkbox extended style doesn't change an item's state */
970     r = SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_CHECKBOXES, 0);
971     ok(r == LVS_EX_CHECKBOXES, "ret %x\n", r);
972
973     item.iItem = 3;
974     item.mask = LVIF_STATE;
975     item.stateMask = 0xffff;
976     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
977     expect(1, r);
978     ok(item.state == 0x2aaa, "state %x\n", item.state);
979
980     /* Now setting the style again will change an item's state */
981     r = SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_CHECKBOXES, LVS_EX_CHECKBOXES);
982     expect(0, r);
983
984     item.iItem = 3;
985     item.mask = LVIF_STATE;
986     item.stateMask = 0xffff;
987     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
988     expect(1, r);
989     ok(item.state == 0x1aaa, "state %x\n", item.state);
990
991     /* Toggle checkbox tests (bug 9934) */
992     memset (&item, 0xcc, sizeof(item));
993     item.mask = LVIF_STATE;
994     item.iItem = 3;
995     item.iSubItem = 0;
996     item.state = LVIS_FOCUSED;
997     item.stateMask = LVIS_FOCUSED;
998     r = SendMessage(hwnd, LVM_SETITEM, 0, (LPARAM) &item);
999     expect(1, r);
1000
1001     item.iItem = 3;
1002     item.mask = LVIF_STATE;
1003     item.stateMask = 0xffff;
1004     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
1005     expect(1, r);
1006     ok(item.state == 0x1aab, "state %x\n", item.state);
1007
1008     r = SendMessage(hwnd, WM_KEYDOWN, VK_SPACE, 0);
1009     expect(0, r);
1010     r = SendMessage(hwnd, WM_KEYUP, VK_SPACE, 0);
1011     expect(0, r);
1012
1013     item.iItem = 3;
1014     item.mask = LVIF_STATE;
1015     item.stateMask = 0xffff;
1016     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
1017     expect(1, r);
1018     ok(item.state == 0x2aab, "state %x\n", item.state);
1019
1020     r = SendMessage(hwnd, WM_KEYDOWN, VK_SPACE, 0);
1021     expect(0, r);
1022     r = SendMessage(hwnd, WM_KEYUP, VK_SPACE, 0);
1023     expect(0, r);
1024
1025     item.iItem = 3;
1026     item.mask = LVIF_STATE;
1027     item.stateMask = 0xffff;
1028     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
1029     expect(1, r);
1030     ok(item.state == 0x1aab, "state %x\n", item.state);
1031
1032     DestroyWindow(hwnd);
1033 }
1034
1035 static void insert_column(HWND hwnd, int idx)
1036 {
1037     LVCOLUMN column;
1038     INT rc;
1039
1040     memset(&column, 0xcc, sizeof(column));
1041     column.mask = LVCF_SUBITEM;
1042     column.iSubItem = idx;
1043
1044     rc = ListView_InsertColumn(hwnd, idx, &column);
1045     expect(idx, rc);
1046 }
1047
1048 static void insert_item(HWND hwnd, int idx)
1049 {
1050     static CHAR text[] = "foo";
1051
1052     LVITEMA item;
1053     INT rc;
1054
1055     memset(&item, 0xcc, sizeof (item));
1056     item.mask = LVIF_TEXT;
1057     item.iItem = idx;
1058     item.iSubItem = 0;
1059     item.pszText = text;
1060
1061     rc = ListView_InsertItem(hwnd, &item);
1062     expect(idx, rc);
1063 }
1064
1065 static void test_items(void)
1066 {
1067     const LPARAM lparamTest = 0x42;
1068     static CHAR text[] = "Text";
1069     char buffA[5];
1070     HWND hwnd;
1071     LVITEMA item;
1072     DWORD r;
1073
1074     hwnd = CreateWindowEx(0, "SysListView32", "foo", LVS_REPORT,
1075                 10, 10, 100, 200, hwndparent, NULL, NULL, NULL);
1076     ok(hwnd != NULL, "failed to create listview window\n");
1077
1078     /*
1079      * Test setting/getting item params
1080      */
1081
1082     /* Set up two columns */
1083     insert_column(hwnd, 0);
1084     insert_column(hwnd, 1);
1085
1086     /* LVIS_SELECTED with zero stateMask */
1087     /* set */
1088     memset (&item, 0, sizeof (item));
1089     item.mask = LVIF_STATE;
1090     item.state = LVIS_SELECTED;
1091     item.stateMask = 0;
1092     item.iItem = 0;
1093     item.iSubItem = 0;
1094     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
1095     expect(0, r);
1096     /* get */
1097     memset (&item, 0xcc, sizeof (item));
1098     item.mask = LVIF_STATE;
1099     item.stateMask = LVIS_SELECTED;
1100     item.state = 0;
1101     item.iItem = 0;
1102     item.iSubItem = 0;
1103     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
1104     expect(1, r);
1105     ok(item.state & LVIS_SELECTED, "Expected LVIS_SELECTED\n");
1106     SendMessage(hwnd, LVM_DELETEITEM, 0, 0);
1107
1108     /* LVIS_SELECTED with zero stateMask */
1109     /* set */
1110     memset (&item, 0, sizeof (item));
1111     item.mask = LVIF_STATE;
1112     item.state = LVIS_FOCUSED;
1113     item.stateMask = 0;
1114     item.iItem = 0;
1115     item.iSubItem = 0;
1116     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
1117     expect(0, r);
1118     /* get */
1119     memset (&item, 0xcc, sizeof (item));
1120     item.mask = LVIF_STATE;
1121     item.stateMask = LVIS_FOCUSED;
1122     item.state = 0;
1123     item.iItem = 0;
1124     item.iSubItem = 0;
1125     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
1126     expect(1, r);
1127     ok(item.state & LVIS_FOCUSED, "Expected LVIS_FOCUSED\n");
1128     SendMessage(hwnd, LVM_DELETEITEM, 0, 0);
1129
1130     /* LVIS_CUT with LVIS_FOCUSED stateMask */
1131     /* set */
1132     memset (&item, 0, sizeof (item));
1133     item.mask = LVIF_STATE;
1134     item.state = LVIS_CUT;
1135     item.stateMask = LVIS_FOCUSED;
1136     item.iItem = 0;
1137     item.iSubItem = 0;
1138     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
1139     expect(0, r);
1140     /* get */
1141     memset (&item, 0xcc, sizeof (item));
1142     item.mask = LVIF_STATE;
1143     item.stateMask = LVIS_CUT;
1144     item.state = 0;
1145     item.iItem = 0;
1146     item.iSubItem = 0;
1147     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
1148     expect(1, r);
1149     ok(item.state & LVIS_CUT, "Expected LVIS_CUT\n");
1150     SendMessage(hwnd, LVM_DELETEITEM, 0, 0);
1151
1152     /* Insert an item with just a param */
1153     memset (&item, 0xcc, sizeof (item));
1154     item.mask = LVIF_PARAM;
1155     item.iItem = 0;
1156     item.iSubItem = 0;
1157     item.lParam = lparamTest;
1158     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
1159     expect(0, r);
1160
1161     /* Test getting of the param */
1162     memset (&item, 0xcc, sizeof (item));
1163     item.mask = LVIF_PARAM;
1164     item.iItem = 0;
1165     item.iSubItem = 0;
1166     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
1167     expect(1, r);
1168     ok(item.lParam == lparamTest, "got lParam %lx, expected %lx\n", item.lParam, lparamTest);
1169
1170     /* Set up a subitem */
1171     memset (&item, 0xcc, sizeof (item));
1172     item.mask = LVIF_TEXT;
1173     item.iItem = 0;
1174     item.iSubItem = 1;
1175     item.pszText = text;
1176     r = SendMessage(hwnd, LVM_SETITEMA, 0, (LPARAM) &item);
1177     expect(1, r);
1178
1179     item.mask = LVIF_TEXT;
1180     item.iItem = 0;
1181     item.iSubItem = 1;
1182     item.pszText = buffA;
1183     item.cchTextMax = sizeof(buffA);
1184     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
1185     expect(1, r);
1186     ok(!memcmp(item.pszText, text, sizeof(text)), "got text %s, expected %s\n", item.pszText, text);
1187
1188     /* set up with extra flag */
1189     /* 1. reset subitem text */
1190     item.mask = LVIF_TEXT;
1191     item.iItem = 0;
1192     item.iSubItem = 1;
1193     item.pszText = NULL;
1194     r = SendMessage(hwnd, LVM_SETITEMA, 0, (LPARAM) &item);
1195     expect(1, r);
1196
1197     item.mask = LVIF_TEXT;
1198     item.iItem = 0;
1199     item.iSubItem = 1;
1200     item.pszText = buffA;
1201     buffA[0] = 'a';
1202     item.cchTextMax = sizeof(buffA);
1203     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
1204     expect(1, r);
1205     ok(item.pszText[0] == 0, "got %p\n", item.pszText);
1206
1207     /* 2. set new text with extra flag specified */
1208     item.mask = LVIF_TEXT | LVIF_DI_SETITEM;
1209     item.iItem = 0;
1210     item.iSubItem = 1;
1211     item.pszText = text;
1212     r = SendMessage(hwnd, LVM_SETITEMA, 0, (LPARAM) &item);
1213     ok(r == 1 || broken(r == 0) /* NT4 */, "ret %d\n", r);
1214
1215     if (r == 1)
1216     {
1217         item.mask = LVIF_TEXT;
1218         item.iItem = 0;
1219         item.iSubItem = 1;
1220         item.pszText = buffA;
1221         buffA[0] = 'a';
1222         item.cchTextMax = sizeof(buffA);
1223         r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
1224         expect(1, r);
1225         ok(!memcmp(item.pszText, text, sizeof(text)), "got %s, expected %s\n", item.pszText, text);
1226     }
1227
1228     /* Query param from subitem: returns main item param */
1229     memset (&item, 0xcc, sizeof (item));
1230     item.mask = LVIF_PARAM;
1231     item.iItem = 0;
1232     item.iSubItem = 1;
1233     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
1234     expect(1, r);
1235     ok(item.lParam == lparamTest, "got lParam %lx, expected %lx\n", item.lParam, lparamTest);
1236
1237     /* Set up param on first subitem: no effect */
1238     memset (&item, 0xcc, sizeof (item));
1239     item.mask = LVIF_PARAM;
1240     item.iItem = 0;
1241     item.iSubItem = 1;
1242     item.lParam = lparamTest+1;
1243     r = SendMessage(hwnd, LVM_SETITEMA, 0, (LPARAM) &item);
1244     expect(0, r);
1245
1246     /* Query param from subitem again: should still return main item param */
1247     memset (&item, 0xcc, sizeof (item));
1248     item.mask = LVIF_PARAM;
1249     item.iItem = 0;
1250     item.iSubItem = 1;
1251     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
1252     expect(1, r);
1253     ok(item.lParam == lparamTest, "got lParam %lx, expected %lx\n", item.lParam, lparamTest);
1254
1255     /**** Some tests of state highlighting ****/
1256     memset (&item, 0xcc, sizeof (item));
1257     item.mask = LVIF_STATE;
1258     item.iItem = 0;
1259     item.iSubItem = 0;
1260     item.state = LVIS_SELECTED;
1261     item.stateMask = LVIS_SELECTED | LVIS_DROPHILITED;
1262     r = SendMessage(hwnd, LVM_SETITEM, 0, (LPARAM) &item);
1263     expect(1, r);
1264     item.iSubItem = 1;
1265     item.state = LVIS_DROPHILITED;
1266     r = SendMessage(hwnd, LVM_SETITEM, 0, (LPARAM) &item);
1267     expect(1, r);
1268
1269     memset (&item, 0xcc, sizeof (item));
1270     item.mask = LVIF_STATE;
1271     item.iItem = 0;
1272     item.iSubItem = 0;
1273     item.stateMask = -1;
1274     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
1275     expect(1, r);
1276     ok(item.state == LVIS_SELECTED, "got state %x, expected %x\n", item.state, LVIS_SELECTED);
1277     item.iSubItem = 1;
1278     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
1279     expect(1, r);
1280     todo_wine ok(item.state == LVIS_DROPHILITED, "got state %x, expected %x\n", item.state, LVIS_DROPHILITED);
1281
1282     /* some notnull but meaningless masks */
1283     memset (&item, 0, sizeof(item));
1284     item.mask = LVIF_NORECOMPUTE;
1285     item.iItem = 0;
1286     item.iSubItem = 0;
1287     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
1288     expect(1, r);
1289     memset (&item, 0, sizeof(item));
1290     item.mask = LVIF_DI_SETITEM;
1291     item.iItem = 0;
1292     item.iSubItem = 0;
1293     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
1294     expect(1, r);
1295
1296     /* set text to callback value already having it */
1297     r = SendMessage(hwnd, LVM_DELETEALLITEMS, 0, 0);
1298     expect(TRUE, r);
1299     memset (&item, 0, sizeof (item));
1300     item.mask  = LVIF_TEXT;
1301     item.pszText = LPSTR_TEXTCALLBACK;
1302     item.iItem = 0;
1303     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
1304     expect(0, r);
1305     memset (&item, 0, sizeof (item));
1306
1307     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1308
1309     item.pszText = LPSTR_TEXTCALLBACK;
1310     r = SendMessage(hwnd, LVM_SETITEMTEXT, 0 , (LPARAM) &item);
1311     expect(TRUE, r);
1312
1313     ok_sequence(sequences, PARENT_SEQ_INDEX, textcallback_set_again_parent_seq,
1314                 "check callback text comparison rule", FALSE);
1315
1316     DestroyWindow(hwnd);
1317 }
1318
1319 static void test_columns(void)
1320 {
1321     HWND hwnd;
1322     LVCOLUMNA column;
1323     LVITEMA item;
1324     INT order[2];
1325     CHAR buff[5];
1326     DWORD rc;
1327
1328     hwnd = CreateWindowExA(0, "SysListView32", "foo", LVS_REPORT,
1329                 10, 10, 100, 200, hwndparent, NULL, NULL, NULL);
1330     ok(hwnd != NULL, "failed to create listview window\n");
1331
1332     /* Add a column with no mask */
1333     memset(&column, 0xcc, sizeof(column));
1334     column.mask = 0;
1335     rc = SendMessageA(hwnd, LVM_INSERTCOLUMNA, 0, (LPARAM)&column);
1336     ok(rc == 0, "Inserting column with no mask failed with %d\n", rc);
1337
1338     /* Check its width */
1339     rc = SendMessageA(hwnd, LVM_GETCOLUMNWIDTH, 0, 0);
1340     ok(rc == 10, "Inserting column with no mask failed to set width to 10 with %d\n", rc);
1341
1342     DestroyWindow(hwnd);
1343
1344     /* LVM_GETCOLUMNORDERARRAY */
1345     hwnd = create_listview_control(LVS_REPORT);
1346     subclass_header(hwnd);
1347
1348     memset(&column, 0, sizeof(column));
1349     column.mask = LVCF_WIDTH;
1350     column.cx = 100;
1351     rc = SendMessageA(hwnd, LVM_INSERTCOLUMNA, 0, (LPARAM)&column);
1352     expect(0, rc);
1353
1354     column.cx = 200;
1355     rc = SendMessageA(hwnd, LVM_INSERTCOLUMNA, 1, (LPARAM)&column);
1356     expect(1, rc);
1357
1358     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1359
1360     rc = SendMessageA(hwnd, LVM_GETCOLUMNORDERARRAY, 2, (LPARAM)&order);
1361     expect(1, rc);
1362     ok(order[0] == 0, "Expected order 0, got %d\n", order[0]);
1363     ok(order[1] == 1, "Expected order 1, got %d\n", order[1]);
1364
1365     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_getorderarray_seq, "get order array", FALSE);
1366
1367     /* after column added subitem is considered as present */
1368     insert_item(hwnd, 0);
1369
1370     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1371
1372     item.pszText = buff;
1373     item.cchTextMax = sizeof(buff);
1374     item.iItem = 0;
1375     item.iSubItem = 1;
1376     item.mask = LVIF_TEXT;
1377     memset(&g_itema, 0, sizeof(g_itema));
1378     rc = SendMessageA(hwnd, LVM_GETITEMA, 0, (LPARAM)&item);
1379     expect(1, rc);
1380     ok(g_itema.iSubItem == 1, "got %d\n", g_itema.iSubItem);
1381
1382     ok_sequence(sequences, PARENT_SEQ_INDEX, single_getdispinfo_parent_seq,
1383         "get subitem text after column added", FALSE);
1384
1385     DestroyWindow(hwnd);
1386 }
1387
1388 /* test setting imagelist between WM_NCCREATE and WM_CREATE */
1389 static WNDPROC listviewWndProc;
1390 static HIMAGELIST test_create_imagelist;
1391
1392 static LRESULT CALLBACK create_test_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1393 {
1394     LRESULT ret;
1395
1396     if (uMsg == WM_CREATE)
1397     {
1398         LPCREATESTRUCT lpcs = (LPCREATESTRUCT)lParam;
1399         lpcs->style |= LVS_REPORT;
1400     }
1401     ret = CallWindowProc(listviewWndProc, hwnd, uMsg, wParam, lParam);
1402     if (uMsg == WM_CREATE) SendMessage(hwnd, LVM_SETIMAGELIST, 0, (LPARAM)test_create_imagelist);
1403     return ret;
1404 }
1405
1406 static void test_create(void)
1407 {
1408     HWND hList;
1409     HWND hHeader;
1410     LONG_PTR ret;
1411     LONG r;
1412     LVCOLUMNA col;
1413     RECT rect;
1414     WNDCLASSEX cls;
1415     DWORD style;
1416
1417     cls.cbSize = sizeof(WNDCLASSEX);
1418     ok(GetClassInfoEx(GetModuleHandle(NULL), "SysListView32", &cls), "GetClassInfoEx failed\n");
1419     listviewWndProc = cls.lpfnWndProc;
1420     cls.lpfnWndProc = create_test_wndproc;
1421     cls.lpszClassName = "MyListView32";
1422     ok(RegisterClassEx(&cls), "RegisterClassEx failed\n");
1423
1424     test_create_imagelist = ImageList_Create(16, 16, 0, 5, 10);
1425     hList = CreateWindow("MyListView32", "Test", WS_VISIBLE, 0, 0, 100, 100, NULL, NULL, GetModuleHandle(NULL), 0);
1426     ok((HIMAGELIST)SendMessage(hList, LVM_GETIMAGELIST, 0, 0) == test_create_imagelist, "Image list not obtained\n");
1427     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1428
1429     if (!IsWindow(hHeader))
1430     {
1431         /* version 4.0 */
1432         win_skip("LVM_GETHEADER not implemented. Skipping.\n");
1433         DestroyWindow(hList);
1434         return;
1435     }
1436
1437     ok(IsWindow(hHeader) && IsWindowVisible(hHeader), "Listview not in report mode\n");
1438     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
1439     DestroyWindow(hList);
1440
1441     /* header isn't created on LVS_ICON and LVS_LIST styles */
1442     hList = CreateWindow("SysListView32", "Test", WS_VISIBLE, 0, 0, 100, 100, NULL, NULL,
1443                           GetModuleHandle(NULL), 0);
1444     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1445     ok(!IsWindow(hHeader), "Header shouldn't be created\n");
1446     ok(NULL == GetDlgItem(hList, 0), "NULL dialog item expected\n");
1447     /* insert column */
1448     memset(&col, 0, sizeof(LVCOLUMNA));
1449     col.mask = LVCF_WIDTH;
1450     col.cx = 100;
1451     r = SendMessage(hList, LVM_INSERTCOLUMN, 0, (LPARAM)&col);
1452     expect(0, r);
1453     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1454     ok(IsWindow(hHeader), "Header should be created\n");
1455     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
1456     style = GetWindowLong(hHeader, GWL_STYLE);
1457     ok(!(style & HDS_HIDDEN), "Not expected HDS_HIDDEN\n");
1458     DestroyWindow(hList);
1459
1460     hList = CreateWindow("SysListView32", "Test", WS_VISIBLE|LVS_LIST, 0, 0, 100, 100, NULL, NULL,
1461                           GetModuleHandle(NULL), 0);
1462     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1463     ok(!IsWindow(hHeader), "Header shouldn't be created\n");
1464     ok(NULL == GetDlgItem(hList, 0), "NULL dialog item expected\n");
1465     /* insert column */
1466     memset(&col, 0, sizeof(LVCOLUMNA));
1467     col.mask = LVCF_WIDTH;
1468     col.cx = 100;
1469     r = SendMessage(hList, LVM_INSERTCOLUMN, 0, (LPARAM)&col);
1470     expect(0, r);
1471     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1472     ok(IsWindow(hHeader), "Header should be created\n");
1473     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
1474     DestroyWindow(hList);
1475
1476     /* try to switch LVS_ICON -> LVS_REPORT and back LVS_ICON -> LVS_REPORT */
1477     hList = CreateWindow("SysListView32", "Test", WS_VISIBLE, 0, 0, 100, 100, NULL, NULL,
1478                           GetModuleHandle(NULL), 0);
1479     ret = SetWindowLongPtr(hList, GWL_STYLE, GetWindowLongPtr(hList, GWL_STYLE) | LVS_REPORT);
1480     ok(ret & WS_VISIBLE, "Style wrong, should have WS_VISIBLE\n");
1481     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1482     ok(IsWindow(hHeader), "Header should be created\n");
1483     ret = SetWindowLongPtr(hList, GWL_STYLE, GetWindowLong(hList, GWL_STYLE) & ~LVS_REPORT);
1484     ok((ret & WS_VISIBLE) && (ret & LVS_REPORT), "Style wrong, should have WS_VISIBLE|LVS_REPORT\n");
1485     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1486     ok(IsWindow(hHeader), "Header should be created\n");
1487     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
1488     DestroyWindow(hList);
1489
1490     /* try to switch LVS_LIST -> LVS_REPORT and back LVS_LIST -> LVS_REPORT */
1491     hList = CreateWindow("SysListView32", "Test", WS_VISIBLE|LVS_LIST, 0, 0, 100, 100, NULL, NULL,
1492                           GetModuleHandle(NULL), 0);
1493     ret = SetWindowLongPtr(hList, GWL_STYLE,
1494                           (GetWindowLongPtr(hList, GWL_STYLE) & ~LVS_LIST) | LVS_REPORT);
1495     ok(((ret & WS_VISIBLE) && (ret & LVS_LIST)), "Style wrong, should have WS_VISIBLE|LVS_LIST\n");
1496     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1497     ok(IsWindow(hHeader), "Header should be created\n");
1498     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
1499     ret = SetWindowLongPtr(hList, GWL_STYLE,
1500                           (GetWindowLongPtr(hList, GWL_STYLE) & ~LVS_REPORT) | LVS_LIST);
1501     ok(((ret & WS_VISIBLE) && (ret & LVS_REPORT)), "Style wrong, should have WS_VISIBLE|LVS_REPORT\n");
1502     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1503     ok(IsWindow(hHeader), "Header should be created\n");
1504     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
1505     DestroyWindow(hList);
1506
1507     /* LVS_REPORT without WS_VISIBLE */
1508     hList = CreateWindow("SysListView32", "Test", LVS_REPORT, 0, 0, 100, 100, NULL, NULL,
1509                           GetModuleHandle(NULL), 0);
1510     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1511     ok(!IsWindow(hHeader), "Header shouldn't be created\n");
1512     ok(NULL == GetDlgItem(hList, 0), "NULL dialog item expected\n");
1513     /* insert column */
1514     memset(&col, 0, sizeof(LVCOLUMNA));
1515     col.mask = LVCF_WIDTH;
1516     col.cx = 100;
1517     r = SendMessage(hList, LVM_INSERTCOLUMN, 0, (LPARAM)&col);
1518     expect(0, r);
1519     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1520     ok(IsWindow(hHeader), "Header should be created\n");
1521     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
1522     DestroyWindow(hList);
1523
1524     /* LVS_REPORT without WS_VISIBLE, try to show it */
1525     hList = CreateWindow("SysListView32", "Test", LVS_REPORT, 0, 0, 100, 100, NULL, NULL,
1526                           GetModuleHandle(NULL), 0);
1527     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1528     ok(!IsWindow(hHeader), "Header shouldn't be created\n");
1529     ok(NULL == GetDlgItem(hList, 0), "NULL dialog item expected\n");
1530     ShowWindow(hList, SW_SHOW);
1531     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1532     ok(IsWindow(hHeader), "Header should be created\n");
1533     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
1534     DestroyWindow(hList);
1535
1536     /* LVS_REPORT with LVS_NOCOLUMNHEADER */
1537     hList = CreateWindow("SysListView32", "Test", LVS_REPORT|LVS_NOCOLUMNHEADER|WS_VISIBLE,
1538                           0, 0, 100, 100, NULL, NULL, GetModuleHandle(NULL), 0);
1539     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1540     ok(IsWindow(hHeader), "Header should be created\n");
1541     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
1542     /* HDS_DRAGDROP set by default */
1543     ok(GetWindowLongPtr(hHeader, GWL_STYLE) & HDS_DRAGDROP, "Expected header to have HDS_DRAGDROP\n");
1544     DestroyWindow(hList);
1545
1546     /* setting LVS_EX_HEADERDRAGDROP creates header */
1547     hList = CreateWindow("SysListView32", "Test", LVS_REPORT, 0, 0, 100, 100, NULL, NULL,
1548                           GetModuleHandle(NULL), 0);
1549     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1550     ok(!IsWindow(hHeader), "Header shouldn't be created\n");
1551     ok(NULL == GetDlgItem(hList, 0), "NULL dialog item expected\n");
1552     SendMessage(hList, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_HEADERDRAGDROP);
1553     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1554     ok(IsWindow(hHeader) ||
1555        broken(!IsWindow(hHeader)), /* 4.7x common controls */
1556        "Header should be created\n");
1557     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
1558     DestroyWindow(hList);
1559
1560     /* setting LVS_EX_GRIDLINES creates header */
1561     hList = CreateWindow("SysListView32", "Test", LVS_REPORT, 0, 0, 100, 100, NULL, NULL,
1562                           GetModuleHandle(NULL), 0);
1563     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1564     ok(!IsWindow(hHeader), "Header shouldn't be created\n");
1565     ok(NULL == GetDlgItem(hList, 0), "NULL dialog item expected\n");
1566     SendMessage(hList, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_GRIDLINES);
1567     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1568     ok(IsWindow(hHeader) ||
1569        broken(!IsWindow(hHeader)), /* 4.7x common controls */
1570        "Header should be created\n");
1571     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
1572     DestroyWindow(hList);
1573
1574     /* setting LVS_EX_FULLROWSELECT creates header */
1575     hList = CreateWindow("SysListView32", "Test", LVS_REPORT, 0, 0, 100, 100, NULL, NULL,
1576                           GetModuleHandle(NULL), 0);
1577     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1578     ok(!IsWindow(hHeader), "Header shouldn't be created\n");
1579     ok(NULL == GetDlgItem(hList, 0), "NULL dialog item expected\n");
1580     SendMessage(hList, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);
1581     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1582     ok(IsWindow(hHeader) ||
1583        broken(!IsWindow(hHeader)), /* 4.7x common controls */
1584        "Header should be created\n");
1585     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
1586     DestroyWindow(hList);
1587
1588     /* not report style accepts LVS_EX_HEADERDRAGDROP too */
1589     hList = create_listview_control(LVS_ICON);
1590     SendMessage(hList, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_HEADERDRAGDROP);
1591     r = SendMessage(hList, LVM_GETEXTENDEDLISTVIEWSTYLE, 0, 0);
1592     ok(r & LVS_EX_HEADERDRAGDROP, "Expected LVS_EX_HEADERDRAGDROP to be set\n");
1593     DestroyWindow(hList);
1594
1595     /* requesting header info with LVM_GETSUBITEMRECT doesn't create it */
1596     hList = CreateWindow("SysListView32", "Test", LVS_REPORT, 0, 0, 100, 100, NULL, NULL,
1597                           GetModuleHandle(NULL), 0);
1598     ok(!IsWindow(hHeader), "Header shouldn't be created\n");
1599     ok(NULL == GetDlgItem(hList, 0), "NULL dialog item expected\n");
1600
1601     rect.left = LVIR_BOUNDS;
1602     rect.top  = 1;
1603     rect.right = rect.bottom = -10;
1604     r = SendMessage(hList, LVM_GETSUBITEMRECT, -1, (LPARAM)&rect);
1605     expect(1, r);
1606
1607     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1608     ok(!IsWindow(hHeader), "Header shouldn't be created\n");
1609     ok(GetDlgItem(hList, 0) == NULL, "NULL dialog item expected\n");
1610
1611     DestroyWindow(hList);
1612
1613     /* WM_MEASUREITEM should be sent when created with LVS_OWNERDRAWFIXED */
1614     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1615     hList = create_listview_control(LVS_OWNERDRAWFIXED | LVS_REPORT);
1616     ok_sequence(sequences, PARENT_SEQ_INDEX, create_ownerdrawfixed_parent_seq,
1617                 "created with LVS_OWNERDRAWFIXED|LVS_REPORT - parent seq", FALSE);
1618     DestroyWindow(hList);
1619 }
1620
1621 static void test_redraw(void)
1622 {
1623     HWND hwnd;
1624     HDC hdc;
1625     BOOL res;
1626     DWORD r;
1627
1628     hwnd = create_listview_control(LVS_REPORT);
1629     subclass_header(hwnd);
1630
1631     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1632
1633     InvalidateRect(hwnd, NULL, TRUE);
1634     UpdateWindow(hwnd);
1635     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, redraw_listview_seq, "redraw listview", FALSE);
1636
1637     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1638
1639     /* forward WM_ERASEBKGND to parent on CLR_NONE background color */
1640     /* 1. Without backbuffer */
1641     res = ListView_SetBkColor(hwnd, CLR_NONE);
1642     expect(TRUE, res);
1643
1644     hdc = GetWindowDC(hwndparent);
1645
1646     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1647     r = SendMessageA(hwnd, WM_ERASEBKGND, (WPARAM)hdc, 0);
1648     ok(r == 1, "Expected not zero result\n");
1649     ok_sequence(sequences, PARENT_FULL_SEQ_INDEX, forward_erasebkgnd_parent_seq,
1650                 "forward WM_ERASEBKGND on CLR_NONE", FALSE);
1651
1652     res = ListView_SetBkColor(hwnd, CLR_DEFAULT);
1653     expect(TRUE, res);
1654
1655     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1656     r = SendMessageA(hwnd, WM_ERASEBKGND, (WPARAM)hdc, 0);
1657     expect(1, r);
1658     ok_sequence(sequences, PARENT_FULL_SEQ_INDEX, empty_seq,
1659                 "don't forward WM_ERASEBKGND on non-CLR_NONE", FALSE);
1660
1661     /* 2. With backbuffer */
1662     SendMessageA(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_DOUBLEBUFFER,
1663                                                      LVS_EX_DOUBLEBUFFER);
1664     res = ListView_SetBkColor(hwnd, CLR_NONE);
1665     expect(TRUE, res);
1666
1667     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1668     r = SendMessageA(hwnd, WM_ERASEBKGND, (WPARAM)hdc, 0);
1669     expect(1, r);
1670     ok_sequence(sequences, PARENT_FULL_SEQ_INDEX, forward_erasebkgnd_parent_seq,
1671                 "forward WM_ERASEBKGND on CLR_NONE", FALSE);
1672
1673     res = ListView_SetBkColor(hwnd, CLR_DEFAULT);
1674     expect(TRUE, res);
1675
1676     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1677     r = SendMessageA(hwnd, WM_ERASEBKGND, (WPARAM)hdc, 0);
1678     todo_wine expect(1, r);
1679     ok_sequence(sequences, PARENT_FULL_SEQ_INDEX, empty_seq,
1680                 "don't forward WM_ERASEBKGND on non-CLR_NONE", FALSE);
1681
1682     ReleaseDC(hwndparent, hdc);
1683
1684     DestroyWindow(hwnd);
1685 }
1686
1687 static LRESULT WINAPI cd_wndproc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
1688 {
1689     COLORREF clr, c0ffee = RGB(0xc0, 0xff, 0xee);
1690
1691     if(msg == WM_NOTIFY) {
1692         NMHDR *nmhdr = (PVOID)lp;
1693         if(nmhdr->code == NM_CUSTOMDRAW) {
1694             NMLVCUSTOMDRAW *nmlvcd = (PVOID)nmhdr;
1695             trace("NMCUSTOMDRAW (0x%.8x)\n", nmlvcd->nmcd.dwDrawStage);
1696             switch(nmlvcd->nmcd.dwDrawStage) {
1697             case CDDS_PREPAINT:
1698                 SetBkColor(nmlvcd->nmcd.hdc, c0ffee);
1699                 return CDRF_NOTIFYITEMDRAW;
1700             case CDDS_ITEMPREPAINT:
1701                 nmlvcd->clrTextBk = CLR_DEFAULT;
1702                 return CDRF_NOTIFYSUBITEMDRAW;
1703             case CDDS_ITEMPREPAINT | CDDS_SUBITEM:
1704                 clr = GetBkColor(nmlvcd->nmcd.hdc);
1705                 todo_wine ok(clr == c0ffee, "clr=%.8x\n", clr);
1706                 return CDRF_NOTIFYPOSTPAINT;
1707             case CDDS_ITEMPOSTPAINT | CDDS_SUBITEM:
1708                 clr = GetBkColor(nmlvcd->nmcd.hdc);
1709                 todo_wine ok(clr == c0ffee, "clr=%.8x\n", clr);
1710                 return CDRF_DODEFAULT;
1711             }
1712             return CDRF_DODEFAULT;
1713         }
1714     }
1715
1716     return DefWindowProcA(hwnd, msg, wp, lp);
1717 }
1718
1719 static void test_customdraw(void)
1720 {
1721     HWND hwnd;
1722     WNDPROC oldwndproc;
1723
1724     hwnd = create_listview_control(LVS_REPORT);
1725
1726     insert_column(hwnd, 0);
1727     insert_column(hwnd, 1);
1728     insert_item(hwnd, 0);
1729
1730     oldwndproc = (WNDPROC)SetWindowLongPtr(hwndparent, GWLP_WNDPROC,
1731                                            (LONG_PTR)cd_wndproc);
1732
1733     InvalidateRect(hwnd, NULL, TRUE);
1734     UpdateWindow(hwnd);
1735
1736     SetWindowLongPtr(hwndparent, GWLP_WNDPROC, (LONG_PTR)oldwndproc);
1737
1738     DestroyWindow(hwnd);
1739 }
1740
1741 static void test_icon_spacing(void)
1742 {
1743     /* LVM_SETICONSPACING */
1744     /* note: LVM_SETICONSPACING returns the previous icon spacing if successful */
1745
1746     HWND hwnd;
1747     WORD w, h;
1748     INT r;
1749
1750     hwnd = create_listview_control(LVS_ICON);
1751     ok(hwnd != NULL, "failed to create a listview window\n");
1752
1753     r = SendMessage(hwnd, WM_NOTIFYFORMAT, (WPARAM)hwndparent, NF_REQUERY);
1754     expect(NFR_ANSI, r);
1755
1756     /* reset the icon spacing to defaults */
1757     SendMessage(hwnd, LVM_SETICONSPACING, 0, MAKELPARAM(-1, -1));
1758
1759     /* now we can request what the defaults are */
1760     r = SendMessage(hwnd, LVM_SETICONSPACING, 0, MAKELPARAM(-1, -1));
1761     w = LOWORD(r);
1762     h = HIWORD(r);
1763
1764     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1765
1766     r = SendMessage(hwnd, LVM_SETICONSPACING, 0, MAKELPARAM(20, 30));
1767     ok(r == MAKELONG(w, h) ||
1768        broken(r == MAKELONG(w, w)), /* win98 */
1769        "Expected %d, got %d\n", MAKELONG(w, h), r);
1770
1771     r = SendMessage(hwnd, LVM_SETICONSPACING, 0, MAKELPARAM(25, 35));
1772     if (r == 0)
1773     {
1774         /* version 4.0 */
1775         win_skip("LVM_SETICONSPACING unimplemented. Skipping.\n");
1776         DestroyWindow(hwnd);
1777         return;
1778     }
1779     expect(MAKELONG(20,30), r);
1780
1781     r = SendMessage(hwnd, LVM_SETICONSPACING, 0, MAKELPARAM(-1,-1));
1782     expect(MAKELONG(25,35), r);
1783
1784     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_icon_spacing_seq, "test icon spacing seq", FALSE);
1785
1786     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1787     DestroyWindow(hwnd);
1788 }
1789
1790 static void test_color(void)
1791 {
1792     RECT rect;
1793     HWND hwnd;
1794     DWORD r;
1795     int i;
1796
1797     COLORREF color;
1798     COLORREF colors[4] = {RGB(0,0,0), RGB(100,50,200), CLR_NONE, RGB(255,255,255)};
1799
1800     hwnd = create_listview_control(LVS_REPORT);
1801     ok(hwnd != NULL, "failed to create a listview window\n");
1802
1803     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1804
1805     for (i = 0; i < 4; i++)
1806     {
1807         color = colors[i];
1808
1809         r = SendMessage(hwnd, LVM_SETBKCOLOR, 0, color);
1810         expect(TRUE, r);
1811         r = SendMessage(hwnd, LVM_GETBKCOLOR, 0, 0);
1812         expect(color, r);
1813
1814         r = SendMessage(hwnd, LVM_SETTEXTCOLOR, 0, color);
1815         expect (TRUE, r);
1816         r = SendMessage(hwnd, LVM_GETTEXTCOLOR, 0, 0);
1817         expect(color, r);
1818
1819         r = SendMessage(hwnd, LVM_SETTEXTBKCOLOR, 0, color);
1820         expect(TRUE, r);
1821         r = SendMessage(hwnd, LVM_GETTEXTBKCOLOR, 0, 0);
1822         expect(color, r);
1823     }
1824
1825     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_color_seq, "test color seq", FALSE);
1826     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1827
1828     /* invalidation test done separately to avoid a message chain mess */
1829     r = ValidateRect(hwnd, NULL);
1830     expect(TRUE, r);
1831     r = SendMessage(hwnd, LVM_SETBKCOLOR, 0, colors[0]);
1832     expect(TRUE, r);
1833
1834     rect.right = rect.bottom = 1;
1835     r = GetUpdateRect(hwnd, &rect, TRUE);
1836     todo_wine expect(FALSE, r);
1837     ok(rect.right == 0 && rect.bottom == 0, "got update rectangle\n");
1838
1839     r = ValidateRect(hwnd, NULL);
1840     expect(TRUE, r);
1841     r = SendMessage(hwnd, LVM_SETTEXTCOLOR, 0, colors[0]);
1842     expect(TRUE, r);
1843
1844     rect.right = rect.bottom = 1;
1845     r = GetUpdateRect(hwnd, &rect, TRUE);
1846     todo_wine expect(FALSE, r);
1847     ok(rect.right == 0 && rect.bottom == 0, "got update rectangle\n");
1848
1849     r = ValidateRect(hwnd, NULL);
1850     expect(TRUE, r);
1851     r = SendMessage(hwnd, LVM_SETTEXTBKCOLOR, 0, colors[0]);
1852     expect(TRUE, r);
1853
1854     rect.right = rect.bottom = 1;
1855     r = GetUpdateRect(hwnd, &rect, TRUE);
1856     todo_wine expect(FALSE, r);
1857     ok(rect.right == 0 && rect.bottom == 0, "got update rectangle\n");
1858
1859     DestroyWindow(hwnd);
1860 }
1861
1862 static void test_item_count(void)
1863 {
1864     /* LVM_INSERTITEM, LVM_DELETEITEM, LVM_DELETEALLITEMS, LVM_GETITEMCOUNT */
1865
1866     HWND hwnd;
1867     DWORD r;
1868     HDC hdc;
1869     HFONT hOldFont;
1870     TEXTMETRICA tm;
1871     RECT rect;
1872     INT height;
1873
1874     LVITEM item0;
1875     LVITEM item1;
1876     LVITEM item2;
1877     static CHAR item0text[] = "item0";
1878     static CHAR item1text[] = "item1";
1879     static CHAR item2text[] = "item2";
1880
1881     hwnd = create_listview_control(LVS_REPORT);
1882     ok(hwnd != NULL, "failed to create a listview window\n");
1883
1884     /* resize in dpiaware manner to fit all 3 items added */
1885     hdc = GetDC(0);
1886     hOldFont = SelectObject(hdc, GetStockObject(SYSTEM_FONT));
1887     GetTextMetricsA(hdc, &tm);
1888     /* 2 extra pixels for bounds and header border */
1889     height = tm.tmHeight + 2;
1890     SelectObject(hdc, hOldFont);
1891     ReleaseDC(0, hdc);
1892
1893     GetWindowRect(hwnd, &rect);
1894     /* 3 items + 1 header + 1 to be sure */
1895     MoveWindow(hwnd, 0, 0, rect.right - rect.left, 5 * height, FALSE);
1896
1897     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1898
1899     r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
1900     expect(0, r);
1901
1902     /* [item0] */
1903     item0.mask = LVIF_TEXT;
1904     item0.iItem = 0;
1905     item0.iSubItem = 0;
1906     item0.pszText = item0text;
1907     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item0);
1908     expect(0, r);
1909
1910     /* [item0, item1] */
1911     item1.mask = LVIF_TEXT;
1912     item1.iItem = 1;
1913     item1.iSubItem = 0;
1914     item1.pszText = item1text;
1915     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item1);
1916     expect(1, r);
1917
1918     /* [item0, item1, item2] */
1919     item2.mask = LVIF_TEXT;
1920     item2.iItem = 2;
1921     item2.iSubItem = 0;
1922     item2.pszText = item2text;
1923     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item2);
1924     expect(2, r);
1925
1926     r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
1927     expect(3, r);
1928
1929     /* [item0, item1] */
1930     r = SendMessage(hwnd, LVM_DELETEITEM, 2, 0);
1931     expect(TRUE, r);
1932
1933     r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
1934     expect(2, r);
1935
1936     /* [] */
1937     r = SendMessage(hwnd, LVM_DELETEALLITEMS, 0, 0);
1938     expect(TRUE, r);
1939
1940     r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
1941     expect(0, r);
1942
1943     /* [item0] */
1944     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item1);
1945     expect(0, r);
1946
1947     /* [item0, item1] */
1948     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item1);
1949     expect(1, r);
1950
1951     r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
1952     expect(2, r);
1953
1954     /* [item0, item1, item2] */
1955     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item2);
1956     expect(2, r);
1957
1958     r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
1959     expect(3, r);
1960
1961     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_item_count_seq, "test item count seq", FALSE);
1962
1963     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1964     DestroyWindow(hwnd);
1965 }
1966
1967 static void test_item_position(void)
1968 {
1969     /* LVM_SETITEMPOSITION/LVM_GETITEMPOSITION */
1970
1971     HWND hwnd;
1972     DWORD r;
1973     POINT position;
1974
1975     LVITEM item0;
1976     LVITEM item1;
1977     LVITEM item2;
1978     static CHAR item0text[] = "item0";
1979     static CHAR item1text[] = "item1";
1980     static CHAR item2text[] = "item2";
1981
1982     hwnd = create_listview_control(LVS_ICON);
1983     ok(hwnd != NULL, "failed to create a listview window\n");
1984
1985     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1986
1987     /* [item0] */
1988     item0.mask = LVIF_TEXT;
1989     item0.iItem = 0;
1990     item0.iSubItem = 0;
1991     item0.pszText = item0text;
1992     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item0);
1993     expect(0, r);
1994
1995     /* [item0, item1] */
1996     item1.mask = LVIF_TEXT;
1997     item1.iItem = 1;
1998     item1.iSubItem = 0;
1999     item1.pszText = item1text;
2000     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item1);
2001     expect(1, r);
2002
2003     /* [item0, item1, item2] */
2004     item2.mask = LVIF_TEXT;
2005     item2.iItem = 2;
2006     item2.iSubItem = 0;
2007     item2.pszText = item2text;
2008     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item2);
2009     expect(2, r);
2010
2011     r = SendMessage(hwnd, LVM_SETITEMPOSITION, 1, MAKELPARAM(10,5));
2012     expect(TRUE, r);
2013     r = SendMessage(hwnd, LVM_GETITEMPOSITION, 1, (LPARAM) &position);
2014     expect(TRUE, r);
2015     expect2(10, 5, position.x, position.y);
2016
2017     r = SendMessage(hwnd, LVM_SETITEMPOSITION, 2, MAKELPARAM(0,0));
2018     expect(TRUE, r);
2019     r = SendMessage(hwnd, LVM_GETITEMPOSITION, 2, (LPARAM) &position);
2020     expect(TRUE, r);
2021     expect2(0, 0, position.x, position.y);
2022
2023     r = SendMessage(hwnd, LVM_SETITEMPOSITION, 0, MAKELPARAM(20,20));
2024     expect(TRUE, r);
2025     r = SendMessage(hwnd, LVM_GETITEMPOSITION, 0, (LPARAM) &position);
2026     expect(TRUE, r);
2027     expect2(20, 20, position.x, position.y);
2028
2029     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_itempos_seq, "test item position seq", TRUE);
2030
2031     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2032     DestroyWindow(hwnd);
2033 }
2034
2035 static void test_getorigin(void)
2036 {
2037     /* LVM_GETORIGIN */
2038
2039     HWND hwnd;
2040     DWORD r;
2041     POINT position;
2042
2043     position.x = position.y = 0;
2044
2045     hwnd = create_listview_control(LVS_ICON);
2046     ok(hwnd != NULL, "failed to create a listview window\n");
2047     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2048
2049     r = SendMessage(hwnd, LVM_GETORIGIN, 0, (LPARAM)&position);
2050     expect(TRUE, r);
2051     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2052     DestroyWindow(hwnd);
2053
2054     hwnd = create_listview_control(LVS_SMALLICON);
2055     ok(hwnd != NULL, "failed to create a listview window\n");
2056     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2057
2058     r = SendMessage(hwnd, LVM_GETORIGIN, 0, (LPARAM)&position);
2059     expect(TRUE, r);
2060     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2061     DestroyWindow(hwnd);
2062
2063     hwnd = create_listview_control(LVS_LIST);
2064     ok(hwnd != NULL, "failed to create a listview window\n");
2065     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2066
2067     r = SendMessage(hwnd, LVM_GETORIGIN, 0, (LPARAM)&position);
2068     expect(FALSE, r);
2069     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2070     DestroyWindow(hwnd);
2071
2072     hwnd = create_listview_control(LVS_REPORT);
2073     ok(hwnd != NULL, "failed to create a listview window\n");
2074     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2075
2076     r = SendMessage(hwnd, LVM_GETORIGIN, 0, (LPARAM)&position);
2077     expect(FALSE, r);
2078     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2079     DestroyWindow(hwnd);
2080 }
2081
2082 static void test_multiselect(void)
2083 {
2084     typedef struct t_select_task
2085     {
2086         const char *descr;
2087         int initPos;
2088         int loopVK;
2089         int count;
2090         int result;
2091     } select_task;
2092
2093     HWND hwnd;
2094     INT r;
2095     int i,j,item_count,selected_count;
2096     static const int items=5;
2097     BYTE kstate[256];
2098     select_task task;
2099     LONG_PTR style;
2100     LVITEMA item;
2101
2102     static struct t_select_task task_list[] = {
2103         { "using VK_DOWN", 0, VK_DOWN, -1, -1 },
2104         { "using VK_UP", -1, VK_UP, -1, -1 },
2105         { "using VK_END", 0, VK_END, 1, -1 },
2106         { "using VK_HOME", -1, VK_HOME, 1, -1 }
2107     };
2108
2109     hwnd = create_listview_control(LVS_REPORT);
2110
2111     for (i=0;i<items;i++) {
2112             insert_item(hwnd, 0);
2113     }
2114
2115     item_count = (int)SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
2116
2117     expect(items,item_count);
2118
2119     for (i=0;i<4;i++) {
2120         task = task_list[i];
2121
2122         /* deselect all items */
2123         ListView_SetItemState(hwnd, -1, 0, LVIS_SELECTED);
2124         SendMessage(hwnd, LVM_SETSELECTIONMARK, 0, -1);
2125
2126         /* set initial position */
2127         SendMessage(hwnd, LVM_SETSELECTIONMARK, 0, (task.initPos == -1 ? item_count -1 : task.initPos));
2128         ListView_SetItemState(hwnd,(task.initPos == -1 ? item_count -1 : task.initPos),LVIS_SELECTED ,LVIS_SELECTED);
2129
2130         selected_count = (int)SendMessage(hwnd, LVM_GETSELECTEDCOUNT, 0, 0);
2131
2132         ok(selected_count == 1, "There should be only one selected item at the beginning (is %d)\n",selected_count);
2133
2134         /* Set SHIFT key pressed */
2135         GetKeyboardState(kstate);
2136         kstate[VK_SHIFT]=0x80;
2137         SetKeyboardState(kstate);
2138
2139         for (j=1;j<=(task.count == -1 ? item_count : task.count);j++) {
2140             r = SendMessage(hwnd, WM_KEYDOWN, task.loopVK, 0);
2141             expect(0,r);
2142             r = SendMessage(hwnd, WM_KEYUP, task.loopVK, 0);
2143             expect(0,r);
2144         }
2145
2146         selected_count = (int)SendMessage(hwnd, LVM_GETSELECTEDCOUNT, 0, 0);
2147
2148         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);
2149
2150         /* Set SHIFT key released */
2151         GetKeyboardState(kstate);
2152         kstate[VK_SHIFT]=0x00;
2153         SetKeyboardState(kstate);
2154     }
2155     DestroyWindow(hwnd);
2156
2157     /* make multiple selection, then switch to LVS_SINGLESEL */
2158     hwnd = create_listview_control(LVS_REPORT);
2159     for (i=0;i<items;i++) {
2160             insert_item(hwnd, 0);
2161     }
2162     item_count = (int)SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
2163     expect(items,item_count);
2164
2165     /* try with NULL pointer */
2166     r = SendMessageA(hwnd, LVM_SETITEMSTATE, 0, 0);
2167     expect(FALSE, r);
2168
2169     /* select all, check notifications */
2170     ListView_SetItemState(hwnd, -1, 0, LVIS_SELECTED);
2171
2172     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2173
2174     item.stateMask = LVIS_SELECTED;
2175     item.state     = LVIS_SELECTED;
2176     r = SendMessageA(hwnd, LVM_SETITEMSTATE, -1, (LPARAM)&item);
2177     expect(TRUE, r);
2178
2179     ok_sequence(sequences, PARENT_SEQ_INDEX, select_all_parent_seq,
2180                 "select all notification", FALSE);
2181
2182     /* deselect all items */
2183     ListView_SetItemState(hwnd, -1, 0, LVIS_SELECTED);
2184     SendMessage(hwnd, LVM_SETSELECTIONMARK, 0, -1);
2185     for (i=0;i<3;i++) {
2186         ListView_SetItemState(hwnd, i, LVIS_SELECTED, LVIS_SELECTED);
2187     }
2188
2189     r = SendMessage(hwnd, LVM_GETSELECTEDCOUNT, 0, 0);
2190     expect(3, r);
2191     r = SendMessage(hwnd, LVM_GETSELECTIONMARK, 0, 0);
2192     expect(-1, r);
2193
2194     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2195     ok(!(style & LVS_SINGLESEL), "LVS_SINGLESEL isn't expected\n");
2196     SetWindowLongPtrA(hwnd, GWL_STYLE, style | LVS_SINGLESEL);
2197     /* check that style is accepted */
2198     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2199     ok(style & LVS_SINGLESEL, "LVS_SINGLESEL expected\n");
2200
2201     for (i=0;i<3;i++) {
2202         r = ListView_GetItemState(hwnd, i, LVIS_SELECTED);
2203         ok(r & LVIS_SELECTED, "Expected item %d to be selected\n", i);
2204     }
2205     r = SendMessage(hwnd, LVM_GETSELECTEDCOUNT, 0, 0);
2206     expect(3, r);
2207     SendMessage(hwnd, LVM_GETSELECTIONMARK, 0, 0);
2208     expect(3, r);
2209
2210     /* select one more */
2211     ListView_SetItemState(hwnd, 3, LVIS_SELECTED, LVIS_SELECTED);
2212
2213     for (i=0;i<3;i++) {
2214         r = ListView_GetItemState(hwnd, i, LVIS_SELECTED);
2215         ok(!(r & LVIS_SELECTED), "Expected item %d to be unselected\n", i);
2216     }
2217     r = ListView_GetItemState(hwnd, 3, LVIS_SELECTED);
2218     ok(r & LVIS_SELECTED, "Expected item %d to be selected\n", i);
2219
2220     r = SendMessage(hwnd, LVM_GETSELECTEDCOUNT, 0, 0);
2221     expect(1, r);
2222     r = SendMessage(hwnd, LVM_GETSELECTIONMARK, 0, 0);
2223     expect(-1, r);
2224
2225     /* try to select all on LVS_SINGLESEL */
2226     memset(&item, 0, sizeof(item));
2227     item.stateMask = LVIS_SELECTED;
2228     r = SendMessage(hwnd, LVM_SETITEMSTATE, -1, (LPARAM)&item);
2229     expect(TRUE, r);
2230     SendMessage(hwnd, LVM_SETSELECTIONMARK, 0, -1);
2231
2232     item.stateMask = LVIS_SELECTED;
2233     item.state     = LVIS_SELECTED;
2234     r = SendMessage(hwnd, LVM_SETITEMSTATE, -1, (LPARAM)&item);
2235     expect(FALSE, r);
2236
2237     r = ListView_GetSelectedCount(hwnd);
2238     expect(0, r);
2239     r = ListView_GetSelectionMark(hwnd);
2240     expect(-1, r);
2241
2242     /* try to deselect all on LVS_SINGLESEL */
2243     item.stateMask = LVIS_SELECTED;
2244     item.state     = LVIS_SELECTED;
2245     r = SendMessage(hwnd, LVM_SETITEMSTATE, 0, (LPARAM)&item);
2246     expect(TRUE, r);
2247
2248     item.stateMask = LVIS_SELECTED;
2249     item.state     = 0;
2250     r = SendMessage(hwnd, LVM_SETITEMSTATE, -1, (LPARAM)&item);
2251     expect(TRUE, r);
2252     r = ListView_GetSelectedCount(hwnd);
2253     expect(0, r);
2254
2255     /* 1. selection mark is update when new focused item is set */
2256     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2257     SetWindowLongPtrA(hwnd, GWL_STYLE, style & ~LVS_SINGLESEL);
2258
2259     r = SendMessage(hwnd, LVM_SETSELECTIONMARK, 0, -1);
2260     expect(-1, r);
2261
2262     item.stateMask = LVIS_FOCUSED;
2263     item.state     = LVIS_FOCUSED;
2264     r = SendMessage(hwnd, LVM_SETITEMSTATE, 0, (LPARAM)&item);
2265     expect(TRUE, r);
2266
2267     r = SendMessage(hwnd, LVM_GETSELECTIONMARK, 0, 0);
2268     expect(0, r);
2269
2270     /* it's not updated if already set */
2271     item.stateMask = LVIS_FOCUSED;
2272     item.state     = LVIS_FOCUSED;
2273     r = SendMessage(hwnd, LVM_SETITEMSTATE, 1, (LPARAM)&item);
2274     expect(TRUE, r);
2275
2276     r = SendMessage(hwnd, LVM_GETSELECTIONMARK, 0, 0);
2277     expect(0, r);
2278
2279     r = SendMessage(hwnd, LVM_SETSELECTIONMARK, 0, -1);
2280     expect(0, r);
2281
2282     item.stateMask = LVIS_FOCUSED;
2283     item.state     = LVIS_FOCUSED;
2284     r = SendMessage(hwnd, LVM_SETITEMSTATE, 1, (LPARAM)&item);
2285     expect(TRUE, r);
2286
2287     r = SendMessage(hwnd, LVM_GETSELECTIONMARK, 0, 0);
2288     expect(-1, r);
2289
2290     /* need to reset focused item first */
2291     item.stateMask = LVIS_FOCUSED;
2292     item.state     = 0;
2293     r = SendMessage(hwnd, LVM_SETITEMSTATE, -1, (LPARAM)&item);
2294     expect(TRUE, r);
2295
2296     item.stateMask = LVIS_FOCUSED;
2297     item.state     = LVIS_FOCUSED;
2298     r = SendMessage(hwnd, LVM_SETITEMSTATE, 2, (LPARAM)&item);
2299     expect(TRUE, r);
2300
2301     r = SendMessage(hwnd, LVM_GETSELECTIONMARK, 0, 0);
2302     expect(2, r);
2303
2304     item.stateMask = LVIS_FOCUSED;
2305     item.state     = 0;
2306     r = SendMessage(hwnd, LVM_SETITEMSTATE, -1, (LPARAM)&item);
2307     expect(TRUE, r);
2308
2309     r = SendMessage(hwnd, LVM_GETSELECTIONMARK, 0, 0);
2310     expect(2, r);
2311
2312     /* 2. same tests, with LVM_SETITEM */
2313     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2314     SetWindowLongPtrA(hwnd, GWL_STYLE, style & ~LVS_SINGLESEL);
2315
2316     r = SendMessage(hwnd, LVM_SETSELECTIONMARK, 0, -1);
2317     expect(2, r);
2318
2319     item.stateMask = LVIS_FOCUSED;
2320     item.state     = LVIS_FOCUSED;
2321     item.mask      = LVIF_STATE;
2322     item.iItem = item.iSubItem = 0;
2323     r = SendMessage(hwnd, LVM_SETITEMA, 0, (LPARAM)&item);
2324     expect(TRUE, r);
2325
2326     r = SendMessage(hwnd, LVM_GETSELECTIONMARK, 0, 0);
2327     expect(0, r);
2328
2329     /* it's not updated if already set */
2330     item.stateMask = LVIS_FOCUSED;
2331     item.state     = LVIS_FOCUSED;
2332     item.mask      = LVIF_STATE;
2333     item.iItem     = 1;
2334     item.iSubItem  = 0;
2335     r = SendMessage(hwnd, LVM_SETITEMA, 0, (LPARAM)&item);
2336     expect(TRUE, r);
2337
2338     r = SendMessage(hwnd, LVM_GETSELECTIONMARK, 0, 0);
2339     expect(0, r);
2340
2341     r = SendMessage(hwnd, LVM_SETSELECTIONMARK, 0, -1);
2342     expect(0, r);
2343
2344     item.stateMask = LVIS_FOCUSED;
2345     item.state     = LVIS_FOCUSED;
2346     item.mask      = LVIF_STATE;
2347     item.iItem     = 1;
2348     item.iSubItem  = 0;
2349     r = SendMessage(hwnd, LVM_SETITEMA, 0, (LPARAM)&item);
2350     expect(TRUE, r);
2351
2352     r = SendMessage(hwnd, LVM_GETSELECTIONMARK, 0, 0);
2353     expect(-1, r);
2354
2355     /* need to reset focused item first */
2356     item.stateMask = LVIS_FOCUSED;
2357     item.state     = 0;
2358     r = SendMessage(hwnd, LVM_SETITEMSTATE, -1, (LPARAM)&item);
2359     expect(TRUE, r);
2360
2361     item.stateMask = LVIS_FOCUSED;
2362     item.state     = LVIS_FOCUSED;
2363     item.mask      = LVIF_STATE;
2364     item.iItem     = 2;
2365     item.iSubItem  = 0;
2366     r = SendMessage(hwnd, LVM_SETITEMA, 0, (LPARAM)&item);
2367     expect(TRUE, r);
2368
2369     r = SendMessage(hwnd, LVM_GETSELECTIONMARK, 0, 0);
2370     expect(2, r);
2371
2372     item.stateMask = LVIS_FOCUSED;
2373     item.state     = 0;
2374     r = SendMessage(hwnd, LVM_SETITEMSTATE, -1, (LPARAM)&item);
2375     expect(TRUE, r);
2376
2377     r = SendMessage(hwnd, LVM_GETSELECTIONMARK, 0, 0);
2378     expect(2, r);
2379
2380     DestroyWindow(hwnd);
2381 }
2382
2383 static void test_subitem_rect(void)
2384 {
2385     HWND hwnd;
2386     DWORD r;
2387     LVCOLUMN col;
2388     RECT rect, rect2;
2389     INT arr[3];
2390
2391     /* test LVM_GETSUBITEMRECT for header */
2392     hwnd = create_listview_control(LVS_REPORT);
2393     ok(hwnd != NULL, "failed to create a listview window\n");
2394     /* add some columns */
2395     memset(&col, 0, sizeof(LVCOLUMN));
2396     col.mask = LVCF_WIDTH;
2397     col.cx = 100;
2398     r = SendMessage(hwnd, LVM_INSERTCOLUMN, 0, (LPARAM)&col);
2399     expect(0, r);
2400     col.cx = 150;
2401     r = SendMessage(hwnd, LVM_INSERTCOLUMN, 1, (LPARAM)&col);
2402     expect(1, r);
2403     col.cx = 200;
2404     r = SendMessage(hwnd, LVM_INSERTCOLUMN, 2, (LPARAM)&col);
2405     expect(2, r);
2406     /* item = -1 means header, subitem index is 1 based */
2407     rect.left = LVIR_BOUNDS;
2408     rect.top  = 0;
2409     rect.right = rect.bottom = 0;
2410     r = SendMessage(hwnd, LVM_GETSUBITEMRECT, -1, (LPARAM)&rect);
2411     expect(0, r);
2412
2413     rect.left = LVIR_BOUNDS;
2414     rect.top  = 1;
2415     rect.right = rect.bottom = 0;
2416     r = SendMessage(hwnd, LVM_GETSUBITEMRECT, -1, (LPARAM)&rect);
2417     expect(1, r);
2418
2419     expect(100, rect.left);
2420     expect(250, rect.right);
2421 todo_wine
2422     expect(3, rect.top);
2423
2424     rect.left = LVIR_BOUNDS;
2425     rect.top  = 2;
2426     rect.right = rect.bottom = 0;
2427     r = SendMessage(hwnd, LVM_GETSUBITEMRECT, -1, (LPARAM)&rect);
2428     expect(1, r);
2429
2430     expect(250, rect.left);
2431     expect(450, rect.right);
2432 todo_wine
2433     expect(3, rect.top);
2434
2435     /* item LVS_REPORT padding isn't applied to subitems */
2436     insert_item(hwnd, 0);
2437
2438     rect.left = LVIR_BOUNDS;
2439     rect.top  = 1;
2440     rect.right = rect.bottom = 0;
2441     r = SendMessage(hwnd, LVM_GETSUBITEMRECT, 0, (LPARAM)&rect);
2442     expect(1, r);
2443     expect(100, rect.left);
2444     expect(250, rect.right);
2445
2446     rect.left = LVIR_ICON;
2447     rect.top  = 1;
2448     rect.right = rect.bottom = 0;
2449     r = SendMessage(hwnd, LVM_GETSUBITEMRECT, 0, (LPARAM)&rect);
2450     expect(1, r);
2451     /* no icon attached - zero width rectangle, with no left padding */
2452     expect(100, rect.left);
2453     expect(100, rect.right);
2454
2455     rect.left = LVIR_LABEL;
2456     rect.top  = 1;
2457     rect.right = rect.bottom = 0;
2458     r = SendMessage(hwnd, LVM_GETSUBITEMRECT, 0, (LPARAM)&rect);
2459     expect(1, r);
2460     /* same as full LVIR_BOUNDS */
2461     expect(100, rect.left);
2462     expect(250, rect.right);
2463
2464     SendMessage(hwnd, LVM_SCROLL, 10, 0);
2465
2466     rect.left = LVIR_BOUNDS;
2467     rect.top  = 1;
2468     rect.right = rect.bottom = 0;
2469     r = SendMessage(hwnd, LVM_GETSUBITEMRECT, 0, (LPARAM)&rect);
2470     expect(1, r);
2471     expect(90, rect.left);
2472     expect(240, rect.right);
2473
2474     SendMessage(hwnd, LVM_SCROLL, -10, 0);
2475
2476     DestroyWindow(hwnd);
2477
2478     /* test subitem rects after re-arranging columns */
2479     hwnd = create_listview_control(LVS_REPORT);
2480     ok(hwnd != NULL, "failed to create a listview window\n");
2481     memset(&col, 0, sizeof(LVCOLUMN));
2482     col.mask = LVCF_WIDTH;
2483
2484     col.cx = 100;
2485     r = SendMessage(hwnd, LVM_INSERTCOLUMN, 0, (LPARAM)&col);
2486     expect(0, r);
2487
2488     col.cx = 200;
2489     r = SendMessage(hwnd, LVM_INSERTCOLUMN, 1, (LPARAM)&col);
2490     expect(1, r);
2491
2492     col.cx = 300;
2493     r = SendMessage(hwnd, LVM_INSERTCOLUMN, 2, (LPARAM)&col);
2494     expect(2, r);
2495
2496     insert_item(hwnd, 0);
2497     insert_item(hwnd, 1);
2498
2499     /* wrong item is refused for main item */
2500     rect.left = LVIR_BOUNDS;
2501     rect.top  = 0;
2502     rect.right = rect.bottom = -1;
2503     r = SendMessage(hwnd, LVM_GETSUBITEMRECT, 2, (LPARAM)&rect);
2504     expect(FALSE, r);
2505
2506     /* for subitems rectangle is calculated even if there's no item added */
2507     rect.left = LVIR_BOUNDS;
2508     rect.top  = 1;
2509     rect.right = rect.bottom = -1;
2510     r = SendMessage(hwnd, LVM_GETSUBITEMRECT, 1, (LPARAM)&rect);
2511     expect(TRUE, r);
2512
2513     rect2.left = LVIR_BOUNDS;
2514     rect2.top  = 1;
2515     rect2.right = rect2.bottom = -1;
2516     r = SendMessage(hwnd, LVM_GETSUBITEMRECT, 2, (LPARAM)&rect2);
2517 todo_wine {
2518     expect(TRUE, r);
2519     expect(rect.right, rect2.right);
2520     expect(rect.left, rect2.left);
2521     expect(rect.bottom, rect2.top);
2522     ok(rect2.bottom > rect2.top, "expected not zero height\n");
2523 }
2524
2525     arr[0] = 1; arr[1] = 0; arr[2] = 2;
2526     r = SendMessage(hwnd, LVM_SETCOLUMNORDERARRAY, 3, (LPARAM)arr);
2527     expect(TRUE, r);
2528
2529     rect.left = LVIR_BOUNDS;
2530     rect.top  = 0;
2531     rect.right = rect.bottom = -1;
2532     r = SendMessage(hwnd, LVM_GETSUBITEMRECT, 0, (LPARAM)&rect);
2533     expect(TRUE, r);
2534     expect(0, rect.left);
2535     expect(600, rect.right);
2536
2537     rect.left = LVIR_BOUNDS;
2538     rect.top  = 1;
2539     rect.right = rect.bottom = -1;
2540     r = SendMessage(hwnd, LVM_GETSUBITEMRECT, 0, (LPARAM)&rect);
2541     expect(TRUE, r);
2542     expect(0, rect.left);
2543     expect(200, rect.right);
2544
2545     rect2.left = LVIR_BOUNDS;
2546     rect2.top  = 1;
2547     rect2.right = rect2.bottom = -1;
2548     r = SendMessage(hwnd, LVM_GETSUBITEMRECT, 1, (LPARAM)&rect2);
2549     expect(TRUE, r);
2550     expect(0, rect2.left);
2551     expect(200, rect2.right);
2552     /* items are of the same height */
2553     ok(rect2.top > 0, "expected positive item height\n");
2554     expect(rect.bottom, rect2.top);
2555     expect(rect.bottom * 2 - rect.top, rect2.bottom);
2556
2557     rect.left = LVIR_BOUNDS;
2558     rect.top  = 2;
2559     rect.right = rect.bottom = -1;
2560     r = SendMessage(hwnd, LVM_GETSUBITEMRECT, 0, (LPARAM)&rect);
2561     expect(TRUE, r);
2562     expect(300, rect.left);
2563     expect(600, rect.right);
2564
2565     DestroyWindow(hwnd);
2566
2567     /* try it for non LVS_REPORT style */
2568     hwnd = CreateWindow("SysListView32", "Test", LVS_ICON, 0, 0, 100, 100, NULL, NULL,
2569                          GetModuleHandle(NULL), 0);
2570     rect.left = LVIR_BOUNDS;
2571     rect.top  = 1;
2572     rect.right = rect.bottom = -10;
2573     r = SendMessage(hwnd, LVM_GETSUBITEMRECT, -1, (LPARAM)&rect);
2574     expect(0, r);
2575     /* rect is unchanged */
2576     expect(0, rect.left);
2577     expect(-10, rect.right);
2578     expect(1, rect.top);
2579     expect(-10, rect.bottom);
2580     DestroyWindow(hwnd);
2581 }
2582
2583 /* comparison callback for test_sorting */
2584 static INT WINAPI test_CallBackCompare(LPARAM first, LPARAM second, LPARAM lParam)
2585 {
2586     if (first == second) return 0;
2587     return (first > second ? 1 : -1);
2588 }
2589
2590 static void test_sorting(void)
2591 {
2592     HWND hwnd;
2593     LVITEMA item = {0};
2594     INT r;
2595     LONG_PTR style;
2596     static CHAR names[][5] = {"A", "B", "C", "D", "0"};
2597     CHAR buff[10];
2598
2599     hwnd = create_listview_control(LVS_REPORT);
2600     ok(hwnd != NULL, "failed to create a listview window\n");
2601
2602     /* insert some items */
2603     item.mask = LVIF_PARAM | LVIF_STATE;
2604     item.state = LVIS_SELECTED;
2605     item.iItem = 0;
2606     item.iSubItem = 0;
2607     item.lParam = 3;
2608     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
2609     expect(0, r);
2610
2611     item.mask = LVIF_PARAM;
2612     item.iItem = 1;
2613     item.iSubItem = 0;
2614     item.lParam = 2;
2615     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
2616     expect(1, r);
2617
2618     item.mask = LVIF_STATE | LVIF_PARAM;
2619     item.state = LVIS_SELECTED;
2620     item.iItem = 2;
2621     item.iSubItem = 0;
2622     item.lParam = 4;
2623     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
2624     expect(2, r);
2625
2626     r = SendMessage(hwnd, LVM_GETSELECTIONMARK, 0, 0);
2627     expect(-1, r);
2628
2629     r = SendMessage(hwnd, LVM_GETSELECTEDCOUNT, 0, 0);
2630     expect(2, r);
2631
2632     r = SendMessage(hwnd, LVM_SORTITEMS, 0, (LPARAM)test_CallBackCompare);
2633     expect(TRUE, r);
2634
2635     r = SendMessage(hwnd, LVM_GETSELECTEDCOUNT, 0, 0);
2636     expect(2, r);
2637     r = SendMessage(hwnd, LVM_GETSELECTIONMARK, 0, 0);
2638     expect(-1, r);
2639     r = SendMessage(hwnd, LVM_GETITEMSTATE, 0, LVIS_SELECTED);
2640     expect(0, r);
2641     r = SendMessage(hwnd, LVM_GETITEMSTATE, 1, LVIS_SELECTED);
2642     expect(LVIS_SELECTED, r);
2643     r = SendMessage(hwnd, LVM_GETITEMSTATE, 2, LVIS_SELECTED);
2644     expect(LVIS_SELECTED, r);
2645
2646     DestroyWindow(hwnd);
2647
2648     /* switch to LVS_SORTASCENDING when some items added */
2649     hwnd = create_listview_control(LVS_REPORT);
2650     ok(hwnd != NULL, "failed to create a listview window\n");
2651
2652     item.mask = LVIF_TEXT;
2653     item.iItem = 0;
2654     item.iSubItem = 0;
2655     item.pszText = names[1];
2656     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
2657     expect(0, r);
2658
2659     item.mask = LVIF_TEXT;
2660     item.iItem = 1;
2661     item.iSubItem = 0;
2662     item.pszText = names[2];
2663     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
2664     expect(1, r);
2665
2666     item.mask = LVIF_TEXT;
2667     item.iItem = 2;
2668     item.iSubItem = 0;
2669     item.pszText = names[0];
2670     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
2671     expect(2, r);
2672
2673     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2674     SetWindowLongPtrA(hwnd, GWL_STYLE, style | LVS_SORTASCENDING);
2675     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2676     ok(style & LVS_SORTASCENDING, "Expected LVS_SORTASCENDING to be set\n");
2677
2678     /* no sorting performed when switched to LVS_SORTASCENDING */
2679     item.mask = LVIF_TEXT;
2680     item.iItem = 0;
2681     item.pszText = buff;
2682     item.cchTextMax = sizeof(buff);
2683     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
2684     expect(TRUE, r);
2685     ok(lstrcmp(buff, names[1]) == 0, "Expected '%s', got '%s'\n", names[1], buff);
2686
2687     item.iItem = 1;
2688     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
2689     expect(TRUE, r);
2690     ok(lstrcmp(buff, names[2]) == 0, "Expected '%s', got '%s'\n", names[2], buff);
2691
2692     item.iItem = 2;
2693     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
2694     expect(TRUE, r);
2695     ok(lstrcmp(buff, names[0]) == 0, "Expected '%s', got '%s'\n", names[0], buff);
2696
2697     /* adding new item doesn't resort list */
2698     item.mask = LVIF_TEXT;
2699     item.iItem = 3;
2700     item.iSubItem = 0;
2701     item.pszText = names[3];
2702     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
2703     expect(3, r);
2704
2705     item.mask = LVIF_TEXT;
2706     item.iItem = 0;
2707     item.pszText = buff;
2708     item.cchTextMax = sizeof(buff);
2709     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
2710     expect(TRUE, r);
2711     ok(lstrcmp(buff, names[1]) == 0, "Expected '%s', got '%s'\n", names[1], buff);
2712
2713     item.iItem = 1;
2714     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
2715     expect(TRUE, r);
2716     ok(lstrcmp(buff, names[2]) == 0, "Expected '%s', got '%s'\n", names[2], buff);
2717
2718     item.iItem = 2;
2719     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
2720     expect(TRUE, r);
2721     ok(lstrcmp(buff, names[0]) == 0, "Expected '%s', got '%s'\n", names[0], buff);
2722
2723     item.iItem = 3;
2724     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
2725     expect(TRUE, r);
2726     ok(lstrcmp(buff, names[3]) == 0, "Expected '%s', got '%s'\n", names[3], buff);
2727
2728     /* corner case - item should be placed at first position */
2729     item.mask = LVIF_TEXT;
2730     item.iItem = 4;
2731     item.iSubItem = 0;
2732     item.pszText = names[4];
2733     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
2734     expect(0, r);
2735
2736     item.iItem = 0;
2737     item.pszText = buff;
2738     item.cchTextMax = sizeof(buff);
2739     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
2740     expect(TRUE, r);
2741     ok(lstrcmp(buff, names[4]) == 0, "Expected '%s', got '%s'\n", names[4], buff);
2742
2743     item.iItem = 1;
2744     item.pszText = buff;
2745     item.cchTextMax = sizeof(buff);
2746     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
2747     expect(TRUE, r);
2748     ok(lstrcmp(buff, names[1]) == 0, "Expected '%s', got '%s'\n", names[1], buff);
2749
2750     item.iItem = 2;
2751     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
2752     expect(TRUE, r);
2753     ok(lstrcmp(buff, names[2]) == 0, "Expected '%s', got '%s'\n", names[2], buff);
2754
2755     item.iItem = 3;
2756     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
2757     expect(TRUE, r);
2758     ok(lstrcmp(buff, names[0]) == 0, "Expected '%s', got '%s'\n", names[0], buff);
2759
2760     item.iItem = 4;
2761     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
2762     expect(TRUE, r);
2763     ok(lstrcmp(buff, names[3]) == 0, "Expected '%s', got '%s'\n", names[3], buff);
2764
2765     DestroyWindow(hwnd);
2766 }
2767
2768 static void test_ownerdata(void)
2769 {
2770     static char test_str[] = "test";
2771
2772     HWND hwnd;
2773     LONG_PTR style, ret;
2774     DWORD res;
2775     LVITEMA item;
2776
2777     /* it isn't possible to set LVS_OWNERDATA after creation */
2778     if (g_is_below_5)
2779     {
2780         win_skip("set LVS_OWNERDATA after creation leads to crash on < 5.80\n");
2781     }
2782     else
2783     {
2784         hwnd = create_listview_control(LVS_REPORT);
2785         ok(hwnd != NULL, "failed to create a listview window\n");
2786         style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2787         ok(!(style & LVS_OWNERDATA) && style, "LVS_OWNERDATA isn't expected\n");
2788
2789         flush_sequences(sequences, NUM_MSG_SEQUENCES);
2790
2791         ret = SetWindowLongPtrA(hwnd, GWL_STYLE, style | LVS_OWNERDATA);
2792         ok(ret == style, "Expected set GWL_STYLE to succeed\n");
2793         ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_ownerdata_switchto_seq,
2794                 "try to switch to LVS_OWNERDATA seq", FALSE);
2795
2796         style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2797         ok(!(style & LVS_OWNERDATA), "LVS_OWNERDATA isn't expected\n");
2798         DestroyWindow(hwnd);
2799     }
2800
2801     /* try to set LVS_OWNERDATA after creation just having it */
2802     hwnd = create_listview_control(LVS_OWNERDATA | LVS_REPORT);
2803     ok(hwnd != NULL, "failed to create a listview window\n");
2804     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2805     ok(style & LVS_OWNERDATA, "LVS_OWNERDATA is expected\n");
2806
2807     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2808
2809     ret = SetWindowLongPtrA(hwnd, GWL_STYLE, style | LVS_OWNERDATA);
2810     ok(ret == style, "Expected set GWL_STYLE to succeed\n");
2811     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_ownerdata_switchto_seq,
2812                 "try to switch to LVS_OWNERDATA seq", FALSE);
2813     DestroyWindow(hwnd);
2814
2815     /* try to remove LVS_OWNERDATA after creation just having it */
2816     if (g_is_below_5)
2817     {
2818         win_skip("remove LVS_OWNERDATA after creation leads to crash on < 5.80\n");
2819     }
2820     else
2821     {
2822         hwnd = create_listview_control(LVS_OWNERDATA | LVS_REPORT);
2823         ok(hwnd != NULL, "failed to create a listview window\n");
2824         style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2825         ok(style & LVS_OWNERDATA, "LVS_OWNERDATA is expected\n");
2826
2827         flush_sequences(sequences, NUM_MSG_SEQUENCES);
2828
2829         ret = SetWindowLongPtrA(hwnd, GWL_STYLE, style & ~LVS_OWNERDATA);
2830         ok(ret == style, "Expected set GWL_STYLE to succeed\n");
2831         ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_ownerdata_switchto_seq,
2832                 "try to switch to LVS_OWNERDATA seq", FALSE);
2833         style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2834         ok(style & LVS_OWNERDATA, "LVS_OWNERDATA is expected\n");
2835         DestroyWindow(hwnd);
2836     }
2837
2838     /* try select an item */
2839     hwnd = create_listview_control(LVS_OWNERDATA | LVS_REPORT);
2840     ok(hwnd != NULL, "failed to create a listview window\n");
2841     res = SendMessageA(hwnd, LVM_SETITEMCOUNT, 1, 0);
2842     expect(1, res);
2843     res = SendMessageA(hwnd, LVM_GETSELECTEDCOUNT, 0, 0);
2844     expect(0, res);
2845     memset(&item, 0, sizeof(item));
2846     item.stateMask = LVIS_SELECTED;
2847     item.state     = LVIS_SELECTED;
2848     res = SendMessageA(hwnd, LVM_SETITEMSTATE, 0, (LPARAM)&item);
2849     expect(TRUE, res);
2850     res = SendMessageA(hwnd, LVM_GETSELECTEDCOUNT, 0, 0);
2851     expect(1, res);
2852     res = SendMessageA(hwnd, LVM_GETITEMCOUNT, 0, 0);
2853     expect(1, res);
2854     DestroyWindow(hwnd);
2855
2856     /* LVM_SETITEM and LVM_SETITEMTEXT is unsupported on LVS_OWNERDATA */
2857     hwnd = create_listview_control(LVS_OWNERDATA | LVS_REPORT);
2858     ok(hwnd != NULL, "failed to create a listview window\n");
2859     res = SendMessageA(hwnd, LVM_SETITEMCOUNT, 1, 0);
2860     expect(1, res);
2861     res = SendMessageA(hwnd, LVM_GETITEMCOUNT, 0, 0);
2862     expect(1, res);
2863     memset(&item, 0, sizeof(item));
2864     item.mask = LVIF_STATE;
2865     item.iItem = 0;
2866     item.stateMask = LVIS_SELECTED;
2867     item.state     = LVIS_SELECTED;
2868     res = SendMessageA(hwnd, LVM_SETITEM, 0, (LPARAM)&item);
2869     expect(FALSE, res);
2870     memset(&item, 0, sizeof(item));
2871     item.pszText = test_str;
2872     res = SendMessageA(hwnd, LVM_SETITEMTEXT, 0, (LPARAM)&item);
2873     expect(FALSE, res);
2874     DestroyWindow(hwnd);
2875
2876     /* check notifications after focused/selected changed */
2877     hwnd = create_listview_control(LVS_OWNERDATA | LVS_REPORT);
2878     ok(hwnd != NULL, "failed to create a listview window\n");
2879     res = SendMessageA(hwnd, LVM_SETITEMCOUNT, 20, 0);
2880     expect(1, res);
2881
2882     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2883
2884     memset(&item, 0, sizeof(item));
2885     item.stateMask = LVIS_SELECTED;
2886     item.state     = LVIS_SELECTED;
2887     res = SendMessageA(hwnd, LVM_SETITEMSTATE, 0, (LPARAM)&item);
2888     expect(TRUE, res);
2889
2890     ok_sequence(sequences, PARENT_SEQ_INDEX, ownderdata_select_focus_parent_seq,
2891                 "ownerdata select notification", TRUE);
2892
2893     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2894
2895     memset(&item, 0, sizeof(item));
2896     item.stateMask = LVIS_FOCUSED;
2897     item.state     = LVIS_FOCUSED;
2898     res = SendMessageA(hwnd, LVM_SETITEMSTATE, 0, (LPARAM)&item);
2899     expect(TRUE, res);
2900
2901     ok_sequence(sequences, PARENT_SEQ_INDEX, ownderdata_select_focus_parent_seq,
2902                 "ownerdata focus notification", TRUE);
2903
2904     /* select all, check notifications */
2905     item.stateMask = LVIS_SELECTED;
2906     item.state     = 0;
2907     res = SendMessageA(hwnd, LVM_SETITEMSTATE, -1, (LPARAM)&item);
2908     expect(TRUE, res);
2909
2910     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2911
2912     item.stateMask = LVIS_SELECTED;
2913     item.state     = LVIS_SELECTED;
2914
2915     memset(&g_nmlistview, 0xcc, sizeof(g_nmlistview));
2916     res = SendMessageA(hwnd, LVM_SETITEMSTATE, -1, (LPARAM)&item);
2917     expect(TRUE, res);
2918     ok(g_nmlistview.iItem == -1, "got item %d\n", g_nmlistview.iItem);
2919     ok(g_nmlistview.iSubItem == 0, "got subitem %d\n", g_nmlistview.iSubItem);
2920     ok(g_nmlistview.uNewState == LVIS_SELECTED, "got new state 0x%08x\n", g_nmlistview.uNewState);
2921     ok(g_nmlistview.uOldState == 0, "got old state 0x%08x\n", g_nmlistview.uOldState);
2922     ok(g_nmlistview.uChanged == LVIF_STATE, "got changed 0x%08x\n", g_nmlistview.uChanged);
2923     ok(g_nmlistview.ptAction.x == 0 && g_nmlistview.ptAction.y == 0, "got wrong ptAction value\n");
2924     ok(g_nmlistview.lParam == 0, "got wrong lparam\n");
2925
2926     ok_sequence(sequences, PARENT_SEQ_INDEX, ownerdata_setstate_all_parent_seq,
2927                 "ownerdata select all notification", FALSE);
2928
2929     /* select all again, note that all items are selected already */
2930     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2931     item.stateMask = LVIS_SELECTED;
2932     item.state     = LVIS_SELECTED;
2933
2934     memset(&g_nmlistview, 0xcc, sizeof(g_nmlistview));
2935     res = SendMessageA(hwnd, LVM_SETITEMSTATE, -1, (LPARAM)&item);
2936     expect(TRUE, res);
2937     ok(g_nmlistview.iItem == -1, "got item %d\n", g_nmlistview.iItem);
2938     ok(g_nmlistview.iSubItem == 0, "got subitem %d\n", g_nmlistview.iSubItem);
2939     ok(g_nmlistview.uNewState == LVIS_SELECTED, "got new state 0x%08x\n", g_nmlistview.uNewState);
2940     ok(g_nmlistview.uOldState == 0, "got old state 0x%08x\n", g_nmlistview.uOldState);
2941     ok(g_nmlistview.uChanged == LVIF_STATE, "got changed 0x%08x\n", g_nmlistview.uChanged);
2942     ok(g_nmlistview.ptAction.x == 0 && g_nmlistview.ptAction.y == 0, "got wrong ptAction value\n");
2943     ok(g_nmlistview.lParam == 0, "got wrong lparam\n");
2944
2945     ok_sequence(sequences, PARENT_SEQ_INDEX, ownerdata_setstate_all_parent_seq,
2946                 "ownerdata select all notification", FALSE);
2947
2948     /* deselect all */
2949     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2950     item.stateMask = LVIS_SELECTED;
2951     item.state     = 0;
2952
2953     memset(&g_nmlistview, 0xcc, sizeof(g_nmlistview));
2954     res = SendMessageA(hwnd, LVM_SETITEMSTATE, -1, (LPARAM)&item);
2955     expect(TRUE, res);
2956     ok(g_nmlistview.iItem == -1, "got item %d\n", g_nmlistview.iItem);
2957     ok(g_nmlistview.iSubItem == 0, "got subitem %d\n", g_nmlistview.iSubItem);
2958     ok(g_nmlistview.uNewState == 0, "got new state 0x%08x\n", g_nmlistview.uNewState);
2959     ok(g_nmlistview.uOldState == LVIS_SELECTED, "got old state 0x%08x\n", g_nmlistview.uOldState);
2960     ok(g_nmlistview.uChanged == LVIF_STATE, "got changed 0x%08x\n", g_nmlistview.uChanged);
2961     ok(g_nmlistview.ptAction.x == 0 && g_nmlistview.ptAction.y == 0, "got wrong ptAction value\n");
2962     ok(g_nmlistview.lParam == 0, "got wrong lparam\n");
2963
2964     ok_sequence(sequences, PARENT_SEQ_INDEX, ownerdata_deselect_all_parent_seq,
2965                 "ownerdata deselect all notification", TRUE);
2966
2967     /* nothing selected, deselect all again */
2968     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2969     item.stateMask = LVIS_SELECTED;
2970     item.state     = 0;
2971
2972     res = SendMessageA(hwnd, LVM_SETITEMSTATE, -1, (LPARAM)&item);
2973     expect(TRUE, res);
2974
2975     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq, "ownerdata deselect all notification", TRUE);
2976
2977     /* select one, then deselect all */
2978     item.stateMask = LVIS_SELECTED;
2979     item.state     = LVIS_SELECTED;
2980     res = SendMessageA(hwnd, LVM_SETITEMSTATE, 0, (LPARAM)&item);
2981     expect(TRUE, res);
2982     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2983     item.stateMask = LVIS_SELECTED;
2984     item.state     = 0;
2985
2986     memset(&g_nmlistview, 0xcc, sizeof(g_nmlistview));
2987     res = SendMessageA(hwnd, LVM_SETITEMSTATE, -1, (LPARAM)&item);
2988     expect(TRUE, res);
2989     ok(g_nmlistview.iItem == -1, "got item %d\n", g_nmlistview.iItem);
2990     ok(g_nmlistview.iSubItem == 0, "got subitem %d\n", g_nmlistview.iSubItem);
2991     ok(g_nmlistview.uNewState == 0, "got new state 0x%08x\n", g_nmlistview.uNewState);
2992     ok(g_nmlistview.uOldState == LVIS_SELECTED, "got old state 0x%08x\n", g_nmlistview.uOldState);
2993     ok(g_nmlistview.uChanged == LVIF_STATE, "got changed 0x%08x\n", g_nmlistview.uChanged);
2994     ok(g_nmlistview.ptAction.x == 0 && g_nmlistview.ptAction.y == 0, "got wrong ptAction value\n");
2995     ok(g_nmlistview.lParam == 0, "got wrong lparam\n");
2996
2997     ok_sequence(sequences, PARENT_SEQ_INDEX, ownerdata_deselect_all_parent_seq,
2998                 "ownerdata select all notification", TRUE);
2999
3000     /* remove focused, try to focus all */
3001     item.stateMask = LVIS_FOCUSED;
3002     item.state     = LVIS_FOCUSED;
3003     res = SendMessageA(hwnd, LVM_SETITEMSTATE, 0, (LPARAM)&item);
3004     expect(TRUE, res);
3005     item.stateMask = LVIS_FOCUSED;
3006     item.state     = 0;
3007     res = SendMessageA(hwnd, LVM_SETITEMSTATE, -1, (LPARAM)&item);
3008     expect(TRUE, res);
3009     item.stateMask = LVIS_FOCUSED;
3010     res = SendMessageA(hwnd, LVM_GETITEMSTATE, 0, LVIS_FOCUSED);
3011     expect(0, res);
3012
3013     /* setting all to focused returns failure value */
3014     flush_sequences(sequences, NUM_MSG_SEQUENCES);
3015     item.stateMask = LVIS_FOCUSED;
3016     item.state     = LVIS_FOCUSED;
3017
3018     res = SendMessageA(hwnd, LVM_SETITEMSTATE, -1, (LPARAM)&item);
3019     expect(FALSE, res);
3020
3021     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq,
3022                 "ownerdata focus all notification", FALSE);
3023
3024     /* focus single item, remove all */
3025     item.stateMask = LVIS_FOCUSED;
3026     item.state     = LVIS_FOCUSED;
3027     res = SendMessage(hwnd, LVM_SETITEMSTATE, 0, (LPARAM)&item);
3028     expect(TRUE, res);
3029     flush_sequences(sequences, NUM_MSG_SEQUENCES);
3030     item.stateMask = LVIS_FOCUSED;
3031     item.state     = 0;
3032
3033     memset(&g_nmlistview, 0xcc, sizeof(g_nmlistview));
3034     res = SendMessageA(hwnd, LVM_SETITEMSTATE, -1, (LPARAM)&item);
3035     expect(TRUE, res);
3036     ok(g_nmlistview.iItem == -1, "got item %d\n", g_nmlistview.iItem);
3037     ok(g_nmlistview.iSubItem == 0, "got subitem %d\n", g_nmlistview.iSubItem);
3038     ok(g_nmlistview.uNewState == 0, "got new state 0x%08x\n", g_nmlistview.uNewState);
3039     ok(g_nmlistview.uOldState == LVIS_FOCUSED, "got old state 0x%08x\n", g_nmlistview.uOldState);
3040     ok(g_nmlistview.uChanged == LVIF_STATE, "got changed 0x%08x\n", g_nmlistview.uChanged);
3041     ok(g_nmlistview.ptAction.x == 0 && g_nmlistview.ptAction.y == 0, "got wrong ptAction value\n");
3042     ok(g_nmlistview.lParam == 0, "got wrong lparam\n");
3043
3044     ok_sequence(sequences, PARENT_SEQ_INDEX, ownerdata_defocus_all_parent_seq,
3045                 "ownerdata remove focus all notification", TRUE);
3046
3047     /* set all cut */
3048     flush_sequences(sequences, NUM_MSG_SEQUENCES);
3049     item.stateMask = LVIS_CUT;
3050     item.state     = LVIS_CUT;
3051
3052     memset(&g_nmlistview, 0xcc, sizeof(g_nmlistview));
3053     res = SendMessageA(hwnd, LVM_SETITEMSTATE, -1, (LPARAM)&item);
3054     expect(TRUE, res);
3055     ok(g_nmlistview.iItem == -1, "got item %d\n", g_nmlistview.iItem);
3056     ok(g_nmlistview.iSubItem == 0, "got subitem %d\n", g_nmlistview.iSubItem);
3057     ok(g_nmlistview.uNewState == LVIS_CUT, "got new state 0x%08x\n", g_nmlistview.uNewState);
3058     ok(g_nmlistview.uOldState == 0, "got old state 0x%08x\n", g_nmlistview.uOldState);
3059     ok(g_nmlistview.uChanged == LVIF_STATE, "got changed 0x%08x\n", g_nmlistview.uChanged);
3060     ok(g_nmlistview.ptAction.x == 0 && g_nmlistview.ptAction.y == 0, "got wrong ptAction value\n");
3061     ok(g_nmlistview.lParam == 0, "got wrong lparam\n");
3062
3063     ok_sequence(sequences, PARENT_SEQ_INDEX, ownerdata_setstate_all_parent_seq,
3064                 "ownerdata cut all notification", FALSE);
3065
3066     /* all marked cut, try again */
3067     flush_sequences(sequences, NUM_MSG_SEQUENCES);
3068     item.stateMask = LVIS_CUT;
3069     item.state     = LVIS_CUT;
3070
3071     memset(&g_nmlistview, 0xcc, sizeof(g_nmlistview));
3072     res = SendMessageA(hwnd, LVM_SETITEMSTATE, -1, (LPARAM)&item);
3073     expect(TRUE, res);
3074     ok(g_nmlistview.iItem == -1, "got item %d\n", g_nmlistview.iItem);
3075     ok(g_nmlistview.iSubItem == 0, "got subitem %d\n", g_nmlistview.iSubItem);
3076     ok(g_nmlistview.uNewState == LVIS_CUT, "got new state 0x%08x\n", g_nmlistview.uNewState);
3077     ok(g_nmlistview.uOldState == 0, "got old state 0x%08x\n", g_nmlistview.uOldState);
3078     ok(g_nmlistview.uChanged == LVIF_STATE, "got changed 0x%08x\n", g_nmlistview.uChanged);
3079     ok(g_nmlistview.ptAction.x == 0 && g_nmlistview.ptAction.y == 0, "got wrong ptAction value\n");
3080     ok(g_nmlistview.lParam == 0, "got wrong lparam\n");
3081
3082     ok_sequence(sequences, PARENT_SEQ_INDEX, ownerdata_setstate_all_parent_seq,
3083                 "ownerdata cut all notification #2", FALSE);
3084
3085     DestroyWindow(hwnd);
3086
3087     /* check notifications on LVM_GETITEM */
3088     /* zero callback mask */
3089     hwnd = create_listview_control(LVS_OWNERDATA | LVS_REPORT);
3090     ok(hwnd != NULL, "failed to create a listview window\n");
3091     res = SendMessageA(hwnd, LVM_SETITEMCOUNT, 1, 0);
3092     expect(1, res);
3093
3094     flush_sequences(sequences, NUM_MSG_SEQUENCES);
3095
3096     memset(&item, 0, sizeof(item));
3097     item.stateMask = LVIS_SELECTED;
3098     item.mask      = LVIF_STATE;
3099     res = SendMessageA(hwnd, LVM_GETITEMA, 0, (LPARAM)&item);
3100     expect(TRUE, res);
3101
3102     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq,
3103                 "ownerdata getitem selected state 1", FALSE);
3104
3105     /* non zero callback mask but not we asking for */
3106     res = SendMessageA(hwnd, LVM_SETCALLBACKMASK, LVIS_OVERLAYMASK, 0);
3107     expect(TRUE, res);
3108
3109     flush_sequences(sequences, NUM_MSG_SEQUENCES);
3110
3111     memset(&item, 0, sizeof(item));
3112     item.stateMask = LVIS_SELECTED;
3113     item.mask      = LVIF_STATE;
3114     res = SendMessageA(hwnd, LVM_GETITEMA, 0, (LPARAM)&item);
3115     expect(TRUE, res);
3116
3117     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq,
3118                 "ownerdata getitem selected state 2", FALSE);
3119
3120     /* LVIS_OVERLAYMASK callback mask, asking for index */
3121     flush_sequences(sequences, NUM_MSG_SEQUENCES);
3122
3123     memset(&item, 0, sizeof(item));
3124     item.stateMask = LVIS_OVERLAYMASK;
3125     item.mask      = LVIF_STATE;
3126     res = SendMessageA(hwnd, LVM_GETITEMA, 0, (LPARAM)&item);
3127     expect(TRUE, res);
3128
3129     ok_sequence(sequences, PARENT_SEQ_INDEX, single_getdispinfo_parent_seq,
3130                 "ownerdata getitem selected state 2", FALSE);
3131
3132     DestroyWindow(hwnd);
3133
3134     /* LVS_SORTASCENDING/LVS_SORTDESCENDING aren't compatible with LVS_OWNERDATA */
3135     hwnd = create_listview_control(LVS_OWNERDATA | LVS_SORTASCENDING | LVS_REPORT);
3136     ok(hwnd != NULL, "failed to create a listview window\n");
3137     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
3138     ok(style & LVS_OWNERDATA, "Expected LVS_OWNERDATA\n");
3139     ok(style & LVS_SORTASCENDING, "Expected LVS_SORTASCENDING to be set\n");
3140     SetWindowLongPtrA(hwnd, GWL_STYLE, style & ~LVS_SORTASCENDING);
3141     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
3142     ok(!(style & LVS_SORTASCENDING), "Expected LVS_SORTASCENDING not set\n");
3143     DestroyWindow(hwnd);
3144     /* apparently it's allowed to switch these style on after creation */
3145     hwnd = create_listview_control(LVS_OWNERDATA | LVS_REPORT);
3146     ok(hwnd != NULL, "failed to create a listview window\n");
3147     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
3148     ok(style & LVS_OWNERDATA, "Expected LVS_OWNERDATA\n");
3149     SetWindowLongPtrA(hwnd, GWL_STYLE, style | LVS_SORTASCENDING);
3150     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
3151     ok(style & LVS_SORTASCENDING, "Expected LVS_SORTASCENDING to be set\n");
3152     DestroyWindow(hwnd);
3153
3154     hwnd = create_listview_control(LVS_OWNERDATA | LVS_REPORT);
3155     ok(hwnd != NULL, "failed to create a listview window\n");
3156     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
3157     ok(style & LVS_OWNERDATA, "Expected LVS_OWNERDATA\n");
3158     SetWindowLongPtrA(hwnd, GWL_STYLE, style | LVS_SORTDESCENDING);
3159     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
3160     ok(style & LVS_SORTDESCENDING, "Expected LVS_SORTDESCENDING to be set\n");
3161     DestroyWindow(hwnd);
3162 }
3163
3164 static void test_norecompute(void)
3165 {
3166     static CHAR testA[] = "test";
3167     CHAR buff[10];
3168     LVITEMA item;
3169     HWND hwnd;
3170     DWORD res;
3171
3172     /* self containing control */
3173     hwnd = create_listview_control(LVS_REPORT);
3174     ok(hwnd != NULL, "failed to create a listview window\n");
3175     memset(&item, 0, sizeof(item));
3176     item.mask = LVIF_TEXT | LVIF_STATE;
3177     item.iItem = 0;
3178     item.stateMask = LVIS_SELECTED;
3179     item.state     = LVIS_SELECTED;
3180     item.pszText   = testA;
3181     res = SendMessageA(hwnd, LVM_INSERTITEM, 0, (LPARAM)&item);
3182     expect(0, res);
3183     /* retrieve with LVIF_NORECOMPUTE */
3184     item.mask  = LVIF_TEXT | LVIF_NORECOMPUTE;
3185     item.iItem = 0;
3186     item.pszText    = buff;
3187     item.cchTextMax = sizeof(buff)/sizeof(CHAR);
3188     res = SendMessageA(hwnd, LVM_GETITEM, 0, (LPARAM)&item);
3189     expect(TRUE, res);
3190     ok(lstrcmp(buff, testA) == 0, "Expected (%s), got (%s)\n", testA, buff);
3191
3192     item.mask = LVIF_TEXT;
3193     item.iItem = 1;
3194     item.pszText = LPSTR_TEXTCALLBACK;
3195     res = SendMessageA(hwnd, LVM_INSERTITEM, 0, (LPARAM)&item);
3196     expect(1, res);
3197
3198     item.mask  = LVIF_TEXT | LVIF_NORECOMPUTE;
3199     item.iItem = 1;
3200     item.pszText    = buff;
3201     item.cchTextMax = sizeof(buff)/sizeof(CHAR);
3202
3203     flush_sequences(sequences, NUM_MSG_SEQUENCES);
3204     res = SendMessageA(hwnd, LVM_GETITEM, 0, (LPARAM)&item);
3205     expect(TRUE, res);
3206     ok(item.pszText == LPSTR_TEXTCALLBACK, "Expected (%p), got (%p)\n",
3207        LPSTR_TEXTCALLBACK, (VOID*)item.pszText);
3208     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq, "retrieve with LVIF_NORECOMPUTE seq", FALSE);
3209
3210     DestroyWindow(hwnd);
3211
3212     /* LVS_OWNERDATA */
3213     hwnd = create_listview_control(LVS_OWNERDATA | LVS_REPORT);
3214     ok(hwnd != NULL, "failed to create a listview window\n");
3215
3216     item.mask = LVIF_STATE;
3217     item.stateMask = LVIS_SELECTED;
3218     item.state     = LVIS_SELECTED;
3219     item.iItem = 0;
3220     res = SendMessageA(hwnd, LVM_INSERTITEM, 0, (LPARAM)&item);
3221     expect(0, res);
3222
3223     item.mask  = LVIF_TEXT | LVIF_NORECOMPUTE;
3224     item.iItem = 0;
3225     item.pszText    = buff;
3226     item.cchTextMax = sizeof(buff)/sizeof(CHAR);
3227     flush_sequences(sequences, NUM_MSG_SEQUENCES);
3228     res = SendMessageA(hwnd, LVM_GETITEM, 0, (LPARAM)&item);
3229     expect(TRUE, res);
3230     ok(item.pszText == LPSTR_TEXTCALLBACK, "Expected (%p), got (%p)\n",
3231        LPSTR_TEXTCALLBACK, (VOID*)item.pszText);
3232     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq, "retrieve with LVIF_NORECOMPUTE seq 2", FALSE);
3233
3234     DestroyWindow(hwnd);
3235 }
3236
3237 static void test_nosortheader(void)
3238 {
3239     HWND hwnd, header;
3240     LONG_PTR style;
3241
3242     hwnd = create_listview_control(LVS_REPORT);
3243     ok(hwnd != NULL, "failed to create a listview window\n");
3244
3245     header = (HWND)SendMessageA(hwnd, LVM_GETHEADER, 0, 0);
3246     ok(IsWindow(header), "header expected\n");
3247
3248     style = GetWindowLongPtr(header, GWL_STYLE);
3249     ok(style & HDS_BUTTONS, "expected header to have HDS_BUTTONS\n");
3250
3251     style = GetWindowLongPtr(hwnd, GWL_STYLE);
3252     SetWindowLongPtr(hwnd, GWL_STYLE, style | LVS_NOSORTHEADER);
3253     /* HDS_BUTTONS retained */
3254     style = GetWindowLongPtr(header, GWL_STYLE);
3255     ok(style & HDS_BUTTONS, "expected header to retain HDS_BUTTONS\n");
3256
3257     DestroyWindow(hwnd);
3258
3259     /* create with LVS_NOSORTHEADER */
3260     hwnd = create_listview_control(LVS_NOSORTHEADER | LVS_REPORT);
3261     ok(hwnd != NULL, "failed to create a listview window\n");
3262
3263     header = (HWND)SendMessageA(hwnd, LVM_GETHEADER, 0, 0);
3264     ok(IsWindow(header), "header expected\n");
3265
3266     style = GetWindowLongPtr(header, GWL_STYLE);
3267     ok(!(style & HDS_BUTTONS), "expected header to have no HDS_BUTTONS\n");
3268
3269     style = GetWindowLongPtr(hwnd, GWL_STYLE);
3270     SetWindowLongPtr(hwnd, GWL_STYLE, style & ~LVS_NOSORTHEADER);
3271     /* not changed here */
3272     style = GetWindowLongPtr(header, GWL_STYLE);
3273     ok(!(style & HDS_BUTTONS), "expected header to have no HDS_BUTTONS\n");
3274
3275     DestroyWindow(hwnd);
3276 }
3277
3278 static void test_setredraw(void)
3279 {
3280     HWND hwnd;
3281     DWORD_PTR style;
3282     DWORD ret;
3283     HDC hdc;
3284     RECT rect;
3285
3286     hwnd = create_listview_control(LVS_OWNERDATA | LVS_REPORT);
3287     ok(hwnd != NULL, "failed to create a listview window\n");
3288
3289     /* Passing WM_SETREDRAW to DefWinProc removes WS_VISIBLE.
3290        ListView seems to handle it internally without DefWinProc */
3291
3292     /* default value first */
3293     ret = SendMessage(hwnd, WM_SETREDRAW, TRUE, 0);
3294     expect(0, ret);
3295     /* disable */
3296     style = GetWindowLongPtr(hwnd, GWL_STYLE);
3297     ok(style & WS_VISIBLE, "Expected WS_VISIBLE to be set\n");
3298     ret = SendMessage(hwnd, WM_SETREDRAW, FALSE, 0);
3299     expect(0, ret);
3300     style = GetWindowLongPtr(hwnd, GWL_STYLE);
3301     ok(style & WS_VISIBLE, "Expected WS_VISIBLE to be set\n");
3302     ret = SendMessage(hwnd, WM_SETREDRAW, TRUE, 0);
3303     expect(0, ret);
3304
3305     /* check update rect after redrawing */
3306     ret = SendMessage(hwnd, WM_SETREDRAW, FALSE, 0);
3307     expect(0, ret);
3308     InvalidateRect(hwnd, NULL, FALSE);
3309     RedrawWindow(hwnd, NULL, NULL, RDW_UPDATENOW);
3310     rect.right = rect.bottom = 1;
3311     GetUpdateRect(hwnd, &rect, FALSE);
3312     expect(0, rect.right);
3313     expect(0, rect.bottom);
3314
3315     /* WM_ERASEBKGND */
3316     hdc = GetWindowDC(hwndparent);
3317     ret = SendMessage(hwnd, WM_ERASEBKGND, (WPARAM)hdc, 0);
3318     expect(TRUE, ret);
3319     ret = SendMessage(hwnd, WM_SETREDRAW, FALSE, 0);
3320     expect(0, ret);
3321     ret = SendMessage(hwnd, WM_ERASEBKGND, (WPARAM)hdc, 0);
3322     expect(TRUE, ret);
3323     ret = SendMessage(hwnd, WM_SETREDRAW, TRUE, 0);
3324     expect(0, ret);
3325     ReleaseDC(hwndparent, hdc);
3326
3327     /* check notification messages to show that repainting is disabled */
3328     ret = SendMessage(hwnd, LVM_SETITEMCOUNT, 1, 0);
3329     expect(TRUE, ret);
3330     ret = SendMessage(hwnd, WM_SETREDRAW, FALSE, 0);
3331     expect(0, ret);
3332     flush_sequences(sequences, NUM_MSG_SEQUENCES);
3333
3334     InvalidateRect(hwnd, NULL, TRUE);
3335     UpdateWindow(hwnd);
3336     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq,
3337                 "redraw after WM_SETREDRAW (FALSE)", FALSE);
3338
3339     ret = SendMessage(hwnd, LVM_SETBKCOLOR, 0, CLR_NONE);
3340     expect(TRUE, ret);
3341     flush_sequences(sequences, NUM_MSG_SEQUENCES);
3342     InvalidateRect(hwnd, NULL, TRUE);
3343     UpdateWindow(hwnd);
3344     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq,
3345                 "redraw after WM_SETREDRAW (FALSE) with CLR_NONE bkgnd", FALSE);
3346
3347     /* message isn't forwarded to header */
3348     subclass_header(hwnd);
3349     flush_sequences(sequences, NUM_MSG_SEQUENCES);
3350     ret = SendMessage(hwnd, WM_SETREDRAW, FALSE, 0);
3351     expect(0, ret);
3352     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, setredraw_seq,
3353                 "WM_SETREDRAW: not forwarded to header", FALSE);
3354
3355     DestroyWindow(hwnd);
3356 }
3357
3358 static void test_hittest(void)
3359 {
3360     HWND hwnd;
3361     DWORD r;
3362     RECT bounds;
3363     LVITEMA item;
3364     static CHAR text[] = "1234567890ABCDEFGHIJKLMNOPQRST";
3365     POINT pos;
3366     INT x, y, i;
3367     WORD vert;
3368     HIMAGELIST himl, himl2;
3369     HBITMAP hbmp;
3370
3371     hwnd = create_listview_control(LVS_REPORT);
3372     ok(hwnd != NULL, "failed to create a listview window\n");
3373
3374     /* LVS_REPORT with a single subitem (2 columns) */
3375     insert_column(hwnd, 0);
3376     insert_column(hwnd, 1);
3377     insert_item(hwnd, 0);
3378
3379     item.iSubItem = 0;
3380     /* the only purpose of that line is to be as long as a half item rect */
3381     item.pszText  = text;
3382     r = SendMessage(hwnd, LVM_SETITEMTEXT, 0, (LPARAM)&item);
3383     expect(TRUE, r);
3384
3385     r = SendMessage(hwnd, LVM_SETCOLUMNWIDTH, 0, MAKELPARAM(100, 0));
3386     expect(TRUE, r);
3387     r = SendMessage(hwnd, LVM_SETCOLUMNWIDTH, 1, MAKELPARAM(100, 0));
3388     expect(TRUE, r);
3389
3390     memset(&bounds, 0, sizeof(bounds));
3391     bounds.left = LVIR_BOUNDS;
3392     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&bounds);
3393     expect(1, r);
3394     ok(bounds.bottom - bounds.top > 0, "Expected non zero item height\n");
3395     ok(bounds.right - bounds.left > 0, "Expected non zero item width\n");
3396     r = SendMessage(hwnd, LVM_GETITEMSPACING, TRUE, 0);
3397     vert = HIWORD(r);
3398     ok(bounds.bottom - bounds.top == vert,
3399         "Vertical spacing inconsistent (%d != %d)\n", bounds.bottom - bounds.top, vert);
3400     r = SendMessage(hwnd, LVM_GETITEMPOSITION, 0, (LPARAM)&pos);
3401     expect(TRUE, r);
3402
3403     /* LVS_EX_FULLROWSELECT not set, no icons attached */
3404
3405     /* outside columns by x position - valid is [0, 199] */
3406     x = -1;
3407     y = pos.y + (bounds.bottom - bounds.top) / 2;
3408     test_lvm_hittest(hwnd, x, y, -1, LVHT_TOLEFT, 0, FALSE, FALSE);
3409     test_lvm_subitemhittest(hwnd, x, y, -1, -1, LVHT_NOWHERE, FALSE, FALSE, FALSE);
3410
3411     x = pos.x + 50; /* column half width */
3412     y = pos.y + (bounds.bottom - bounds.top) / 2;
3413     test_lvm_hittest(hwnd, x, y, 0, LVHT_ONITEMLABEL, 0, FALSE, FALSE);
3414     test_lvm_subitemhittest(hwnd, x, y, 0, 0, LVHT_ONITEMLABEL, FALSE, FALSE, FALSE);
3415     x = pos.x + 150; /* outside column */
3416     y = pos.y + (bounds.bottom - bounds.top) / 2;
3417     test_lvm_hittest(hwnd, x, y, -1, LVHT_TORIGHT, 0, FALSE, FALSE);
3418     test_lvm_subitemhittest(hwnd, x, y, 0, 1, LVHT_ONITEMLABEL, FALSE, FALSE, FALSE);
3419     y = (bounds.bottom - bounds.top) / 2;
3420     test_lvm_hittest(hwnd, x, y, -1, LVHT_TORIGHT, 0, FALSE, TRUE);
3421     test_lvm_subitemhittest(hwnd, x, y, 0, 1, LVHT_ONITEMLABEL, FALSE, FALSE, FALSE);
3422     /* outside possible client rectangle (to right) */
3423     x = pos.x + 500;
3424     y = pos.y + (bounds.bottom - bounds.top) / 2;
3425     test_lvm_hittest(hwnd, x, y, -1, LVHT_TORIGHT, 0, FALSE, FALSE);
3426     test_lvm_subitemhittest(hwnd, x, y, -1, -1, LVHT_NOWHERE, FALSE, FALSE, FALSE);
3427     y = (bounds.bottom - bounds.top) / 2;
3428     test_lvm_hittest(hwnd, x, y, -1, LVHT_TORIGHT, 0, FALSE, TRUE);
3429     test_lvm_subitemhittest(hwnd, x, y, -1, -1, LVHT_NOWHERE, FALSE, FALSE, FALSE);
3430     /* subitem returned with -1 item too */
3431     x = pos.x + 150;
3432     y = bounds.top - vert;
3433     test_lvm_subitemhittest(hwnd, x, y, -1, 1, LVHT_NOWHERE, FALSE, FALSE, FALSE);
3434     test_lvm_subitemhittest(hwnd, x, y - vert + 1, -1, 1, LVHT_NOWHERE, FALSE, FALSE, FALSE);
3435     /* return values appear to underflow with negative indices */
3436     i = -2;
3437     y = y - vert;
3438     while (i > -10) {
3439         test_lvm_subitemhittest(hwnd, x, y, i, 1, LVHT_ONITEMLABEL, TRUE, FALSE, TRUE);
3440         test_lvm_subitemhittest(hwnd, x, y - vert + 1, i, 1, LVHT_ONITEMLABEL, TRUE, FALSE, TRUE);
3441         y = y - vert;
3442         i--;
3443     }
3444     /* parent client area is 100x100 by default */
3445     MoveWindow(hwnd, 0, 0, 300, 100, FALSE);
3446     x = pos.x + 150; /* outside column */
3447     y = pos.y + (bounds.bottom - bounds.top) / 2;
3448     test_lvm_hittest(hwnd, x, y, -1, LVHT_NOWHERE, 0, FALSE, FALSE);
3449     test_lvm_subitemhittest(hwnd, x, y, 0, 1, LVHT_ONITEMLABEL, FALSE, FALSE, FALSE);
3450     y = (bounds.bottom - bounds.top) / 2;
3451     test_lvm_hittest(hwnd, x, y, -1, LVHT_NOWHERE, 0, FALSE, TRUE);
3452     test_lvm_subitemhittest(hwnd, x, y, 0, 1, LVHT_ONITEMLABEL, FALSE, FALSE, FALSE);
3453     /* the same with LVS_EX_FULLROWSELECT */
3454     SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);
3455     x = pos.x + 150; /* outside column */
3456     y = pos.y + (bounds.bottom - bounds.top) / 2;
3457     test_lvm_hittest(hwnd, x, y, 0, LVHT_ONITEM, LVHT_ONITEMLABEL, FALSE, FALSE);
3458     test_lvm_subitemhittest(hwnd, x, y, 0, 1, LVHT_ONITEMLABEL, FALSE, FALSE, FALSE);
3459     y = (bounds.bottom - bounds.top) / 2;
3460     test_lvm_subitemhittest(hwnd, x, y, 0, 1, LVHT_ONITEMLABEL, FALSE, FALSE, FALSE);
3461     MoveWindow(hwnd, 0, 0, 100, 100, FALSE);
3462     x = pos.x + 150; /* outside column */
3463     y = pos.y + (bounds.bottom - bounds.top) / 2;
3464     test_lvm_hittest(hwnd, x, y, -1, LVHT_TORIGHT, 0, FALSE, FALSE);
3465     test_lvm_subitemhittest(hwnd, x, y, 0, 1, LVHT_ONITEMLABEL, FALSE, FALSE, FALSE);
3466     y = (bounds.bottom - bounds.top) / 2;
3467     test_lvm_hittest(hwnd, x, y, -1, LVHT_TORIGHT, 0, FALSE, TRUE);
3468     test_lvm_subitemhittest(hwnd, x, y, 0, 1, LVHT_ONITEMLABEL, FALSE, FALSE, FALSE);
3469     /* outside possible client rectangle (to right) */
3470     x = pos.x + 500;
3471     y = pos.y + (bounds.bottom - bounds.top) / 2;
3472     test_lvm_hittest(hwnd, x, y, -1, LVHT_TORIGHT, 0, FALSE, FALSE);
3473     test_lvm_subitemhittest(hwnd, x, y, -1, -1, LVHT_NOWHERE, FALSE, FALSE, FALSE);
3474     y = (bounds.bottom - bounds.top) / 2;
3475     test_lvm_hittest(hwnd, x, y, -1, LVHT_TORIGHT, 0, FALSE, TRUE);
3476     test_lvm_subitemhittest(hwnd, x, y, -1, -1, LVHT_NOWHERE, FALSE, FALSE, FALSE);
3477     /* try with icons, state icons index is 1 based so at least 2 bitmaps needed */
3478     himl = ImageList_Create(16, 16, 0, 4, 4);
3479     ok(himl != NULL, "failed to create imagelist\n");
3480     hbmp = CreateBitmap(16, 16, 1, 1, NULL);
3481     ok(hbmp != NULL, "failed to create bitmap\n");
3482     r = ImageList_Add(himl, hbmp, 0);
3483     ok(r == 0, "should be zero\n");
3484     hbmp = CreateBitmap(16, 16, 1, 1, NULL);
3485     ok(hbmp != NULL, "failed to create bitmap\n");
3486     r = ImageList_Add(himl, hbmp, 0);
3487     ok(r == 1, "should be one\n");
3488
3489     r = SendMessage(hwnd, LVM_SETIMAGELIST, LVSIL_STATE, (LPARAM)himl);
3490     expect(0, r);
3491
3492     item.mask = LVIF_IMAGE;
3493     item.iImage = 0;
3494     item.iItem = 0;
3495     item.iSubItem = 0;
3496     r = SendMessage(hwnd, LVM_SETITEM, 0, (LPARAM)&item);
3497     expect(TRUE, r);
3498     /* on state icon */
3499     x = pos.x + 8;
3500     y = pos.y + (bounds.bottom - bounds.top) / 2;
3501     test_lvm_hittest(hwnd, x, y, 0, LVHT_ONITEMSTATEICON, 0, FALSE, FALSE);
3502     test_lvm_subitemhittest(hwnd, x, y, 0, 0, LVHT_ONITEMSTATEICON, FALSE, FALSE, FALSE);
3503     y = (bounds.bottom - bounds.top) / 2;
3504     test_lvm_subitemhittest(hwnd, x, y, 0, 0, LVHT_ONITEMSTATEICON, FALSE, FALSE, FALSE);
3505
3506     /* state icons indices are 1 based, check with valid index */
3507     item.mask = LVIF_STATE;
3508     item.state = INDEXTOSTATEIMAGEMASK(1);
3509     item.stateMask = LVIS_STATEIMAGEMASK;
3510     item.iItem = 0;
3511     item.iSubItem = 0;
3512     r = SendMessage(hwnd, LVM_SETITEM, 0, (LPARAM)&item);
3513     expect(TRUE, r);
3514     /* on state icon */
3515     x = pos.x + 8;
3516     y = pos.y + (bounds.bottom - bounds.top) / 2;
3517     test_lvm_hittest(hwnd, x, y, 0, LVHT_ONITEMSTATEICON, 0, FALSE, FALSE);
3518     test_lvm_subitemhittest(hwnd, x, y, 0, 0, LVHT_ONITEMSTATEICON, FALSE, FALSE, FALSE);
3519     y = (bounds.bottom - bounds.top) / 2;
3520     test_lvm_subitemhittest(hwnd, x, y, 0, 0, LVHT_ONITEMSTATEICON, FALSE, FALSE, FALSE);
3521
3522     himl2 = (HIMAGELIST)SendMessage(hwnd, LVM_SETIMAGELIST, LVSIL_STATE, 0);
3523     ok(himl2 == himl, "should return handle\n");
3524
3525     r = SendMessage(hwnd, LVM_SETIMAGELIST, LVSIL_SMALL, (LPARAM)himl);
3526     expect(0, r);
3527     /* on item icon */
3528     x = pos.x + 8;
3529     y = pos.y + (bounds.bottom - bounds.top) / 2;
3530     test_lvm_hittest(hwnd, x, y, 0, LVHT_ONITEMICON, 0, FALSE, FALSE);
3531     test_lvm_subitemhittest(hwnd, x, y, 0, 0, LVHT_ONITEMICON, FALSE, FALSE, FALSE);
3532     y = (bounds.bottom - bounds.top) / 2;
3533     test_lvm_subitemhittest(hwnd, x, y, 0, 0, LVHT_ONITEMICON, FALSE, FALSE, FALSE);
3534
3535     DestroyWindow(hwnd);
3536 }
3537
3538 static void test_getviewrect(void)
3539 {
3540     HWND hwnd;
3541     DWORD r;
3542     RECT rect;
3543     LVITEMA item;
3544
3545     hwnd = create_listview_control(LVS_REPORT);
3546     ok(hwnd != NULL, "failed to create a listview window\n");
3547
3548     /* empty */
3549     r = SendMessage(hwnd, LVM_GETVIEWRECT, 0, (LPARAM)&rect);
3550     expect(TRUE, r);
3551
3552     insert_column(hwnd, 0);
3553     insert_column(hwnd, 1);
3554
3555     memset(&item, 0, sizeof(item));
3556     item.iItem = 0;
3557     item.iSubItem = 0;
3558     SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM)&item);
3559
3560     r = SendMessage(hwnd, LVM_SETCOLUMNWIDTH, 0, MAKELPARAM(100, 0));
3561     expect(TRUE, r);
3562     r = SendMessage(hwnd, LVM_SETCOLUMNWIDTH, 1, MAKELPARAM(120, 0));
3563     expect(TRUE, r);
3564
3565     rect.left = rect.right = rect.top = rect.bottom = -1;
3566     r = SendMessage(hwnd, LVM_GETVIEWRECT, 0, (LPARAM)&rect);
3567     expect(TRUE, r);
3568     /* left is set to (2e31-1) - XP SP2 */
3569     expect(0, rect.right);
3570     expect(0, rect.top);
3571     expect(0, rect.bottom);
3572
3573     /* switch to LVS_ICON */
3574     SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~LVS_REPORT);
3575
3576     rect.left = rect.right = rect.top = rect.bottom = -1;
3577     r = SendMessage(hwnd, LVM_GETVIEWRECT, 0, (LPARAM)&rect);
3578     expect(TRUE, r);
3579     expect(0, rect.left);
3580     expect(0, rect.top);
3581     /* precise value differs for 2k, XP and Vista */
3582     ok(rect.bottom > 0, "Expected positive bottom value, got %d\n", rect.bottom);
3583     ok(rect.right  > 0, "Expected positive right value, got %d\n", rect.right);
3584
3585     DestroyWindow(hwnd);
3586 }
3587
3588 static void test_getitemposition(void)
3589 {
3590     HWND hwnd, header;
3591     DWORD r;
3592     POINT pt;
3593     RECT rect;
3594
3595     hwnd = create_listview_control(LVS_REPORT);
3596     ok(hwnd != NULL, "failed to create a listview window\n");
3597     header = subclass_header(hwnd);
3598
3599     /* LVS_REPORT, single item, no columns added */
3600     insert_item(hwnd, 0);
3601
3602     flush_sequences(sequences, NUM_MSG_SEQUENCES);
3603
3604     pt.x = pt.y = -1;
3605     r = SendMessage(hwnd, LVM_GETITEMPOSITION, 0, (LPARAM)&pt);
3606     expect(TRUE, r);
3607     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, getitemposition_seq1, "get item position 1", FALSE);
3608
3609     /* LVS_REPORT, single item, single column */
3610     insert_column(hwnd, 0);
3611
3612     flush_sequences(sequences, NUM_MSG_SEQUENCES);
3613
3614     pt.x = pt.y = -1;
3615     r = SendMessage(hwnd, LVM_GETITEMPOSITION, 0, (LPARAM)&pt);
3616     expect(TRUE, r);
3617     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, getitemposition_seq2, "get item position 2", TRUE);
3618
3619     memset(&rect, 0, sizeof(rect));
3620     SendMessage(header, HDM_GETITEMRECT, 0, (LPARAM)&rect);
3621     /* some padding? */
3622     expect(2, pt.x);
3623     /* offset by header height */
3624     expect(rect.bottom - rect.top, pt.y);
3625
3626     DestroyWindow(hwnd);
3627 }
3628
3629 static void test_columnscreation(void)
3630 {
3631     HWND hwnd, header;
3632     DWORD r;
3633
3634     hwnd = create_listview_control(LVS_REPORT);
3635     ok(hwnd != NULL, "failed to create a listview window\n");
3636
3637     insert_item(hwnd, 0);
3638
3639     /* headers columns aren't created automatically */
3640     header = (HWND)SendMessage(hwnd, LVM_GETHEADER, 0, 0);
3641     ok(IsWindow(header), "Expected header handle\n");
3642     r = SendMessage(header, HDM_GETITEMCOUNT, 0, 0);
3643     expect(0, r);
3644
3645     DestroyWindow(hwnd);
3646 }
3647
3648 static void test_getitemrect(void)
3649 {
3650     HWND hwnd;
3651     HIMAGELIST himl, himl_ret;
3652     HBITMAP hbm;
3653     RECT rect;
3654     DWORD r;
3655     LVITEMA item;
3656     LVCOLUMNA col;
3657     INT order[2];
3658     POINT pt;
3659     HDC hdc;
3660
3661     /* rectangle isn't empty for empty text items */
3662     hwnd = create_listview_control(LVS_LIST);
3663     memset(&item, 0, sizeof(item));
3664     item.mask = 0;
3665     item.iItem = 0;
3666     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM)&item);
3667     expect(0, r);
3668     rect.left = LVIR_LABEL;
3669     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
3670     expect(TRUE, r);
3671     expect(0, rect.left);
3672     expect(0, rect.top);
3673     hdc = GetDC(hwnd);
3674     todo_wine expect(((GetDeviceCaps(hdc, LOGPIXELSX) + 15) / 16) * 16, rect.right);
3675     ReleaseDC(hwnd, hdc);
3676     DestroyWindow(hwnd);
3677
3678     hwnd = create_listview_control(LVS_REPORT);
3679     ok(hwnd != NULL, "failed to create a listview window\n");
3680
3681     /* empty item */
3682     memset(&item, 0, sizeof(item));
3683     item.iItem = 0;
3684     item.iSubItem = 0;
3685     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM)&item);
3686     expect(0, r);
3687
3688     rect.left = LVIR_BOUNDS;
3689     rect.right = rect.top = rect.bottom = -1;
3690     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
3691     expect(TRUE, r);
3692
3693     /* zero width rectangle with no padding */
3694     expect(0, rect.left);
3695     expect(0, rect.right);
3696
3697     insert_column(hwnd, 0);
3698     insert_column(hwnd, 1);
3699
3700     col.mask = LVCF_WIDTH;
3701     col.cx   = 50;
3702     r = SendMessage(hwnd, LVM_SETCOLUMN, 0, (LPARAM)&col);
3703     expect(TRUE, r);
3704
3705     col.mask = LVCF_WIDTH;
3706     col.cx   = 100;
3707     r = SendMessage(hwnd, LVM_SETCOLUMN, 1, (LPARAM)&col);
3708     expect(TRUE, r);
3709
3710     rect.left = LVIR_BOUNDS;
3711     rect.right = rect.top = rect.bottom = -1;
3712     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
3713     expect(TRUE, r);
3714
3715     /* still no left padding */
3716     expect(0, rect.left);
3717     expect(150, rect.right);
3718
3719     rect.left = LVIR_SELECTBOUNDS;
3720     rect.right = rect.top = rect.bottom = -1;
3721     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
3722     expect(TRUE, r);
3723     /* padding */
3724     expect(2, rect.left);
3725
3726     rect.left = LVIR_LABEL;
3727     rect.right = rect.top = rect.bottom = -1;
3728     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
3729     expect(TRUE, r);
3730     /* padding, column width */
3731     expect(2, rect.left);
3732     expect(50, rect.right);
3733
3734     /* no icons attached */
3735     rect.left = LVIR_ICON;
3736     rect.right = rect.top = rect.bottom = -1;
3737     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
3738     expect(TRUE, r);
3739     /* padding */
3740     expect(2, rect.left);
3741     expect(2, rect.right);
3742
3743     /* change order */
3744     order[0] = 1; order[1] = 0;
3745     r = SendMessage(hwnd, LVM_SETCOLUMNORDERARRAY, 2, (LPARAM)&order);
3746     expect(TRUE, r);
3747     pt.x = -1;
3748     r = SendMessage(hwnd, LVM_GETITEMPOSITION, 0, (LPARAM)&pt);
3749     expect(TRUE, r);
3750     /* 1 indexed column width + padding */
3751     expect(102, pt.x);
3752     /* rect is at zero too */
3753     rect.left = LVIR_BOUNDS;
3754     rect.right = rect.top = rect.bottom = -1;
3755     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
3756     expect(TRUE, r);
3757     expect(0, rect.left);
3758     /* just width sum */
3759     expect(150, rect.right);
3760
3761     rect.left = LVIR_SELECTBOUNDS;
3762     rect.right = rect.top = rect.bottom = -1;
3763     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
3764     expect(TRUE, r);
3765     /* column width + padding */
3766     expect(102, rect.left);
3767
3768     /* back to initial order */
3769     order[0] = 0; order[1] = 1;
3770     r = SendMessage(hwnd, LVM_SETCOLUMNORDERARRAY, 2, (LPARAM)&order);
3771     expect(TRUE, r);
3772
3773     /* state icons */
3774     himl = ImageList_Create(16, 16, 0, 2, 2);
3775     ok(himl != NULL, "failed to create imagelist\n");
3776     hbm = CreateBitmap(16, 16, 1, 1, NULL);
3777     ok(hbm != NULL, "failed to create bitmap\n");
3778     r = ImageList_Add(himl, hbm, 0);
3779     expect(0, r);
3780     hbm = CreateBitmap(16, 16, 1, 1, NULL);
3781     ok(hbm != NULL, "failed to create bitmap\n");
3782     r = ImageList_Add(himl, hbm, 0);
3783     expect(1, r);
3784
3785     r = SendMessage(hwnd, LVM_SETIMAGELIST, LVSIL_STATE, (LPARAM)himl);
3786     expect(0, r);
3787
3788     item.mask = LVIF_STATE;
3789     item.state = INDEXTOSTATEIMAGEMASK(1);
3790     item.stateMask = LVIS_STATEIMAGEMASK;
3791     item.iItem = 0;
3792     item.iSubItem = 0;
3793     r = SendMessage(hwnd, LVM_SETITEM, 0, (LPARAM)&item);
3794     expect(TRUE, r);
3795
3796     /* icon bounds */
3797     rect.left = LVIR_ICON;
3798     rect.right = rect.top = rect.bottom = -1;
3799     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
3800     expect(TRUE, r);
3801     /* padding + stateicon width */
3802     expect(18, rect.left);
3803     expect(18, rect.right);
3804     /* label bounds */
3805     rect.left = LVIR_LABEL;
3806     rect.right = rect.top = rect.bottom = -1;
3807     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
3808     expect(TRUE, r);
3809     /* padding + stateicon width -> column width */
3810     expect(18, rect.left);
3811     expect(50, rect.right);
3812
3813     himl_ret = (HIMAGELIST)SendMessage(hwnd, LVM_SETIMAGELIST, LVSIL_STATE, 0);
3814     ok(himl_ret == himl, "got %p, expected %p\n", himl_ret, himl);
3815
3816     r = SendMessage(hwnd, LVM_SETIMAGELIST, LVSIL_SMALL, (LPARAM)himl);
3817     expect(0, r);
3818
3819     item.mask = LVIF_STATE | LVIF_IMAGE;
3820     item.iImage = 1;
3821     item.state = 0;
3822     item.stateMask = ~0;
3823     item.iItem = 0;
3824     item.iSubItem = 0;
3825     r = SendMessage(hwnd, LVM_SETITEM, 0, (LPARAM)&item);
3826     expect(TRUE, r);
3827
3828     /* icon bounds */
3829     rect.left = LVIR_ICON;
3830     rect.right = rect.top = rect.bottom = -1;
3831     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
3832     expect(TRUE, r);
3833     /* padding, icon width */
3834     expect(2, rect.left);
3835     expect(18, rect.right);
3836     /* label bounds */
3837     rect.left = LVIR_LABEL;
3838     rect.right = rect.top = rect.bottom = -1;
3839     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
3840     expect(TRUE, r);
3841     /* padding + icon width -> column width */
3842     expect(18, rect.left);
3843     expect(50, rect.right);
3844
3845     /* select bounds */
3846     rect.left = LVIR_SELECTBOUNDS;
3847     rect.right = rect.top = rect.bottom = -1;
3848     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
3849     expect(TRUE, r);
3850     /* padding, column width */
3851     expect(2, rect.left);
3852     expect(50, rect.right);
3853
3854     /* try with indentation */
3855     item.mask = LVIF_INDENT;
3856     item.iIndent = 1;
3857     item.iItem = 0;
3858     item.iSubItem = 0;
3859     r = SendMessage(hwnd, LVM_SETITEM, 0, (LPARAM)&item);
3860     expect(TRUE, r);
3861
3862     /* bounds */
3863     rect.left = LVIR_BOUNDS;
3864     rect.right = rect.top = rect.bottom = -1;
3865     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
3866     expect(TRUE, r);
3867     /* padding + 1 icon width, column width */
3868     expect(0, rect.left);
3869     expect(150, rect.right);
3870
3871     /* select bounds */
3872     rect.left = LVIR_SELECTBOUNDS;
3873     rect.right = rect.top = rect.bottom = -1;
3874     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
3875     expect(TRUE, r);
3876     /* padding + 1 icon width, column width */
3877     expect(2 + 16, rect.left);
3878     expect(50, rect.right);
3879
3880     /* label bounds */
3881     rect.left = LVIR_LABEL;
3882     rect.right = rect.top = rect.bottom = -1;
3883     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
3884     expect(TRUE, r);
3885     /* padding + 2 icon widths, column width */
3886     expect(2 + 16*2, rect.left);
3887     expect(50, rect.right);
3888
3889     /* icon bounds */
3890     rect.left = LVIR_ICON;
3891     rect.right = rect.top = rect.bottom = -1;
3892     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
3893     expect(TRUE, r);
3894     /* padding + 1 icon width indentation, icon width */
3895     expect(2 + 16, rect.left);
3896     expect(34, rect.right);
3897
3898     DestroyWindow(hwnd);
3899 }
3900
3901 static void test_editbox(void)
3902 {
3903     static CHAR testitemA[]  = "testitem";
3904     static CHAR testitem1A[] = "testitem_quitelongname";
3905     static CHAR testitem2A[] = "testITEM_quitelongname";
3906     static CHAR buffer[25];
3907     HWND hwnd, hwndedit, hwndedit2, header;
3908     LVITEMA item;
3909     INT r;
3910
3911     hwnd = create_listview_control(LVS_EDITLABELS | LVS_REPORT);
3912     ok(hwnd != NULL, "failed to create a listview window\n");
3913
3914     insert_column(hwnd, 0);
3915
3916     memset(&item, 0, sizeof(item));
3917     item.mask = LVIF_TEXT;
3918     item.pszText = testitemA;
3919     item.iItem = 0;
3920     item.iSubItem = 0;
3921     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM)&item);
3922     expect(0, r);
3923
3924     /* test notifications without edit created */
3925     flush_sequences(sequences, NUM_MSG_SEQUENCES);
3926     r = SendMessage(hwnd, WM_COMMAND, MAKEWPARAM(0, EN_SETFOCUS), (LPARAM)0xdeadbeef);
3927     expect(0, r);
3928     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq,
3929                 "edit box WM_COMMAND (EN_SETFOCUS), no edit created", FALSE);
3930     /* same thing but with valid window */
3931     hwndedit = CreateWindowA("Edit", "Test edit", WS_VISIBLE | WS_CHILD, 0, 0, 20,
3932                 10, hwnd, (HMENU)1, (HINSTANCE)GetWindowLongPtrA(hwnd, GWLP_HINSTANCE), 0);
3933     flush_sequences(sequences, NUM_MSG_SEQUENCES);
3934     r = SendMessage(hwnd, WM_COMMAND, MAKEWPARAM(0, EN_SETFOCUS), (LPARAM)hwndedit);
3935     expect(0, r);
3936     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq,
3937                 "edit box WM_COMMAND (EN_SETFOCUS), no edit created #2", FALSE);
3938     DestroyWindow(hwndedit);
3939
3940     /* setting focus is necessary */
3941     SetFocus(hwnd);
3942     hwndedit = (HWND)SendMessage(hwnd, LVM_EDITLABEL, 0, 0);
3943     ok(IsWindow(hwndedit), "Expected Edit window to be created\n");
3944
3945     /* test children Z-order after Edit box created */
3946     header = (HWND)SendMessageA(hwnd, LVM_GETHEADER, 0, 0);
3947     ok(IsWindow(header), "Expected header to be created\n");
3948     ok(GetTopWindow(hwnd) == header, "Expected header to be on top\n");
3949     ok(GetNextWindow(header, GW_HWNDNEXT) == hwndedit, "got %p\n", GetNextWindow(header, GW_HWNDNEXT));
3950
3951     /* modify initial string */
3952     r = SendMessage(hwndedit, WM_SETTEXT, 0, (LPARAM)testitem1A);
3953     expect(TRUE, r);
3954
3955     /* edit window is resized and repositioned,
3956        check again for Z-order - it should be preserved */
3957     ok(GetTopWindow(hwnd) == header, "Expected header to be on top\n");
3958     ok(GetNextWindow(header, GW_HWNDNEXT) == hwndedit, "got %p\n", GetNextWindow(header, GW_HWNDNEXT));
3959
3960     /* return focus to listview */
3961     SetFocus(hwnd);
3962
3963     memset(&item, 0, sizeof(item));
3964     item.mask = LVIF_TEXT;
3965     item.pszText = buffer;
3966     item.cchTextMax = sizeof(buffer);
3967     item.iItem = 0;
3968     item.iSubItem = 0;
3969     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM)&item);
3970     expect(TRUE, r);
3971
3972     ok(strcmp(buffer, testitem1A) == 0, "Expected item text to change\n");
3973
3974     /* send LVM_EDITLABEL on already created edit */
3975     SetFocus(hwnd);
3976     hwndedit = (HWND)SendMessage(hwnd, LVM_EDITLABEL, 0, 0);
3977     ok(IsWindow(hwndedit), "Expected Edit window to be created\n");
3978     /* focus will be set to edit */
3979     ok(GetFocus() == hwndedit, "Expected Edit window to be focused\n");
3980     hwndedit2 = (HWND)SendMessage(hwnd, LVM_EDITLABEL, 0, 0);
3981     ok(IsWindow(hwndedit2), "Expected Edit window to be created\n");
3982
3983     /* creating label disabled when control isn't focused */
3984     SetFocus(0);
3985     hwndedit = (HWND)SendMessage(hwnd, LVM_EDITLABEL, 0, 0);
3986     todo_wine ok(hwndedit == NULL, "Expected Edit window not to be created\n");
3987
3988     /* check EN_KILLFOCUS handling */
3989     memset(&item, 0, sizeof(item));
3990     item.pszText = testitemA;
3991     item.iItem = 0;
3992     item.iSubItem = 0;
3993     r = SendMessage(hwnd, LVM_SETITEMTEXTA, 0, (LPARAM)&item);
3994     expect(TRUE, r);
3995
3996     SetFocus(hwnd);
3997     hwndedit = (HWND)SendMessage(hwnd, LVM_EDITLABEL, 0, 0);
3998     ok(IsWindow(hwndedit), "Expected Edit window to be created\n");
3999     /* modify edit and notify control that it lost focus */
4000     r = SendMessage(hwndedit, WM_SETTEXT, 0, (LPARAM)testitem1A);
4001     expect(TRUE, r);
4002     g_editbox_disp_info.item.pszText = NULL;
4003     r = SendMessage(hwnd, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)hwndedit);
4004     expect(0, r);
4005     ok(g_editbox_disp_info.item.pszText != NULL, "expected notification with not null text\n");
4006
4007     memset(&item, 0, sizeof(item));
4008     item.pszText = buffer;
4009     item.cchTextMax = sizeof(buffer);
4010     item.iItem = 0;
4011     item.iSubItem = 0;
4012     r = SendMessage(hwnd, LVM_GETITEMTEXTA, 0, (LPARAM)&item);
4013     expect(lstrlen(item.pszText), r);
4014     ok(strcmp(buffer, testitem1A) == 0, "Expected item text to change\n");
4015     ok(!IsWindow(hwndedit), "Expected Edit window to be freed\n");
4016
4017     /* change item name to differ in casing only */
4018     SetFocus(hwnd);
4019     hwndedit = (HWND)SendMessage(hwnd, LVM_EDITLABEL, 0, 0);
4020     ok(IsWindow(hwndedit), "Expected Edit window to be created\n");
4021     /* modify edit and notify control that it lost focus */
4022     r = SendMessage(hwndedit, WM_SETTEXT, 0, (LPARAM)testitem2A);
4023     expect(TRUE, r);
4024     g_editbox_disp_info.item.pszText = NULL;
4025     r = SendMessage(hwnd, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)hwndedit);
4026     expect(0, r);
4027     ok(g_editbox_disp_info.item.pszText != NULL, "got %p\n", g_editbox_disp_info.item.pszText);
4028
4029     memset(&item, 0, sizeof(item));
4030     item.pszText = buffer;
4031     item.cchTextMax = sizeof(buffer);
4032     item.iItem = 0;
4033     item.iSubItem = 0;
4034     r = SendMessage(hwnd, LVM_GETITEMTEXTA, 0, (LPARAM)&item);
4035     expect(lstrlen(item.pszText), r);
4036     ok(strcmp(buffer, testitem2A) == 0, "got %s, expected %s\n", buffer, testitem2A);
4037     ok(!IsWindow(hwndedit), "Expected Edit window to be freed\n");
4038
4039     /* end edit without saving */
4040     SetFocus(hwnd);
4041     hwndedit = (HWND)SendMessage(hwnd, LVM_EDITLABEL, 0, 0);
4042     flush_sequences(sequences, NUM_MSG_SEQUENCES);
4043     r = SendMessage(hwndedit, WM_KEYDOWN, VK_ESCAPE, 0);
4044     expect(0, r);
4045     ok_sequence(sequences, PARENT_SEQ_INDEX, edit_end_nochange,
4046                 "edit box - end edit, no change, escape", TRUE);
4047     /* end edit with saving */
4048     SetFocus(hwnd);
4049     hwndedit = (HWND)SendMessage(hwnd, LVM_EDITLABEL, 0, 0);
4050     flush_sequences(sequences, NUM_MSG_SEQUENCES);
4051     r = SendMessage(hwndedit, WM_KEYDOWN, VK_RETURN, 0);
4052     expect(0, r);
4053     ok_sequence(sequences, PARENT_SEQ_INDEX, edit_end_nochange,
4054                 "edit box - end edit, no change, return", TRUE);
4055
4056     memset(&item, 0, sizeof(item));
4057     item.pszText = buffer;
4058     item.cchTextMax = sizeof(buffer);
4059     item.iItem = 0;
4060     item.iSubItem = 0;
4061     r = SendMessage(hwnd, LVM_GETITEMTEXTA, 0, (LPARAM)&item);
4062     expect(lstrlen(item.pszText), r);
4063     ok(strcmp(buffer, testitem2A) == 0, "Expected item text to change\n");
4064
4065     /* LVM_EDITLABEL with -1 destroys current edit */
4066     hwndedit = (HWND)SendMessage(hwnd, LVM_GETEDITCONTROL, 0, 0);
4067     ok(hwndedit == NULL, "Expected Edit window not to be created\n");
4068     /* no edit present */
4069     hwndedit = (HWND)SendMessage(hwnd, LVM_EDITLABEL, -1, 0);
4070     ok(hwndedit == NULL, "Expected Edit window not to be created\n");
4071     hwndedit = (HWND)SendMessage(hwnd, LVM_EDITLABEL, 0, 0);
4072     ok(IsWindow(hwndedit), "Expected Edit window to be created\n");
4073     /* edit present */
4074     ok(GetFocus() == hwndedit, "Expected Edit to be focused\n");
4075     hwndedit2 = (HWND)SendMessage(hwnd, LVM_EDITLABEL, -1, 0);
4076     ok(hwndedit2 == NULL, "Expected Edit window not to be created\n");
4077     ok(!IsWindow(hwndedit), "Expected Edit window to be destroyed\n");
4078     ok(GetFocus() == hwnd, "Expected List to be focused\n");
4079     /* check another negative value */
4080     hwndedit = (HWND)SendMessage(hwnd, LVM_EDITLABEL, 0, 0);
4081     ok(IsWindow(hwndedit), "Expected Edit window to be created\n");
4082     ok(GetFocus() == hwndedit, "Expected Edit to be focused\n");
4083     hwndedit2 = (HWND)SendMessage(hwnd, LVM_EDITLABEL, -2, 0);
4084     ok(hwndedit2 == NULL, "Expected Edit window not to be created\n");
4085     ok(!IsWindow(hwndedit), "Expected Edit window to be destroyed\n");
4086     ok(GetFocus() == hwnd, "Expected List to be focused\n");
4087     /* and value greater than max item index */
4088     hwndedit = (HWND)SendMessage(hwnd, LVM_EDITLABEL, 0, 0);
4089     ok(IsWindow(hwndedit), "Expected Edit window to be created\n");
4090     ok(GetFocus() == hwndedit, "Expected Edit to be focused\n");
4091     r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
4092     hwndedit2 = (HWND)SendMessage(hwnd, LVM_EDITLABEL, r, 0);
4093     ok(hwndedit2 == NULL, "Expected Edit window not to be created\n");
4094     ok(!IsWindow(hwndedit), "Expected Edit window to be destroyed\n");
4095     ok(GetFocus() == hwnd, "Expected List to be focused\n");
4096
4097     /* messaging tests */
4098     SetFocus(hwnd);
4099     flush_sequences(sequences, NUM_MSG_SEQUENCES);
4100     blockEdit = FALSE;
4101     hwndedit = (HWND)SendMessage(hwnd, LVM_EDITLABEL, 0, 0);
4102     ok(IsWindow(hwndedit), "Expected Edit window to be created\n");
4103     /* testing only sizing messages */
4104     ok_sequence(sequences, EDITBOX_SEQ_INDEX, editbox_create_pos,
4105                 "edit box create - sizing", FALSE);
4106
4107     /* WM_COMMAND with EN_KILLFOCUS isn't forwarded to parent */
4108     SetFocus(hwnd);
4109     hwndedit = (HWND)SendMessage(hwnd, LVM_EDITLABEL, 0, 0);
4110     ok(IsWindow(hwndedit), "Expected Edit window to be created\n");
4111     flush_sequences(sequences, NUM_MSG_SEQUENCES);
4112     r = SendMessage(hwnd, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)hwndedit);
4113     expect(0, r);
4114     ok_sequence(sequences, PARENT_SEQ_INDEX, edit_end_nochange,
4115                 "edit box WM_COMMAND (EN_KILLFOCUS)", TRUE);
4116
4117     DestroyWindow(hwnd);
4118 }
4119
4120 static void test_notifyformat(void)
4121 {
4122     HWND hwnd, header;
4123     DWORD r;
4124
4125     hwnd = create_listview_control(LVS_REPORT);
4126     ok(hwnd != NULL, "failed to create a listview window\n");
4127
4128     /* CCM_GETUNICODEFORMAT == LVM_GETUNICODEFORMAT,
4129        CCM_SETUNICODEFORMAT == LVM_SETUNICODEFORMAT */
4130     r = SendMessage(hwnd, LVM_GETUNICODEFORMAT, 0, 0);
4131     expect(0, r);
4132     SendMessage(hwnd, WM_NOTIFYFORMAT, 0, NF_QUERY);
4133     /* set */
4134     r = SendMessage(hwnd, LVM_SETUNICODEFORMAT, 1, 0);
4135     expect(0, r);
4136     r = SendMessage(hwnd, LVM_GETUNICODEFORMAT, 0, 0);
4137     if (r == 1)
4138     {
4139         r = SendMessage(hwnd, LVM_SETUNICODEFORMAT, 0, 0);
4140         expect(1, r);
4141         r = SendMessage(hwnd, LVM_GETUNICODEFORMAT, 0, 0);
4142         expect(0, r);
4143     }
4144     else
4145     {
4146         win_skip("LVM_GETUNICODEFORMAT is unsupported\n");
4147         DestroyWindow(hwnd);
4148         return;
4149     }
4150
4151     DestroyWindow(hwnd);
4152
4153     /* test failure in parent WM_NOTIFYFORMAT  */
4154     notifyFormat = 0;
4155     hwnd = create_listview_control(LVS_REPORT);
4156     ok(hwnd != NULL, "failed to create a listview window\n");
4157     header = (HWND)SendMessage(hwnd, LVM_GETHEADER, 0, 0);
4158     ok(IsWindow(header), "expected header to be created\n");
4159     r = SendMessage(hwnd, LVM_GETUNICODEFORMAT, 0, 0);
4160     expect(0, r);
4161     r = SendMessage(header, HDM_GETUNICODEFORMAT, 0, 0);
4162     ok( r == 1, "Expected 1, got %d\n", r );
4163     r = SendMessage(hwnd, WM_NOTIFYFORMAT, 0, NF_QUERY);
4164     ok(r != 0, "Expected valid format\n");
4165
4166     notifyFormat = NFR_UNICODE;
4167     r = SendMessage(hwnd, WM_NOTIFYFORMAT, 0, NF_REQUERY);
4168     expect(NFR_UNICODE, r);
4169     r = SendMessage(hwnd, LVM_GETUNICODEFORMAT, 0, 0);
4170     expect(1, r);
4171     r = SendMessage(header, HDM_GETUNICODEFORMAT, 0, 0);
4172     ok( r == 1, "Expected 1, got %d\n", r );
4173
4174     notifyFormat = NFR_ANSI;
4175     r = SendMessage(hwnd, WM_NOTIFYFORMAT, 0, NF_REQUERY);
4176     expect(NFR_ANSI, r);
4177     r = SendMessage(hwnd, LVM_GETUNICODEFORMAT, 0, 0);
4178     expect(0, r);
4179     r = SendMessage(header, HDM_GETUNICODEFORMAT, 0, 0);
4180     ok( r == 1, "Expected 1, got %d\n", r );
4181
4182     DestroyWindow(hwnd);
4183
4184     hwndparentW = create_parent_window(TRUE);
4185     ok(IsWindow(hwndparentW), "Unicode parent creation failed\n");
4186     if (!IsWindow(hwndparentW))  return;
4187
4188     notifyFormat = -1;
4189     hwnd = create_listview_controlW(LVS_REPORT, hwndparentW);
4190     ok(hwnd != NULL, "failed to create a listview window\n");
4191     header = (HWND)SendMessage(hwnd, LVM_GETHEADER, 0, 0);
4192     ok(IsWindow(header), "expected header to be created\n");
4193     r = SendMessageW(hwnd, LVM_GETUNICODEFORMAT, 0, 0);
4194     expect(1, r);
4195     r = SendMessage(header, HDM_GETUNICODEFORMAT, 0, 0);
4196     expect(1, r);
4197     DestroyWindow(hwnd);
4198     /* receiving error code defaulting to ansi */
4199     notifyFormat = 0;
4200     hwnd = create_listview_controlW(LVS_REPORT, hwndparentW);
4201     ok(hwnd != NULL, "failed to create a listview window\n");
4202     header = (HWND)SendMessage(hwnd, LVM_GETHEADER, 0, 0);
4203     ok(IsWindow(header), "expected header to be created\n");
4204     r = SendMessageW(hwnd, LVM_GETUNICODEFORMAT, 0, 0);
4205     expect(0, r);
4206     r = SendMessage(header, HDM_GETUNICODEFORMAT, 0, 0);
4207     expect(1, r);
4208     DestroyWindow(hwnd);
4209     /* receiving ansi code from unicode window, use it */
4210     notifyFormat = NFR_ANSI;
4211     hwnd = create_listview_controlW(LVS_REPORT, hwndparentW);
4212     ok(hwnd != NULL, "failed to create a listview window\n");
4213     header = (HWND)SendMessage(hwnd, LVM_GETHEADER, 0, 0);
4214     ok(IsWindow(header), "expected header to be created\n");
4215     r = SendMessageW(hwnd, LVM_GETUNICODEFORMAT, 0, 0);
4216     expect(0, r);
4217     r = SendMessage(header, HDM_GETUNICODEFORMAT, 0, 0);
4218     expect(1, r);
4219     DestroyWindow(hwnd);
4220     /* unicode listview with ansi parent window */
4221     notifyFormat = -1;
4222     hwnd = create_listview_controlW(LVS_REPORT, hwndparent);
4223     ok(hwnd != NULL, "failed to create a listview window\n");
4224     header = (HWND)SendMessage(hwnd, LVM_GETHEADER, 0, 0);
4225     ok(IsWindow(header), "expected header to be created\n");
4226     r = SendMessageW(hwnd, LVM_GETUNICODEFORMAT, 0, 0);
4227     expect(0, r);
4228     r = SendMessage(header, HDM_GETUNICODEFORMAT, 0, 0);
4229     expect(1, r);
4230     DestroyWindow(hwnd);
4231     /* unicode listview with ansi parent window, return error code */
4232     notifyFormat = 0;
4233     hwnd = create_listview_controlW(LVS_REPORT, hwndparent);
4234     ok(hwnd != NULL, "failed to create a listview window\n");
4235     header = (HWND)SendMessage(hwnd, LVM_GETHEADER, 0, 0);
4236     ok(IsWindow(header), "expected header to be created\n");
4237     r = SendMessageW(hwnd, LVM_GETUNICODEFORMAT, 0, 0);
4238     expect(0, r);
4239     r = SendMessage(header, HDM_GETUNICODEFORMAT, 0, 0);
4240     expect(1, r);
4241     DestroyWindow(hwnd);
4242
4243     DestroyWindow(hwndparentW);
4244 }
4245
4246 static void test_indentation(void)
4247 {
4248     HWND hwnd;
4249     LVITEMA item;
4250     DWORD r;
4251
4252     hwnd = create_listview_control(LVS_REPORT);
4253     ok(hwnd != NULL, "failed to create a listview window\n");
4254
4255     memset(&item, 0, sizeof(item));
4256     item.mask = LVIF_INDENT;
4257     item.iItem = 0;
4258     item.iIndent = I_INDENTCALLBACK;
4259     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM)&item);
4260     expect(0, r);
4261
4262     flush_sequences(sequences, NUM_MSG_SEQUENCES);
4263
4264     item.iItem = 0;
4265     item.mask = LVIF_INDENT;
4266     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM)&item);
4267     expect(TRUE, r);
4268
4269     ok_sequence(sequences, PARENT_SEQ_INDEX, single_getdispinfo_parent_seq,
4270                 "get indent dispinfo", FALSE);
4271
4272     DestroyWindow(hwnd);
4273 }
4274
4275 static INT CALLBACK DummyCompareEx(LPARAM first, LPARAM second, LPARAM param)
4276 {
4277     return 0;
4278 }
4279
4280 static BOOL is_below_comctl_5(void)
4281 {
4282     HWND hwnd;
4283     BOOL ret;
4284
4285     hwnd = create_listview_control(LVS_REPORT);
4286     ok(hwnd != NULL, "failed to create a listview window\n");
4287     insert_item(hwnd, 0);
4288
4289     ret = SendMessage(hwnd, LVM_SORTITEMSEX, 0, (LPARAM)&DummyCompareEx);
4290
4291     DestroyWindow(hwnd);
4292
4293     return !ret;
4294 }
4295
4296 static void test_get_set_view(void)
4297 {
4298     HWND hwnd;
4299     DWORD ret;
4300     DWORD_PTR style;
4301
4302     /* test style->view mapping */
4303     hwnd = create_listview_control(LVS_REPORT);
4304     ok(hwnd != NULL, "failed to create a listview window\n");
4305
4306     ret = SendMessage(hwnd, LVM_GETVIEW, 0, 0);
4307     expect(LV_VIEW_DETAILS, ret);
4308
4309     style = GetWindowLongPtr(hwnd, GWL_STYLE);
4310     /* LVS_ICON == 0 */
4311     SetWindowLongPtr(hwnd, GWL_STYLE, style & ~LVS_REPORT);
4312     ret = SendMessage(hwnd, LVM_GETVIEW, 0, 0);
4313     expect(LV_VIEW_ICON, ret);
4314
4315     style = GetWindowLongPtr(hwnd, GWL_STYLE);
4316     SetWindowLongPtr(hwnd, GWL_STYLE, style | LVS_SMALLICON);
4317     ret = SendMessage(hwnd, LVM_GETVIEW, 0, 0);
4318     expect(LV_VIEW_SMALLICON, ret);
4319
4320     style = GetWindowLongPtr(hwnd, GWL_STYLE);
4321     SetWindowLongPtr(hwnd, GWL_STYLE, (style & ~LVS_SMALLICON) | LVS_LIST);
4322     ret = SendMessage(hwnd, LVM_GETVIEW, 0, 0);
4323     expect(LV_VIEW_LIST, ret);
4324
4325     /* switching view doesn't touch window style */
4326     ret = SendMessage(hwnd, LVM_SETVIEW, LV_VIEW_DETAILS, 0);
4327     expect(1, ret);
4328     style = GetWindowLongPtr(hwnd, GWL_STYLE);
4329     ok(style & LVS_LIST, "Expected style to be preserved\n");
4330     ret = SendMessage(hwnd, LVM_SETVIEW, LV_VIEW_ICON, 0);
4331     expect(1, ret);
4332     style = GetWindowLongPtr(hwnd, GWL_STYLE);
4333     ok(style & LVS_LIST, "Expected style to be preserved\n");
4334     ret = SendMessage(hwnd, LVM_SETVIEW, LV_VIEW_SMALLICON, 0);
4335     expect(1, ret);
4336     style = GetWindowLongPtr(hwnd, GWL_STYLE);
4337     ok(style & LVS_LIST, "Expected style to be preserved\n");
4338
4339     DestroyWindow(hwnd);
4340 }
4341
4342 static void test_canceleditlabel(void)
4343 {
4344     HWND hwnd, hwndedit;
4345     DWORD ret;
4346     CHAR buff[10];
4347     LVITEMA itema;
4348     static CHAR test[] = "test";
4349     static const CHAR test1[] = "test1";
4350
4351     hwnd = create_listview_control(LVS_EDITLABELS | LVS_REPORT);
4352     ok(hwnd != NULL, "failed to create a listview window\n");
4353
4354     insert_item(hwnd, 0);
4355
4356     /* try without edit created */
4357     flush_sequences(sequences, NUM_MSG_SEQUENCES);
4358     ret = SendMessage(hwnd, LVM_CANCELEDITLABEL, 0, 0);
4359     expect(TRUE, ret);
4360     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq,
4361                 "cancel edit label without edit", FALSE);
4362
4363     /* cancel without data change */
4364     SetFocus(hwnd);
4365     hwndedit = (HWND)SendMessage(hwnd, LVM_EDITLABEL, 0, 0);
4366     ok(IsWindow(hwndedit), "Expected edit control to be created\n");
4367     ret = SendMessage(hwnd, LVM_CANCELEDITLABEL, 0, 0);
4368     expect(TRUE, ret);
4369     ok(!IsWindow(hwndedit), "Expected edit control to be destroyed\n");
4370
4371     /* cancel after data change */
4372     memset(&itema, 0, sizeof(itema));
4373     itema.pszText = test;
4374     ret = SendMessage(hwnd, LVM_SETITEMTEXT, 0, (LPARAM)&itema);
4375     expect(TRUE, ret);
4376     SetFocus(hwnd);
4377     hwndedit = (HWND)SendMessage(hwnd, LVM_EDITLABEL, 0, 0);
4378     ok(IsWindow(hwndedit), "Expected edit control to be created\n");
4379     ret = SetWindowText(hwndedit, test1);
4380     expect(1, ret);
4381     ret = SendMessage(hwnd, LVM_CANCELEDITLABEL, 0, 0);
4382     expect(TRUE, ret);
4383     ok(!IsWindow(hwndedit), "Expected edit control to be destroyed\n");
4384     memset(&itema, 0, sizeof(itema));
4385     itema.pszText = buff;
4386     itema.cchTextMax = sizeof(buff)/sizeof(CHAR);
4387     ret = SendMessage(hwnd, LVM_GETITEMTEXT, 0, (LPARAM)&itema);
4388     expect(5, ret);
4389     ok(strcmp(buff, test1) == 0, "Expected label text not to change\n");
4390
4391     DestroyWindow(hwnd);
4392 }
4393
4394 static void test_mapidindex(void)
4395 {
4396     HWND hwnd;
4397     INT ret;
4398
4399     /* LVM_MAPINDEXTOID unsupported with LVS_OWNERDATA */
4400     hwnd = create_listview_control(LVS_OWNERDATA | LVS_REPORT);
4401     ok(hwnd != NULL, "failed to create a listview window\n");
4402     insert_item(hwnd, 0);
4403     ret = SendMessage(hwnd, LVM_MAPINDEXTOID, 0, 0);
4404     expect(-1, ret);
4405     DestroyWindow(hwnd);
4406
4407     hwnd = create_listview_control(LVS_REPORT);
4408     ok(hwnd != NULL, "failed to create a listview window\n");
4409
4410     /* LVM_MAPINDEXTOID with invalid index */
4411     ret = SendMessage(hwnd, LVM_MAPINDEXTOID, 0, 0);
4412     expect(-1, ret);
4413
4414     insert_item(hwnd, 0);
4415     insert_item(hwnd, 1);
4416
4417     ret = SendMessage(hwnd, LVM_MAPINDEXTOID, -1, 0);
4418     expect(-1, ret);
4419     ret = SendMessage(hwnd, LVM_MAPINDEXTOID, 2, 0);
4420     expect(-1, ret);
4421
4422     ret = SendMessage(hwnd, LVM_MAPINDEXTOID, 0, 0);
4423     expect(0, ret);
4424     ret = SendMessage(hwnd, LVM_MAPINDEXTOID, 1, 0);
4425     expect(1, ret);
4426     /* remove 0 indexed item, id retained */
4427     SendMessage(hwnd, LVM_DELETEITEM, 0, 0);
4428     ret = SendMessage(hwnd, LVM_MAPINDEXTOID, 0, 0);
4429     expect(1, ret);
4430     /* new id starts from previous value */
4431     insert_item(hwnd, 1);
4432     ret = SendMessage(hwnd, LVM_MAPINDEXTOID, 1, 0);
4433     expect(2, ret);
4434
4435     /* get index by id */
4436     ret = SendMessage(hwnd, LVM_MAPIDTOINDEX, -1, 0);
4437     expect(-1, ret);
4438     ret = SendMessage(hwnd, LVM_MAPIDTOINDEX, 0, 0);
4439     expect(-1, ret);
4440     ret = SendMessage(hwnd, LVM_MAPIDTOINDEX, 1, 0);
4441     expect(0, ret);
4442     ret = SendMessage(hwnd, LVM_MAPIDTOINDEX, 2, 0);
4443     expect(1, ret);
4444
4445     DestroyWindow(hwnd);
4446 }
4447
4448 static void test_getitemspacing(void)
4449 {
4450     HWND hwnd;
4451     DWORD ret;
4452     INT cx, cy;
4453     HIMAGELIST himl;
4454     HBITMAP hbmp;
4455     LVITEMA itema;
4456
4457     cx = GetSystemMetrics(SM_CXICONSPACING) - GetSystemMetrics(SM_CXICON);
4458     cy = GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON);
4459
4460     /* LVS_ICON */
4461     hwnd = create_listview_control(LVS_ICON);
4462     ret = SendMessage(hwnd, LVM_GETITEMSPACING, FALSE, 0);
4463 todo_wine {
4464     expect(cx, LOWORD(ret));
4465     expect(cy, HIWORD(ret));
4466 }
4467     /* now try with icons */
4468     himl = ImageList_Create(40, 40, 0, 4, 4);
4469     ok(himl != NULL, "failed to create imagelist\n");
4470     hbmp = CreateBitmap(40, 40, 1, 1, NULL);
4471     ok(hbmp != NULL, "failed to create bitmap\n");
4472     ret = ImageList_Add(himl, hbmp, 0);
4473     expect(0, ret);
4474     ret = SendMessage(hwnd, LVM_SETIMAGELIST, 0, (LPARAM)himl);
4475     expect(0, ret);
4476
4477     itema.mask = LVIF_IMAGE;
4478     itema.iImage = 0;
4479     itema.iItem = 0;
4480     itema.iSubItem = 0;
4481     ret = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM)&itema);
4482     expect(0, ret);
4483     ret = SendMessage(hwnd, LVM_GETITEMSPACING, FALSE, 0);
4484 todo_wine {
4485     /* spacing + icon size returned */
4486     expect(cx + 40, LOWORD(ret));
4487     expect(cy + 40, HIWORD(ret));
4488 }
4489     DestroyWindow(hwnd);
4490     /* LVS_SMALLICON */
4491     hwnd = create_listview_control(LVS_SMALLICON);
4492     ret = SendMessage(hwnd, LVM_GETITEMSPACING, FALSE, 0);
4493 todo_wine {
4494     expect(cx, LOWORD(ret));
4495     expect(cy, HIWORD(ret));
4496 }
4497     DestroyWindow(hwnd);
4498     /* LVS_REPORT */
4499     hwnd = create_listview_control(LVS_REPORT);
4500     ret = SendMessage(hwnd, LVM_GETITEMSPACING, FALSE, 0);
4501 todo_wine {
4502     expect(cx, LOWORD(ret));
4503     expect(cy, HIWORD(ret));
4504 }
4505     DestroyWindow(hwnd);
4506     /* LVS_LIST */
4507     hwnd = create_listview_control(LVS_LIST);
4508     ret = SendMessage(hwnd, LVM_GETITEMSPACING, FALSE, 0);
4509 todo_wine {
4510     expect(cx, LOWORD(ret));
4511     expect(cy, HIWORD(ret));
4512 }
4513     DestroyWindow(hwnd);
4514 }
4515
4516 static void test_getcolumnwidth(void)
4517 {
4518     HWND hwnd;
4519     INT ret;
4520     DWORD_PTR style;
4521     LVCOLUMNA col;
4522     LVITEMA itema;
4523     HDC hdc;
4524
4525     /* default column width */
4526     hwnd = create_listview_control(LVS_ICON);
4527     ret = SendMessage(hwnd, LVM_GETCOLUMNWIDTH, 0, 0);
4528     expect(0, ret);
4529     style = GetWindowLong(hwnd, GWL_STYLE);
4530     SetWindowLong(hwnd, GWL_STYLE, style | LVS_LIST);
4531     ret = SendMessage(hwnd, LVM_GETCOLUMNWIDTH, 0, 0);
4532     todo_wine expect(8, ret);
4533     style = GetWindowLong(hwnd, GWL_STYLE) & ~LVS_LIST;
4534     SetWindowLong(hwnd, GWL_STYLE, style | LVS_REPORT);
4535     col.mask = 0;
4536     ret = SendMessage(hwnd, LVM_INSERTCOLUMNA, 0, (LPARAM)&col);
4537     expect(0, ret);
4538     ret = SendMessage(hwnd, LVM_GETCOLUMNWIDTH, 0, 0);
4539     expect(10, ret);
4540     DestroyWindow(hwnd);
4541
4542     /* default column width with item added */
4543     hwnd = create_listview_control(LVS_LIST);
4544     memset(&itema, 0, sizeof(itema));
4545     SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM)&itema);
4546     ret = SendMessage(hwnd, LVM_GETCOLUMNWIDTH, 0, 0);
4547     hdc = GetDC(hwnd);
4548     todo_wine expect(((GetDeviceCaps(hdc, LOGPIXELSX) + 15) / 16) * 16, ret);
4549     ReleaseDC(hwnd, hdc);
4550     DestroyWindow(hwnd);
4551 }
4552
4553 static void test_scrollnotify(void)
4554 {
4555     HWND hwnd;
4556     DWORD ret;
4557
4558     hwnd = create_listview_control(LVS_REPORT);
4559
4560     insert_column(hwnd, 0);
4561     insert_column(hwnd, 1);
4562     insert_item(hwnd, 0);
4563
4564     /* make it scrollable - resize */
4565     ret = SendMessage(hwnd, LVM_SETCOLUMNWIDTH, 0, MAKELPARAM(100, 0));
4566     expect(TRUE, ret);
4567     ret = SendMessage(hwnd, LVM_SETCOLUMNWIDTH, 1, MAKELPARAM(100, 0));
4568     expect(TRUE, ret);
4569
4570     /* try with dummy call */
4571     flush_sequences(sequences, NUM_MSG_SEQUENCES);
4572     ret = SendMessage(hwnd, LVM_SCROLL, 0, 0);
4573     expect(TRUE, ret);
4574     ok_sequence(sequences, PARENT_SEQ_INDEX, scroll_parent_seq,
4575                 "scroll notify 1", TRUE);
4576
4577     flush_sequences(sequences, NUM_MSG_SEQUENCES);
4578     ret = SendMessage(hwnd, LVM_SCROLL, 1, 0);
4579     expect(TRUE, ret);
4580     ok_sequence(sequences, PARENT_SEQ_INDEX, scroll_parent_seq,
4581                 "scroll notify 2", TRUE);
4582
4583     flush_sequences(sequences, NUM_MSG_SEQUENCES);
4584     ret = SendMessage(hwnd, LVM_SCROLL, 1, 1);
4585     expect(TRUE, ret);
4586     ok_sequence(sequences, PARENT_SEQ_INDEX, scroll_parent_seq,
4587                 "scroll notify 3", TRUE);
4588
4589     DestroyWindow(hwnd);
4590 }
4591
4592 static void test_LVS_EX_TRANSPARENTBKGND(void)
4593 {
4594     HWND hwnd;
4595     DWORD ret;
4596     HDC hdc;
4597
4598     hwnd = create_listview_control(LVS_REPORT);
4599
4600     ret = SendMessage(hwnd, LVM_SETBKCOLOR, 0, RGB(0, 0, 0));
4601     expect(TRUE, ret);
4602
4603     SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_TRANSPARENTBKGND,
4604                                                     LVS_EX_TRANSPARENTBKGND);
4605
4606     ret = SendMessage(hwnd, LVM_GETBKCOLOR, 0, 0);
4607     if (ret != CLR_NONE)
4608     {
4609         win_skip("LVS_EX_TRANSPARENTBKGND unsupported\n");
4610         DestroyWindow(hwnd);
4611         return;
4612     }
4613
4614     /* try to set some back color and check this style bit */
4615     ret = SendMessage(hwnd, LVM_SETBKCOLOR, 0, RGB(0, 0, 0));
4616     expect(TRUE, ret);
4617     ret = SendMessage(hwnd, LVM_GETEXTENDEDLISTVIEWSTYLE, 0, 0);
4618     ok(!(ret & LVS_EX_TRANSPARENTBKGND), "Expected LVS_EX_TRANSPARENTBKGND to unset\n");
4619
4620     /* now test what this style actually does */
4621     SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_TRANSPARENTBKGND,
4622                                                     LVS_EX_TRANSPARENTBKGND);
4623
4624     hdc = GetWindowDC(hwndparent);
4625
4626     flush_sequences(sequences, NUM_MSG_SEQUENCES);
4627     SendMessageA(hwnd, WM_ERASEBKGND, (WPARAM)hdc, 0);
4628     ok_sequence(sequences, PARENT_SEQ_INDEX, lvs_ex_transparentbkgnd_seq,
4629                 "LVS_EX_TRANSPARENTBKGND parent", FALSE);
4630
4631     ReleaseDC(hwndparent, hdc);
4632
4633     DestroyWindow(hwnd);
4634 }
4635
4636 static void test_approximate_viewrect(void)
4637 {
4638     HWND hwnd;
4639     DWORD ret;
4640     HIMAGELIST himl;
4641     HBITMAP hbmp;
4642     LVITEMA itema;
4643     static CHAR test[] = "abracadabra, a very long item label";
4644
4645     hwnd = create_listview_control(LVS_ICON);
4646     himl = ImageList_Create(40, 40, 0, 4, 4);
4647     ok(himl != NULL, "failed to create imagelist\n");
4648     hbmp = CreateBitmap(40, 40, 1, 1, NULL);
4649     ok(hbmp != NULL, "failed to create bitmap\n");
4650     ret = ImageList_Add(himl, hbmp, 0);
4651     expect(0, ret);
4652     ret = SendMessage(hwnd, LVM_SETIMAGELIST, 0, (LPARAM)himl);
4653     expect(0, ret);
4654
4655     itema.mask = LVIF_IMAGE;
4656     itema.iImage = 0;
4657     itema.iItem = 0;
4658     itema.iSubItem = 0;
4659     ret = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM)&itema);
4660     expect(0, ret);
4661
4662     ret = SendMessage(hwnd, LVM_SETICONSPACING, 0, MAKELPARAM(75, 75));
4663     if (ret == 0)
4664     {
4665         /* version 4.0 */
4666         win_skip("LVM_SETICONSPACING unimplemented. Skipping.\n");
4667         return;
4668     }
4669
4670     ret = SendMessage(hwnd, LVM_APPROXIMATEVIEWRECT, 11, MAKELPARAM(100,100));
4671     expect(MAKELONG(77,827), ret);
4672
4673     ret = SendMessage(hwnd, LVM_SETICONSPACING, 0, MAKELPARAM(50, 50));
4674     ok(ret != 0, "got 0\n");
4675
4676     ret = SendMessage(hwnd, LVM_APPROXIMATEVIEWRECT, 11, MAKELPARAM(100,100));
4677     expect(MAKELONG(102,302), ret);
4678
4679     ret = SendMessage(hwnd, LVM_APPROXIMATEVIEWRECT, -1, MAKELPARAM(100,100));
4680     expect(MAKELONG(52,52), ret);
4681
4682     itema.pszText = test;
4683     ret = SendMessage(hwnd, LVM_SETITEMTEXT, 0, (LPARAM)&itema);
4684     expect(TRUE, ret);
4685     ret = SendMessage(hwnd, LVM_APPROXIMATEVIEWRECT, -1, MAKELPARAM(100,100));
4686     expect(MAKELONG(52,52), ret);
4687
4688     ret = SendMessage(hwnd, LVM_APPROXIMATEVIEWRECT, 0, MAKELPARAM(100,100));
4689     expect(MAKELONG(52,2), ret);
4690     ret = SendMessage(hwnd, LVM_APPROXIMATEVIEWRECT, 1, MAKELPARAM(100,100));
4691     expect(MAKELONG(52,52), ret);
4692     ret = SendMessage(hwnd, LVM_APPROXIMATEVIEWRECT, 2, MAKELPARAM(100,100));
4693     expect(MAKELONG(102,52), ret);
4694     ret = SendMessage(hwnd, LVM_APPROXIMATEVIEWRECT, 3, MAKELPARAM(100,100));
4695     expect(MAKELONG(102,102), ret);
4696     ret = SendMessage(hwnd, LVM_APPROXIMATEVIEWRECT, 4, MAKELPARAM(100,100));
4697     expect(MAKELONG(102,102), ret);
4698     ret = SendMessage(hwnd, LVM_APPROXIMATEVIEWRECT, 5, MAKELPARAM(100,100));
4699     expect(MAKELONG(102,152), ret);
4700     ret = SendMessage(hwnd, LVM_APPROXIMATEVIEWRECT, 6, MAKELPARAM(100,100));
4701     expect(MAKELONG(102,152), ret);
4702     ret = SendMessage(hwnd, LVM_APPROXIMATEVIEWRECT, 7, MAKELPARAM(160,100));
4703     expect(MAKELONG(152,152), ret);
4704
4705     DestroyWindow(hwnd);
4706 }
4707
4708 static void test_finditem(void)
4709 {
4710     LVFINDINFOA fi;
4711     static char f[5];
4712     HWND hwnd;
4713     INT r;
4714
4715     hwnd = create_listview_control(LVS_REPORT);
4716     insert_item(hwnd, 0);
4717
4718     memset(&fi, 0, sizeof(fi));
4719
4720     /* full string search, inserted text was "foo" */
4721     strcpy(f, "foo");
4722     fi.flags = LVFI_STRING;
4723     fi.psz = f;
4724     r = SendMessage(hwnd, LVM_FINDITEMA, -1, (LPARAM)&fi);
4725     expect(0, r);
4726     /* partial string search, inserted text was "foo" */
4727     strcpy(f, "fo");
4728     fi.flags = LVFI_STRING | LVFI_PARTIAL;
4729     fi.psz = f;
4730     r = SendMessage(hwnd, LVM_FINDITEMA, -1, (LPARAM)&fi);
4731     expect(0, r);
4732     /* partial string search, part after start char */
4733     strcpy(f, "oo");
4734     fi.flags = LVFI_STRING | LVFI_PARTIAL;
4735     fi.psz = f;
4736     r = SendMessage(hwnd, LVM_FINDITEMA, -1, (LPARAM)&fi);
4737     expect(-1, r);
4738
4739     /* try with LVFI_SUBSTRING */
4740     strcpy(f, "fo");
4741     fi.flags = LVFI_SUBSTRING;
4742     fi.psz = f;
4743     r = SendMessage(hwnd, LVM_FINDITEMA, -1, (LPARAM)&fi);
4744     if (r == -1)
4745     {
4746         win_skip("LVFI_SUBSTRING not supported\n");
4747         DestroyWindow(hwnd);
4748         return;
4749     }
4750     expect(0, r);
4751     strcpy(f, "f");
4752     fi.flags = LVFI_SUBSTRING;
4753     fi.psz = f;
4754     r = SendMessage(hwnd, LVM_FINDITEMA, -1, (LPARAM)&fi);
4755     expect(0, r);
4756     strcpy(f, "o");
4757     fi.flags = LVFI_SUBSTRING;
4758     fi.psz = f;
4759     r = SendMessage(hwnd, LVM_FINDITEMA, -1, (LPARAM)&fi);
4760     expect(-1, r);
4761
4762     strcpy(f, "f");
4763     fi.flags = LVFI_SUBSTRING | LVFI_STRING;
4764     fi.psz = f;
4765     r = SendMessage(hwnd, LVM_FINDITEMA, -1, (LPARAM)&fi);
4766     expect(0, r);
4767
4768     DestroyWindow(hwnd);
4769 }
4770
4771 static void test_LVS_EX_HEADERINALLVIEWS(void)
4772 {
4773     HWND hwnd, header;
4774     DWORD style;
4775
4776     hwnd = create_listview_control(LVS_ICON);
4777
4778     SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_HEADERINALLVIEWS,
4779                                                     LVS_EX_HEADERINALLVIEWS);
4780
4781     header = (HWND)SendMessage(hwnd, LVM_GETHEADER, 0, 0);
4782     if (!IsWindow(header))
4783     {
4784         win_skip("LVS_EX_HEADERINALLVIEWS unsupported\n");
4785         DestroyWindow(hwnd);
4786         return;
4787     }
4788
4789     /* LVS_NOCOLUMNHEADER works as before */
4790     style = GetWindowLongA(hwnd, GWL_STYLE);
4791     SetWindowLongW(hwnd, GWL_STYLE, style | LVS_NOCOLUMNHEADER);
4792     style = GetWindowLongA(header, GWL_STYLE);
4793     ok(style & HDS_HIDDEN, "Expected HDS_HIDDEN\n");
4794     style = GetWindowLongA(hwnd, GWL_STYLE);
4795     SetWindowLongW(hwnd, GWL_STYLE, style & ~LVS_NOCOLUMNHEADER);
4796     style = GetWindowLongA(header, GWL_STYLE);
4797     ok(!(style & HDS_HIDDEN), "Expected HDS_HIDDEN to be unset\n");
4798
4799     /* try to remove style */
4800     SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_HEADERINALLVIEWS, 0);
4801     header = (HWND)SendMessage(hwnd, LVM_GETHEADER, 0, 0);
4802     ok(IsWindow(header), "Expected header to be created\n");
4803     style = GetWindowLongA(header, GWL_STYLE);
4804     ok(!(style & HDS_HIDDEN), "HDS_HIDDEN not expected\n");
4805
4806     DestroyWindow(hwnd);
4807
4808     /* check other styles */
4809     hwnd = create_listview_control(LVS_LIST);
4810     SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_HEADERINALLVIEWS,
4811                                                     LVS_EX_HEADERINALLVIEWS);
4812     header = (HWND)SendMessage(hwnd, LVM_GETHEADER, 0, 0);
4813     ok(IsWindow(header), "Expected header to be created\n");
4814     DestroyWindow(hwnd);
4815
4816     hwnd = create_listview_control(LVS_SMALLICON);
4817     SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_HEADERINALLVIEWS,
4818                                                     LVS_EX_HEADERINALLVIEWS);
4819     header = (HWND)SendMessage(hwnd, LVM_GETHEADER, 0, 0);
4820     ok(IsWindow(header), "Expected header to be created\n");
4821     DestroyWindow(hwnd);
4822
4823     hwnd = create_listview_control(LVS_REPORT);
4824     SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_HEADERINALLVIEWS,
4825                                                     LVS_EX_HEADERINALLVIEWS);
4826     header = (HWND)SendMessage(hwnd, LVM_GETHEADER, 0, 0);
4827     ok(IsWindow(header), "Expected header to be created\n");
4828     DestroyWindow(hwnd);
4829 }
4830
4831 static void test_hover(void)
4832 {
4833     HWND hwnd;
4834     DWORD r;
4835
4836     hwnd = create_listview_control(LVS_ICON);
4837
4838     /* test WM_MOUSEHOVER forwarding */
4839     flush_sequences(sequences, NUM_MSG_SEQUENCES);
4840     r = SendMessage(hwnd, WM_MOUSEHOVER, 0, 0);
4841     expect(0, r);
4842     ok_sequence(sequences, PARENT_SEQ_INDEX, hover_parent, "NM_HOVER allow test", TRUE);
4843     g_block_hover = TRUE;
4844     flush_sequences(sequences, NUM_MSG_SEQUENCES);
4845     r = SendMessage(hwnd, WM_MOUSEHOVER, 0, 0);
4846     expect(0, r);
4847     ok_sequence(sequences, PARENT_SEQ_INDEX, hover_parent, "NM_HOVER block test", TRUE);
4848     g_block_hover = FALSE;
4849
4850     r = SendMessage(hwnd, LVM_SETHOVERTIME, 0, 500);
4851     expect(HOVER_DEFAULT, r);
4852     r = SendMessage(hwnd, LVM_GETHOVERTIME, 0, 0);
4853     expect(500, r);
4854
4855     DestroyWindow(hwnd);
4856 }
4857
4858 static void test_destroynotify(void)
4859 {
4860     HWND hwnd;
4861
4862     hwnd = create_listview_control(LVS_REPORT);
4863     ok(hwnd != NULL, "failed to create listview window\n");
4864
4865     flush_sequences(sequences, NUM_MSG_SEQUENCES);
4866     DestroyWindow(hwnd);
4867     ok_sequence(sequences, COMBINED_SEQ_INDEX, listview_destroy, "check destroy order", FALSE);
4868 }
4869
4870 static void test_header_notification(void)
4871 {
4872     static char textA[] = "newtext";
4873     HWND list, header;
4874     HDITEMA item;
4875     NMHEADER nmh;
4876     LVCOLUMNA col;
4877     DWORD ret;
4878     BOOL r;
4879
4880     list = create_listview_control(LVS_REPORT);
4881     ok(list != NULL, "failed to create listview window\n");
4882
4883     memset(&col, 0, sizeof(col));
4884     col.mask = LVCF_WIDTH;
4885     col.cx = 100;
4886     ret = SendMessage(list, LVM_INSERTCOLUMNA, 0, (LPARAM)&col);
4887     expect(0, ret);
4888
4889     /* check list parent notification after header item changed,
4890        this test should be placed before header subclassing to avoid
4891        Listview -> Header messages to be logged */
4892     flush_sequences(sequences, NUM_MSG_SEQUENCES);
4893
4894     col.mask = LVCF_TEXT;
4895     col.pszText = textA;
4896     r = SendMessage(list, LVM_SETCOLUMNA, 0, (LPARAM)&col);
4897     expect(TRUE, r);
4898
4899     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_header_changed_seq,
4900                 "header notify, listview", FALSE);
4901     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq,
4902                 "header notify, parent", FALSE);
4903
4904     header = subclass_header(list);
4905
4906     ret = SendMessage(header, HDM_GETITEMCOUNT, 0, 0);
4907     expect(1, ret);
4908
4909     memset(&item, 0, sizeof(item));
4910     item.mask = HDI_WIDTH;
4911     ret = SendMessage(header, HDM_GETITEMA, 0, (LPARAM)&item);
4912     expect(1, ret);
4913     expect(100, item.cxy);
4914
4915     nmh.hdr.hwndFrom = header;
4916     nmh.hdr.idFrom = GetWindowLongPtr(header, GWLP_ID);
4917     nmh.hdr.code = HDN_ITEMCHANGEDA;
4918     nmh.iItem = 0;
4919     nmh.iButton = 0;
4920     item.mask = HDI_WIDTH;
4921     item.cxy = 50;
4922     nmh.pitem = &item;
4923     ret = SendMessage(list, WM_NOTIFY, 0, (LPARAM)&nmh);
4924     expect(0, ret);
4925
4926     DestroyWindow(list);
4927 }
4928
4929 static void test_header_notification2(void)
4930 {
4931     static char textA[] = "newtext";
4932     HWND list, header;
4933     HDITEMW itemW;
4934     NMHEADERW nmhdr;
4935     LVCOLUMNA col;
4936     DWORD ret;
4937     WCHAR buffer[100];
4938     struct message parent_header_notify_seq[] = {
4939         { WM_NOTIFY, sent|id, 0, 0, 0 },
4940         { 0 }
4941     };
4942
4943     list = create_listview_control(LVS_REPORT);
4944     ok(list != NULL, "failed to create listview window\n");
4945
4946     memset(&col, 0, sizeof(col));
4947     col.mask = LVCF_WIDTH | LVCF_TEXT;
4948     col.cx = 100;
4949     col.pszText = textA;
4950     ret = SendMessage(list, LVM_INSERTCOLUMNA, 0, (LPARAM)&col);
4951     expect(0, ret);
4952
4953     header = ListView_GetHeader(list);
4954     ok(header != 0, "No header\n");
4955     memset(&itemW, 0, sizeof(itemW));
4956     itemW.mask = HDI_WIDTH | HDI_ORDER | HDI_TEXT;
4957     itemW.pszText = buffer;
4958     itemW.cchTextMax = sizeof(buffer);
4959     ret = SendMessageW(header, HDM_GETITEMW, 0, (LPARAM)&itemW);
4960     expect(1, ret);
4961
4962     nmhdr.hdr.hwndFrom = header;
4963     nmhdr.hdr.idFrom = GetWindowLongPtr(header, GWLP_ID);
4964     nmhdr.iItem = 0;
4965     nmhdr.iButton = 0;
4966     nmhdr.pitem = &itemW;
4967
4968     flush_sequences(sequences, NUM_MSG_SEQUENCES);
4969     nmhdr.hdr.code = HDN_ITEMCHANGINGW;
4970     ret = SendMessageW(list, WM_NOTIFY, 0, (LPARAM)&nmhdr);
4971     parent_header_notify_seq[0].id = HDN_ITEMCHANGINGA;
4972     ok_sequence(sequences, PARENT_SEQ_INDEX, parent_header_notify_seq,
4973                 "header notify, parent", TRUE);
4974     todo_wine
4975     ok(nmhdr.hdr.code == HDN_ITEMCHANGINGA, "Expected ANSI notification code\n");
4976     flush_sequences(sequences, NUM_MSG_SEQUENCES);
4977     nmhdr.hdr.code = HDN_ITEMCHANGEDW;
4978     ret = SendMessageW(list, WM_NOTIFY, 0, (LPARAM)&nmhdr);
4979     parent_header_notify_seq[0].id = HDN_ITEMCHANGEDA;
4980     ok_sequence(sequences, PARENT_SEQ_INDEX, parent_header_notify_seq,
4981                 "header notify, parent", TRUE);
4982     todo_wine
4983     ok(nmhdr.hdr.code == HDN_ITEMCHANGEDA, "Expected ANSI notification code\n");
4984     /* HDN_ITEMCLICK sets focus to list, which generates messages we don't want to check */
4985     SetFocus(list);
4986     flush_sequences(sequences, NUM_MSG_SEQUENCES);
4987     nmhdr.hdr.code = HDN_ITEMCLICKW;
4988     ret = SendMessageW(list, WM_NOTIFY, 0, (LPARAM)&nmhdr);
4989     ok_sequence(sequences, PARENT_SEQ_INDEX, parent_header_click_seq,
4990                 "header notify, parent", FALSE);
4991     ok(nmhdr.hdr.code == HDN_ITEMCLICKA, "Expected ANSI notification code\n");
4992     flush_sequences(sequences, NUM_MSG_SEQUENCES);
4993     nmhdr.hdr.code = HDN_ITEMDBLCLICKW;
4994     ret = SendMessageW(list, WM_NOTIFY, 0, (LPARAM)&nmhdr);
4995     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq,
4996                 "header notify, parent", FALSE);
4997     ok(nmhdr.hdr.code == HDN_ITEMDBLCLICKW, "Expected Unicode notification code\n");
4998     flush_sequences(sequences, NUM_MSG_SEQUENCES);
4999     nmhdr.hdr.code = HDN_DIVIDERDBLCLICKW;
5000     ret = SendMessageW(list, WM_NOTIFY, 0, (LPARAM)&nmhdr);
5001     ok_sequence(sequences, PARENT_SEQ_INDEX, parent_header_divider_dclick_seq,
5002                 "header notify, parent", TRUE);
5003     ok(nmhdr.hdr.code == HDN_DIVIDERDBLCLICKA, "Expected ANSI notification code\n");
5004     flush_sequences(sequences, NUM_MSG_SEQUENCES);
5005     nmhdr.hdr.code = HDN_BEGINTRACKW;
5006     ret = SendMessageW(list, WM_NOTIFY, 0, (LPARAM)&nmhdr);
5007     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq,
5008                 "header notify, parent", FALSE);
5009     ok(nmhdr.hdr.code == HDN_BEGINTRACKW, "Expected Unicode notification code\n");
5010     flush_sequences(sequences, NUM_MSG_SEQUENCES);
5011     nmhdr.hdr.code = HDN_ENDTRACKW;
5012     ret = SendMessageW(list, WM_NOTIFY, 0, (LPARAM)&nmhdr);
5013     parent_header_notify_seq[0].id = HDN_ENDTRACKA;
5014     ok_sequence(sequences, PARENT_SEQ_INDEX, parent_header_notify_seq,
5015                 "header notify, parent", FALSE);
5016     ok(nmhdr.hdr.code == HDN_ENDTRACKA, "Expected ANSI notification code\n");
5017     flush_sequences(sequences, NUM_MSG_SEQUENCES);
5018     nmhdr.hdr.code = HDN_TRACKW;
5019     ret = SendMessageW(list, WM_NOTIFY, 0, (LPARAM)&nmhdr);
5020     parent_header_notify_seq[0].id = HDN_TRACKA;
5021     ok_sequence(sequences, PARENT_SEQ_INDEX, parent_header_notify_seq,
5022                 "header notify, parent", FALSE);
5023     ok(nmhdr.hdr.code == HDN_TRACKA, "Expected ANSI notification code\n");
5024     flush_sequences(sequences, NUM_MSG_SEQUENCES);
5025     nmhdr.hdr.code = HDN_BEGINDRAG;
5026     ret = SendMessageW(list, WM_NOTIFY, 0, (LPARAM)&nmhdr);
5027     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq,
5028                 "header notify, parent", FALSE);
5029     flush_sequences(sequences, NUM_MSG_SEQUENCES);
5030     nmhdr.hdr.code = HDN_ENDDRAG;
5031     ret = SendMessageW(list, WM_NOTIFY, 0, (LPARAM)&nmhdr);
5032     parent_header_notify_seq[0].id = HDN_ENDDRAG;
5033     ok_sequence(sequences, PARENT_SEQ_INDEX, parent_header_notify_seq,
5034                 "header notify, parent", FALSE);
5035     flush_sequences(sequences, NUM_MSG_SEQUENCES);
5036     nmhdr.hdr.code = HDN_FILTERCHANGE;
5037     ret = SendMessageW(list, WM_NOTIFY, 0, (LPARAM)&nmhdr);
5038     parent_header_notify_seq[0].id = HDN_FILTERCHANGE;
5039     parent_header_notify_seq[0].flags |= optional; /* NT4 does not send this message */
5040     ok_sequence(sequences, PARENT_SEQ_INDEX, parent_header_notify_seq,
5041                 "header notify, parent", FALSE);
5042     parent_header_notify_seq[0].flags &= ~optional;
5043     flush_sequences(sequences, NUM_MSG_SEQUENCES);
5044     nmhdr.hdr.code = HDN_BEGINFILTEREDIT;
5045     ret = SendMessageW(list, WM_NOTIFY, 0, (LPARAM)&nmhdr);
5046     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq,
5047                 "header notify, parent", FALSE);
5048     flush_sequences(sequences, NUM_MSG_SEQUENCES);
5049     nmhdr.hdr.code = HDN_ENDFILTEREDIT;
5050     ret = SendMessageW(list, WM_NOTIFY, 0, (LPARAM)&nmhdr);
5051     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq,
5052                 "header notify, parent", FALSE);
5053     flush_sequences(sequences, NUM_MSG_SEQUENCES);
5054     nmhdr.hdr.code = HDN_ITEMSTATEICONCLICK;
5055     ret = SendMessageW(list, WM_NOTIFY, 0, (LPARAM)&nmhdr);
5056     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq,
5057                 "header notify, parent", FALSE);
5058     flush_sequences(sequences, NUM_MSG_SEQUENCES);
5059     nmhdr.hdr.code = HDN_ITEMKEYDOWN;
5060     ret = SendMessageW(list, WM_NOTIFY, 0, (LPARAM)&nmhdr);
5061     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq,
5062                 "header notify, parent", FALSE);
5063
5064     flush_sequences(sequences, NUM_MSG_SEQUENCES);
5065
5066     DestroyWindow(list);
5067 }
5068
5069 static void test_createdragimage(void)
5070 {
5071     HIMAGELIST himl;
5072     POINT pt;
5073     HWND list;
5074
5075     list = create_listview_control(LVS_ICON);
5076     ok(list != NULL, "failed to create listview window\n");
5077
5078     insert_item(list, 0);
5079
5080     /* NULL point */
5081     himl = (HIMAGELIST)SendMessageA(list, LVM_CREATEDRAGIMAGE, 0, 0);
5082     ok(himl == NULL, "got %p\n", himl);
5083
5084     himl = (HIMAGELIST)SendMessageA(list, LVM_CREATEDRAGIMAGE, 0, (LPARAM)&pt);
5085     ok(himl != NULL, "got %p\n", himl);
5086     ImageList_Destroy(himl);
5087
5088     DestroyWindow(list);
5089 }
5090
5091 static void test_dispinfo(void)
5092 {
5093     static const char testA[] = "TEST";
5094     WCHAR buff[10];
5095     LVITEMA item;
5096     HWND hwnd;
5097     DWORD ret;
5098
5099     hwnd = create_listview_control(LVS_ICON);
5100     ok(hwnd != NULL, "failed to create listview window\n");
5101
5102     insert_item(hwnd, 0);
5103
5104     memset(&item, 0, sizeof(item));
5105     item.pszText = LPSTR_TEXTCALLBACKA;
5106     ret = SendMessageA(hwnd, LVM_SETITEMTEXTA, 0, (LPARAM)&item);
5107     expect(1, ret);
5108
5109     g_disp_A_to_W = TRUE;
5110     item.pszText = (char*)buff;
5111     item.cchTextMax = sizeof(buff)/sizeof(WCHAR);
5112     ret = SendMessageA(hwnd, LVM_GETITEMTEXTA, 0, (LPARAM)&item);
5113     ok(ret == sizeof(testA)-1, "got %d, expected 4\n", ret);
5114     g_disp_A_to_W = FALSE;
5115
5116     ok(memcmp(item.pszText, testA, sizeof(testA)) == 0,
5117         "got %s, expected %s\n", item.pszText, testA);
5118
5119     DestroyWindow(hwnd);
5120 }
5121
5122 static void test_LVM_SETITEMTEXT(void)
5123 {
5124     static char testA[] = "TEST";
5125     LVITEMA item;
5126     HWND hwnd;
5127     DWORD ret;
5128
5129     hwnd = create_listview_control(LVS_ICON);
5130     ok(hwnd != NULL, "failed to create listview window\n");
5131
5132     insert_item(hwnd, 0);
5133
5134     /* null item pointer */
5135     ret = SendMessage(hwnd, LVM_SETITEMTEXTA, 0, 0);
5136     expect(FALSE, ret);
5137
5138     ret = SendMessage(hwnd, LVM_SETITEMTEXTW, 0, 0);
5139     expect(FALSE, ret);
5140
5141     /* index out of bounds */
5142     item.pszText = testA;
5143     item.cchTextMax = 0; /* ignored */
5144     item.iSubItem = 0;
5145
5146     ret = SendMessageA(hwnd, LVM_SETITEMTEXTA, 1, (LPARAM)&item);
5147     expect(FALSE, ret);
5148
5149     ret = SendMessageA(hwnd, LVM_SETITEMTEXTA, -1, (LPARAM)&item);
5150     expect(FALSE, ret);
5151
5152     ret = SendMessageA(hwnd, LVM_SETITEMTEXTA, 0, (LPARAM)&item);
5153     expect(TRUE, ret);
5154
5155     DestroyWindow(hwnd);
5156 }
5157
5158 START_TEST(listview)
5159 {
5160     HMODULE hComctl32;
5161     BOOL (WINAPI *pInitCommonControlsEx)(const INITCOMMONCONTROLSEX*);
5162
5163     ULONG_PTR ctx_cookie;
5164     HANDLE hCtx;
5165     HWND hwnd;
5166
5167     hComctl32 = GetModuleHandleA("comctl32.dll");
5168     pInitCommonControlsEx = (void*)GetProcAddress(hComctl32, "InitCommonControlsEx");
5169     if (pInitCommonControlsEx)
5170     {
5171         INITCOMMONCONTROLSEX iccex;
5172         iccex.dwSize = sizeof(iccex);
5173         iccex.dwICC  = ICC_LISTVIEW_CLASSES;
5174         pInitCommonControlsEx(&iccex);
5175     }
5176     else
5177         InitCommonControls();
5178
5179     init_msg_sequences(sequences, NUM_MSG_SEQUENCES);
5180
5181     hwndparent = create_parent_window(FALSE);
5182     flush_sequences(sequences, NUM_MSG_SEQUENCES);
5183
5184     g_is_below_5 = is_below_comctl_5();
5185
5186     test_header_notification();
5187     test_header_notification2();
5188     test_images();
5189     test_checkboxes();
5190     test_items();
5191     test_create();
5192     test_redraw();
5193     test_customdraw();
5194     test_icon_spacing();
5195     test_color();
5196     test_item_count();
5197     test_item_position();
5198     test_columns();
5199     test_getorigin();
5200     test_multiselect();
5201     test_getitemrect();
5202     test_subitem_rect();
5203     test_sorting();
5204     test_ownerdata();
5205     test_norecompute();
5206     test_nosortheader();
5207     test_setredraw();
5208     test_hittest();
5209     test_getviewrect();
5210     test_getitemposition();
5211     test_columnscreation();
5212     test_editbox();
5213     test_notifyformat();
5214     test_indentation();
5215     test_getitemspacing();
5216     test_getcolumnwidth();
5217     test_approximate_viewrect();
5218     test_finditem();
5219     test_hover();
5220     test_destroynotify();
5221     test_createdragimage();
5222     test_dispinfo();
5223     test_LVM_SETITEMTEXT();
5224
5225     if (!load_v6_module(&ctx_cookie, &hCtx))
5226     {
5227         DestroyWindow(hwndparent);
5228         return;
5229     }
5230
5231     /* this is a XP SP3 failure workaround */
5232     hwnd = CreateWindowExA(0, WC_LISTVIEW, "foo",
5233                            WS_CHILD | WS_BORDER | WS_VISIBLE | LVS_REPORT,
5234                            0, 0, 100, 100,
5235                            hwndparent, NULL, GetModuleHandleA(NULL), NULL);
5236     if (!IsWindow(hwnd))
5237     {
5238         win_skip("FIXME: failed to create ListView window.\n");
5239         unload_v6_module(ctx_cookie, hCtx);
5240         DestroyWindow(hwndparent);
5241         return;
5242     }
5243     else
5244         DestroyWindow(hwnd);
5245
5246     /* comctl32 version 6 tests start here */
5247     test_get_set_view();
5248     test_canceleditlabel();
5249     test_mapidindex();
5250     test_scrollnotify();
5251     test_LVS_EX_TRANSPARENTBKGND();
5252     test_LVS_EX_HEADERINALLVIEWS();
5253
5254     unload_v6_module(ctx_cookie, hCtx);
5255
5256     DestroyWindow(hwndparent);
5257 }