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