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