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