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