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