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