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