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