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