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