comctl32/listview: Position edit box after notification.
[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 "msg.h"
28
29 #define PARENT_SEQ_INDEX       0
30 #define PARENT_FULL_SEQ_INDEX  1
31 #define LISTVIEW_SEQ_INDEX     2
32 #define EDITBOX_SEQ_INDEX      3
33 #define NUM_MSG_SEQUENCES      4
34
35 #define LISTVIEW_ID 0
36 #define HEADER_ID   1
37
38 #define expect(expected, got) ok(got == expected, "Expected %d, got %d\n", expected, got)
39 #define expect2(expected1, expected2, got1, got2) ok(expected1 == got1 && expected2 == got2, \
40        "expected (%d,%d), got (%d,%d)\n", expected1, expected2, got1, got2)
41
42 HWND hwndparent;
43 /* prevents edit box creation, LVN_BEGINLABELEDIT return value */
44 BOOL blockEdit;
45 static HWND subclass_editbox(HWND hwndListview);
46
47 static struct msg_sequence *sequences[NUM_MSG_SEQUENCES];
48
49 static const struct message create_parent_wnd_seq[] = {
50     { WM_GETMINMAXINFO,     sent },
51     { WM_NCCREATE,          sent },
52     { WM_NCCALCSIZE,        sent|wparam, 0 },
53     { WM_CREATE,            sent },
54     { WM_SHOWWINDOW,        sent|wparam, 1 },
55     { WM_WINDOWPOSCHANGING, sent|wparam, 0 },
56     { WM_QUERYNEWPALETTE,   sent|optional },
57     { WM_WINDOWPOSCHANGING, sent|wparam, 0 },
58     { WM_WINDOWPOSCHANGED,  sent|optional },
59     { WM_NCCALCSIZE,        sent|wparam|optional, 1 },
60     { WM_ACTIVATEAPP,       sent|wparam, 1 },
61     { WM_NCACTIVATE,        sent|wparam, 1 },
62     { WM_ACTIVATE,          sent|wparam, 1 },
63     { WM_IME_SETCONTEXT,    sent|wparam|defwinproc|optional, 1 },
64     { WM_IME_NOTIFY,        sent|defwinproc|optional },
65     { WM_SETFOCUS,          sent|wparam|defwinproc, 0 },
66     /* Win9x adds SWP_NOZORDER below */
67     { WM_WINDOWPOSCHANGED,  sent, /*|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE*/ },
68     { WM_NCCALCSIZE,        sent|wparam|optional, 1 },
69     { WM_SIZE,              sent },
70     { WM_MOVE,              sent },
71     { 0 }
72 };
73
74 static const struct message redraw_listview_seq[] = {
75     { WM_PAINT,      sent|id,            0, 0, LISTVIEW_ID },
76     { WM_PAINT,      sent|id,            0, 0, HEADER_ID },
77     { WM_NCPAINT,    sent|id|defwinproc, 0, 0, HEADER_ID },
78     { WM_ERASEBKGND, sent|id|defwinproc, 0, 0, HEADER_ID },
79     { WM_NOTIFY,     sent|id|defwinproc, 0, 0, LISTVIEW_ID },
80     { WM_NCPAINT,    sent|id|defwinproc, 0, 0, LISTVIEW_ID },
81     { WM_ERASEBKGND, sent|id|defwinproc, 0, 0, LISTVIEW_ID },
82     { 0 }
83 };
84
85 static const struct message listview_icon_spacing_seq[] = {
86     { LVM_SETICONSPACING, sent|lparam, 0, MAKELPARAM(20, 30) },
87     { LVM_SETICONSPACING, sent|lparam, 0, MAKELPARAM(25, 35) },
88     { LVM_SETICONSPACING, sent|lparam, 0, MAKELPARAM(-1, -1) },
89     { 0 }
90 };
91
92 static const struct message listview_color_seq[] = {
93     { LVM_SETBKCOLOR,     sent|lparam, 0, RGB(0,0,0) },
94     { LVM_GETBKCOLOR,     sent },
95     { LVM_SETTEXTCOLOR,   sent|lparam, 0, RGB(0,0,0) },
96     { LVM_GETTEXTCOLOR,   sent },
97     { LVM_SETTEXTBKCOLOR, sent|lparam, 0, RGB(0,0,0) },
98     { LVM_GETTEXTBKCOLOR, sent },
99
100     { LVM_SETBKCOLOR,     sent|lparam, 0, RGB(100,50,200) },
101     { LVM_GETBKCOLOR,     sent },
102     { LVM_SETTEXTCOLOR,   sent|lparam, 0, RGB(100,50,200) },
103     { LVM_GETTEXTCOLOR,   sent },
104     { LVM_SETTEXTBKCOLOR, sent|lparam, 0, RGB(100,50,200) },
105     { LVM_GETTEXTBKCOLOR, sent },
106
107     { LVM_SETBKCOLOR,     sent|lparam, 0, CLR_NONE },
108     { LVM_GETBKCOLOR,     sent },
109     { LVM_SETTEXTCOLOR,   sent|lparam, 0, CLR_NONE },
110     { LVM_GETTEXTCOLOR,   sent },
111     { LVM_SETTEXTBKCOLOR, sent|lparam, 0, CLR_NONE },
112     { LVM_GETTEXTBKCOLOR, sent },
113
114     { LVM_SETBKCOLOR,     sent|lparam, 0, RGB(255,255,255) },
115     { LVM_GETBKCOLOR,     sent },
116     { LVM_SETTEXTCOLOR,   sent|lparam, 0, RGB(255,255,255) },
117     { LVM_GETTEXTCOLOR,   sent },
118     { LVM_SETTEXTBKCOLOR, sent|lparam, 0, RGB(255,255,255) },
119     { LVM_GETTEXTBKCOLOR, sent },
120     { 0 }
121 };
122
123 static const struct message listview_item_count_seq[] = {
124     { LVM_GETITEMCOUNT,   sent },
125     { LVM_INSERTITEM,     sent },
126     { LVM_INSERTITEM,     sent },
127     { LVM_INSERTITEM,     sent },
128     { LVM_GETITEMCOUNT,   sent },
129     { LVM_DELETEITEM,     sent|wparam, 2 },
130     { LVM_GETITEMCOUNT,   sent },
131     { LVM_DELETEALLITEMS, sent },
132     { LVM_GETITEMCOUNT,   sent },
133     { LVM_INSERTITEM,     sent },
134     { LVM_INSERTITEM,     sent },
135     { LVM_GETITEMCOUNT,   sent },
136     { LVM_INSERTITEM,     sent },
137     { LVM_GETITEMCOUNT,   sent },
138     { 0 }
139 };
140
141 static const struct message listview_itempos_seq[] = {
142     { LVM_INSERTITEM,      sent },
143     { LVM_INSERTITEM,      sent },
144     { LVM_INSERTITEM,      sent },
145     { LVM_SETITEMPOSITION, sent|wparam|lparam, 1, MAKELPARAM(10,5) },
146     { LVM_GETITEMPOSITION, sent|wparam,        1 },
147     { LVM_SETITEMPOSITION, sent|wparam|lparam, 2, MAKELPARAM(0,0) },
148     { LVM_GETITEMPOSITION, sent|wparam,        2 },
149     { LVM_SETITEMPOSITION, sent|wparam|lparam, 0, MAKELPARAM(20,20) },
150     { LVM_GETITEMPOSITION, sent|wparam,        0 },
151     { 0 }
152 };
153
154 static const struct message listview_ownerdata_switchto_seq[] = {
155     { WM_STYLECHANGING,    sent },
156     { WM_STYLECHANGED,     sent },
157     { 0 }
158 };
159
160 static const struct message listview_getorderarray_seq[] = {
161     { LVM_GETCOLUMNORDERARRAY, sent|id|wparam, 2, 0, LISTVIEW_ID },
162     { HDM_GETORDERARRAY,       sent|id|wparam, 2, 0, HEADER_ID },
163     { 0 }
164 };
165
166 static const struct message empty_seq[] = {
167     { 0 }
168 };
169
170 static const struct message forward_erasebkgnd_parent_seq[] = {
171     { WM_ERASEBKGND, sent },
172     { 0 }
173 };
174
175 static const struct message ownderdata_select_focus_parent_seq[] = {
176     { WM_NOTIFY, sent|id, 0, 0, LVN_ITEMCHANGED },
177     { WM_NOTIFY, sent|id, 0, 0, LVN_GETDISPINFOA },
178     { 0 }
179 };
180
181 static const struct message textcallback_set_again_parent_seq[] = {
182     { WM_NOTIFY, sent|id, 0, 0, LVN_ITEMCHANGING },
183     { WM_NOTIFY, sent|id, 0, 0, LVN_ITEMCHANGED  },
184     { 0 }
185 };
186
187 static const struct message single_getdispinfo_parent_seq[] = {
188     { WM_NOTIFY, sent|id, 0, 0, LVN_GETDISPINFOA },
189     { 0 }
190 };
191
192 static const struct message getitemposition_seq1[] = {
193     { LVM_GETITEMPOSITION, sent|id, 0, 0, LISTVIEW_ID },
194     { 0 }
195 };
196
197 static const struct message getitemposition_seq2[] = {
198     { LVM_GETITEMPOSITION, sent|id, 0, 0, LISTVIEW_ID },
199     { HDM_GETITEMRECT, sent|id, 0, 0, HEADER_ID },
200     { 0 }
201 };
202
203 static const struct message editbox_create_pos[] = {
204     /* sequence sent after LVN_BEGINLABELEDIT */
205     { WM_WINDOWPOSCHANGING, sent },
206     { WM_NCCALCSIZE, sent },
207     { WM_WINDOWPOSCHANGED, sent },
208     { WM_MOVE, sent|defwinproc },
209     { WM_SIZE, sent|defwinproc },
210     /* the rest is todo */
211     { WM_WINDOWPOSCHANGING, sent },
212     { WM_WINDOWPOSCHANGED, sent },
213     { 0 }
214 };
215
216 struct subclass_info
217 {
218     WNDPROC oldproc;
219 };
220
221 static LRESULT WINAPI parent_wnd_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
222 {
223     static LONG defwndproc_counter = 0;
224     LRESULT ret;
225     struct message msg;
226
227     msg.message = message;
228     msg.flags = sent|wparam|lparam;
229     if (defwndproc_counter) msg.flags |= defwinproc;
230     msg.wParam = wParam;
231     msg.lParam = lParam;
232     if (message == WM_NOTIFY && lParam) msg.id = ((NMHDR*)lParam)->code;
233
234     /* log system messages, except for painting */
235     if (message < WM_USER &&
236         message != WM_PAINT &&
237         message != WM_ERASEBKGND &&
238         message != WM_NCPAINT &&
239         message != WM_NCHITTEST &&
240         message != WM_GETTEXT &&
241         message != WM_GETICON &&
242         message != WM_DEVICECHANGE)
243     {
244         trace("parent: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
245
246         add_message(sequences, PARENT_SEQ_INDEX, &msg);
247     }
248     add_message(sequences, PARENT_FULL_SEQ_INDEX, &msg);
249
250     if (message == WM_NOTIFY && lParam)
251     {
252         switch (((NMHDR*)lParam)->code)
253         {
254         case LVN_BEGINLABELEDIT:
255             /* subclass edit box */
256             if (!blockEdit)
257                 subclass_editbox(((NMHDR*)lParam)->hwndFrom);
258
259             return blockEdit;
260
261         case LVN_ENDLABELEDIT:
262             /* always accept new item text */
263             return TRUE;
264         }
265     }
266
267     defwndproc_counter++;
268     ret = DefWindowProcA(hwnd, message, wParam, lParam);
269     defwndproc_counter--;
270
271     return ret;
272 }
273
274 static BOOL register_parent_wnd_class(void)
275 {
276     WNDCLASSA cls;
277
278     cls.style = 0;
279     cls.lpfnWndProc = parent_wnd_proc;
280     cls.cbClsExtra = 0;
281     cls.cbWndExtra = 0;
282     cls.hInstance = GetModuleHandleA(NULL);
283     cls.hIcon = 0;
284     cls.hCursor = LoadCursorA(0, IDC_ARROW);
285     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
286     cls.lpszMenuName = NULL;
287     cls.lpszClassName = "Listview test parent class";
288     return RegisterClassA(&cls);
289 }
290
291 static HWND create_parent_window(void)
292 {
293     if (!register_parent_wnd_class())
294         return NULL;
295
296     blockEdit = FALSE;
297
298     return CreateWindowEx(0, "Listview test parent class",
299                           "Listview test parent window",
300                           WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
301                           WS_MAXIMIZEBOX | WS_VISIBLE,
302                           0, 0, 100, 100,
303                           GetDesktopWindow(), NULL, GetModuleHandleA(NULL), NULL);
304 }
305
306 static LRESULT WINAPI listview_subclass_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
307 {
308     struct subclass_info *info = (struct subclass_info *)GetWindowLongPtrA(hwnd, GWLP_USERDATA);
309     static LONG defwndproc_counter = 0;
310     LRESULT ret;
311     struct message msg;
312
313     trace("listview: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
314
315     /* some debug output for style changing */
316     if ((message == WM_STYLECHANGING ||
317          message == WM_STYLECHANGED) && lParam)
318     {
319         STYLESTRUCT *style = (STYLESTRUCT*)lParam;
320         trace("\told style: 0x%08x, new style: 0x%08x\n", style->styleOld, style->styleNew);
321     }
322
323     msg.message = message;
324     msg.flags = sent|wparam|lparam;
325     if (defwndproc_counter) msg.flags |= defwinproc;
326     msg.wParam = wParam;
327     msg.lParam = lParam;
328     msg.id = LISTVIEW_ID;
329     add_message(sequences, LISTVIEW_SEQ_INDEX, &msg);
330
331     defwndproc_counter++;
332     ret = CallWindowProcA(info->oldproc, hwnd, message, wParam, lParam);
333     defwndproc_counter--;
334     return ret;
335 }
336
337 static HWND create_listview_control(DWORD style)
338 {
339     struct subclass_info *info;
340     HWND hwnd;
341     RECT rect;
342
343     info = HeapAlloc(GetProcessHeap(), 0, sizeof(struct subclass_info));
344     if (!info)
345         return NULL;
346
347     GetClientRect(hwndparent, &rect);
348     hwnd = CreateWindowExA(0, WC_LISTVIEW, "foo",
349                            WS_CHILD | WS_BORDER | WS_VISIBLE | LVS_REPORT | style,
350                            0, 0, rect.right, rect.bottom,
351                            hwndparent, NULL, GetModuleHandleA(NULL), NULL);
352     ok(hwnd != NULL, "gle=%d\n", GetLastError());
353
354     if (!hwnd)
355     {
356         HeapFree(GetProcessHeap(), 0, info);
357         return NULL;
358     }
359
360     info->oldproc = (WNDPROC)SetWindowLongPtrA(hwnd, GWLP_WNDPROC,
361                                             (LONG_PTR)listview_subclass_proc);
362     SetWindowLongPtrA(hwnd, GWLP_USERDATA, (LONG_PTR)info);
363
364     return hwnd;
365 }
366
367 static HWND create_custom_listview_control(DWORD style)
368 {
369     struct subclass_info *info;
370     HWND hwnd;
371     RECT rect;
372
373     info = HeapAlloc(GetProcessHeap(), 0, sizeof(struct subclass_info));
374     if (!info)
375         return NULL;
376
377     GetClientRect(hwndparent, &rect);
378     hwnd = CreateWindowExA(0, WC_LISTVIEW, "foo",
379                            WS_CHILD | WS_BORDER | WS_VISIBLE | style,
380                            0, 0, rect.right, rect.bottom,
381                            hwndparent, NULL, GetModuleHandleA(NULL), NULL);
382     ok(hwnd != NULL, "gle=%d\n", GetLastError());
383
384     if (!hwnd)
385     {
386         HeapFree(GetProcessHeap(), 0, info);
387         return NULL;
388     }
389
390     info->oldproc = (WNDPROC)SetWindowLongPtrA(hwnd, GWLP_WNDPROC,
391                                             (LONG_PTR)listview_subclass_proc);
392     SetWindowLongPtrA(hwnd, GWLP_USERDATA, (LONG_PTR)info);
393
394     return hwnd;
395 }
396
397 static LRESULT WINAPI header_subclass_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
398 {
399     struct subclass_info *info = (struct subclass_info *)GetWindowLongPtrA(hwnd, GWLP_USERDATA);
400     static LONG defwndproc_counter = 0;
401     LRESULT ret;
402     struct message msg;
403
404     trace("header: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
405
406     msg.message = message;
407     msg.flags = sent|wparam|lparam;
408     if (defwndproc_counter) msg.flags |= defwinproc;
409     msg.wParam = wParam;
410     msg.lParam = lParam;
411     msg.id = HEADER_ID;
412     add_message(sequences, LISTVIEW_SEQ_INDEX, &msg);
413
414     defwndproc_counter++;
415     ret = CallWindowProcA(info->oldproc, hwnd, message, wParam, lParam);
416     defwndproc_counter--;
417     return ret;
418 }
419
420 static HWND subclass_header(HWND hwndListview)
421 {
422     struct subclass_info *info;
423     HWND hwnd;
424
425     info = HeapAlloc(GetProcessHeap(), 0, sizeof(struct subclass_info));
426     if (!info)
427         return NULL;
428
429     hwnd = ListView_GetHeader(hwndListview);
430     info->oldproc = (WNDPROC)SetWindowLongPtrA(hwnd, GWLP_WNDPROC,
431                                             (LONG_PTR)header_subclass_proc);
432     SetWindowLongPtrA(hwnd, GWLP_USERDATA, (LONG_PTR)info);
433
434     return hwnd;
435 }
436
437 static LRESULT WINAPI editbox_subclass_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
438 {
439     struct subclass_info *info = (struct subclass_info *)GetWindowLongPtrA(hwnd, GWLP_USERDATA);
440     static LONG defwndproc_counter = 0;
441     LRESULT ret;
442     struct message msg;
443
444     msg.message = message;
445     msg.flags = sent|wparam|lparam;
446     if (defwndproc_counter) msg.flags |= defwinproc;
447     msg.wParam = wParam;
448     msg.lParam = lParam;
449
450     /* all we need is sizing */
451     if (message == WM_WINDOWPOSCHANGING ||
452         message == WM_NCCALCSIZE ||
453         message == WM_WINDOWPOSCHANGED ||
454         message == WM_MOVE ||
455         message == WM_SIZE)
456     {
457         add_message(sequences, EDITBOX_SEQ_INDEX, &msg);
458     }
459
460     defwndproc_counter++;
461     ret = CallWindowProcA(info->oldproc, hwnd, message, wParam, lParam);
462     defwndproc_counter--;
463     return ret;
464 }
465
466 static HWND subclass_editbox(HWND hwndListview)
467 {
468     struct subclass_info *info;
469     HWND hwnd;
470
471     info = HeapAlloc(GetProcessHeap(), 0, sizeof(struct subclass_info));
472     if (!info)
473         return NULL;
474
475     hwnd = (HWND)SendMessage(hwndListview, LVM_GETEDITCONTROL, 0, 0);
476     info->oldproc = (WNDPROC)SetWindowLongPtrA(hwnd, GWLP_WNDPROC,
477                                             (LONG_PTR)editbox_subclass_proc);
478     SetWindowLongPtrA(hwnd, GWLP_USERDATA, (LONG_PTR)info);
479
480     return hwnd;
481 }
482
483 /* Performs a single LVM_HITTEST test */
484 static void test_lvm_hittest(HWND hwnd, INT x, INT y, INT item, UINT flags,
485                              BOOL todo_item, BOOL todo_flags, int line)
486 {
487     LVHITTESTINFO lpht;
488     DWORD ret;
489
490     lpht.pt.x = x;
491     lpht.pt.y = y;
492     lpht.iSubItem = 10;
493
494     trace("hittesting pt=(%d,%d)\n", lpht.pt.x, lpht.pt.y);
495     ret = SendMessage(hwnd, LVM_HITTEST, 0, (LPARAM)&lpht);
496
497     if (todo_item)
498     {
499         todo_wine
500         {
501             ok_(__FILE__, line)(ret == item, "Expected %d item, got %d\n", item, ret);
502             ok_(__FILE__, line)(lpht.iItem == item, "Expected %d item, got %d\n", item, lpht.iItem);
503             ok_(__FILE__, line)(lpht.iSubItem == 10, "Expected subitem not overwrited\n");
504         }
505     }
506     else
507     {
508         ok_(__FILE__, line)(ret == item, "Expected %d item, got %d\n", item, ret);
509         ok_(__FILE__, line)(lpht.iItem == item, "Expected %d item, got %d\n", item, lpht.iItem);
510         ok_(__FILE__, line)(lpht.iSubItem == 10, "Expected subitem not overwrited\n");
511     }
512
513     if (todo_flags)
514     {
515         todo_wine
516             ok_(__FILE__, line)(lpht.flags == flags, "Expected flags %x, got %x\n", flags, lpht.flags);
517     }
518     else
519         ok_(__FILE__, line)(lpht.flags == flags, "Expected flags %x, got %x\n", flags, lpht.flags);
520 }
521
522 /* Performs a single LVM_SUBITEMHITTEST test */
523 static void test_lvm_subitemhittest(HWND hwnd, INT x, INT y, INT item, INT subitem, UINT flags,
524                                     BOOL todo_item, BOOL todo_subitem, BOOL todo_flags, int line)
525 {
526     LVHITTESTINFO lpht;
527     DWORD ret;
528
529     lpht.pt.x = x;
530     lpht.pt.y = y;
531
532     trace("subhittesting pt=(%d,%d)\n", lpht.pt.x, lpht.pt.y);
533     ret = SendMessage(hwnd, LVM_SUBITEMHITTEST, 0, (LPARAM)&lpht);
534
535     if (todo_item)
536     {
537         todo_wine
538         {
539             ok_(__FILE__, line)(ret == item, "Expected %d item, got %d\n", item, ret);
540             ok_(__FILE__, line)(lpht.iItem == item, "Expected %d item, got %d\n", item, lpht.iItem);
541         }
542     }
543     else
544     {
545         ok_(__FILE__, line)(ret == item, "Expected %d item, got %d\n", item, ret);
546         ok_(__FILE__, line)(lpht.iItem == item, "Expected %d item, got %d\n", item, lpht.iItem);
547     }
548
549     if (todo_subitem)
550     {
551         todo_wine
552             ok_(__FILE__, line)(lpht.iSubItem == subitem, "Expected subitem %d, got %d\n", subitem, lpht.iSubItem);
553     }
554     else
555         ok_(__FILE__, line)(lpht.iSubItem == subitem, "Expected subitem %d, got %d\n", subitem, lpht.iSubItem);
556
557     if (todo_flags)
558     {
559         todo_wine
560             ok_(__FILE__, line)(lpht.flags == flags, "Expected flags %x, got %x\n", flags, lpht.flags);
561     }
562     else
563         ok_(__FILE__, line)(lpht.flags == flags, "Expected flags %x, got %x\n", flags, lpht.flags);
564 }
565
566 static void test_images(void)
567 {
568     HWND hwnd;
569     DWORD r;
570     LVITEM item;
571     HIMAGELIST himl;
572     HBITMAP hbmp;
573     RECT r1, r2;
574     static CHAR hello[] = "hello";
575
576     himl = ImageList_Create(40, 40, 0, 4, 4);
577     ok(himl != NULL, "failed to create imagelist\n");
578
579     hbmp = CreateBitmap(40, 40, 1, 1, NULL);
580     ok(hbmp != NULL, "failed to create bitmap\n");
581
582     r = ImageList_Add(himl, hbmp, 0);
583     ok(r == 0, "should be zero\n");
584
585     hwnd = CreateWindowEx(0, "SysListView32", "foo", LVS_OWNERDRAWFIXED, 
586                 10, 10, 100, 200, hwndparent, NULL, NULL, NULL);
587     ok(hwnd != NULL, "failed to create listview window\n");
588
589     r = SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, 0,
590                     LVS_EX_UNDERLINEHOT | LVS_EX_FLATSB | LVS_EX_ONECLICKACTIVATE);
591
592     ok(r == 0, "should return zero\n");
593
594     r = SendMessage(hwnd, LVM_SETIMAGELIST, 0, (LPARAM)himl);
595     ok(r == 0, "should return zero\n");
596
597     r = SendMessage(hwnd, LVM_SETICONSPACING, 0, MAKELONG(100,50));
598     /* returns dimensions */
599
600     r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
601     ok(r == 0, "should be zero items\n");
602
603     item.mask = LVIF_IMAGE | LVIF_TEXT;
604     item.iItem = 0;
605     item.iSubItem = 1;
606     item.iImage = 0;
607     item.pszText = 0;
608     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
609     ok(r == -1, "should fail\n");
610
611     item.iSubItem = 0;
612     item.pszText = hello;
613     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
614     ok(r == 0, "should not fail\n");
615
616     memset(&r1, 0, sizeof r1);
617     r1.left = LVIR_ICON;
618     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM) &r1);
619
620     r = SendMessage(hwnd, LVM_DELETEALLITEMS, 0, 0);
621     ok(r == TRUE, "should not fail\n");
622
623     item.iSubItem = 0;
624     item.pszText = hello;
625     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
626     ok(r == 0, "should not fail\n");
627
628     memset(&r2, 0, sizeof r2);
629     r2.left = LVIR_ICON;
630     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM) &r2);
631
632     ok(!memcmp(&r1, &r2, sizeof r1), "rectangle should be the same\n");
633
634     DestroyWindow(hwnd);
635 }
636
637 static void test_checkboxes(void)
638 {
639     HWND hwnd;
640     LVITEMA item;
641     DWORD r;
642     static CHAR text[]  = "Text",
643                 text2[] = "Text2",
644                 text3[] = "Text3";
645
646     hwnd = CreateWindowEx(0, "SysListView32", "foo", LVS_REPORT, 
647                 10, 10, 100, 200, hwndparent, NULL, NULL, NULL);
648     ok(hwnd != NULL, "failed to create listview window\n");
649
650     /* first without LVS_EX_CHECKBOXES set and an item and check that state is preserved */
651     item.mask = LVIF_TEXT | LVIF_STATE;
652     item.stateMask = 0xffff;
653     item.state = 0xfccc;
654     item.iItem = 0;
655     item.iSubItem = 0;
656     item.pszText = text;
657     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
658     ok(r == 0, "ret %d\n", r);
659
660     item.iItem = 0;
661     item.mask = LVIF_STATE;
662     item.stateMask = 0xffff;
663     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
664     ok(item.state == 0xfccc, "state %x\n", item.state);
665
666     /* Don't set LVIF_STATE */
667     item.mask = LVIF_TEXT;
668     item.stateMask = 0xffff;
669     item.state = 0xfccc;
670     item.iItem = 1;
671     item.iSubItem = 0;
672     item.pszText = text;
673     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
674     ok(r == 1, "ret %d\n", r);
675
676     item.iItem = 1;
677     item.mask = LVIF_STATE;
678     item.stateMask = 0xffff;
679     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
680     ok(item.state == 0, "state %x\n", item.state);
681
682     r = SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_CHECKBOXES, LVS_EX_CHECKBOXES);
683     ok(r == 0, "should return zero\n");
684
685     /* Having turned on checkboxes, check that all existing items are set to 0x1000 (unchecked) */
686     item.iItem = 0;
687     item.mask = LVIF_STATE;
688     item.stateMask = 0xffff;
689     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
690     ok(item.state == 0x1ccc, "state %x\n", item.state);
691
692     /* Now add an item without specifying a state and check that its state goes to 0x1000 */
693     item.iItem = 2;
694     item.mask = LVIF_TEXT;
695     item.state = 0;
696     item.pszText = text2;
697     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
698     ok(r == 2, "ret %d\n", r);
699
700     item.iItem = 2;
701     item.mask = LVIF_STATE;
702     item.stateMask = 0xffff;
703     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
704     ok(item.state == 0x1000, "state %x\n", item.state);
705
706     /* Add a further item this time specifying a state and still its state goes to 0x1000 */
707     item.iItem = 3;
708     item.mask = LVIF_TEXT | LVIF_STATE;
709     item.stateMask = 0xffff;
710     item.state = 0x2aaa;
711     item.pszText = text3;
712     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
713     ok(r == 3, "ret %d\n", r);
714
715     item.iItem = 3;
716     item.mask = LVIF_STATE;
717     item.stateMask = 0xffff;
718     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
719     ok(item.state == 0x1aaa, "state %x\n", item.state);
720
721     /* Set an item's state to checked */
722     item.iItem = 3;
723     item.mask = LVIF_STATE;
724     item.stateMask = 0xf000;
725     item.state = 0x2000;
726     r = SendMessage(hwnd, LVM_SETITEMA, 0, (LPARAM) &item);
727
728     item.iItem = 3;
729     item.mask = LVIF_STATE;
730     item.stateMask = 0xffff;
731     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
732     ok(item.state == 0x2aaa, "state %x\n", item.state);
733
734     /* Check that only the bits we asked for are returned,
735      * and that all the others are set to zero
736      */
737     item.iItem = 3;
738     item.mask = LVIF_STATE;
739     item.stateMask = 0xf000;
740     item.state = 0xffff;
741     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
742     ok(item.state == 0x2000, "state %x\n", item.state);
743
744     /* Set the style again and check that doesn't change an item's state */
745     r = SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_CHECKBOXES, LVS_EX_CHECKBOXES);
746     ok(r == LVS_EX_CHECKBOXES, "ret %x\n", r);
747
748     item.iItem = 3;
749     item.mask = LVIF_STATE;
750     item.stateMask = 0xffff;
751     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
752     ok(item.state == 0x2aaa, "state %x\n", item.state);
753
754     /* Unsetting the checkbox extended style doesn't change an item's state */
755     r = SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_CHECKBOXES, 0);
756     ok(r == LVS_EX_CHECKBOXES, "ret %x\n", r);
757
758     item.iItem = 3;
759     item.mask = LVIF_STATE;
760     item.stateMask = 0xffff;
761     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
762     ok(item.state == 0x2aaa, "state %x\n", item.state);
763
764     /* Now setting the style again will change an item's state */
765     r = SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_CHECKBOXES, LVS_EX_CHECKBOXES);
766     ok(r == 0, "ret %x\n", r);
767
768     item.iItem = 3;
769     item.mask = LVIF_STATE;
770     item.stateMask = 0xffff;
771     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
772     ok(item.state == 0x1aaa, "state %x\n", item.state);
773
774     /* Toggle checkbox tests (bug 9934) */
775     memset (&item, 0xcc, sizeof(item));
776     item.mask = LVIF_STATE;
777     item.iItem = 3;
778     item.iSubItem = 0;
779     item.state = LVIS_FOCUSED;
780     item.stateMask = LVIS_FOCUSED;
781     r = SendMessage(hwnd, LVM_SETITEM, 0, (LPARAM) &item);
782     expect(1, r);
783
784     item.iItem = 3;
785     item.mask = LVIF_STATE;
786     item.stateMask = 0xffff;
787     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
788     ok(item.state == 0x1aab, "state %x\n", item.state);
789
790     r = SendMessage(hwnd, WM_KEYDOWN, VK_SPACE, 0);
791     expect(0, r);
792     r = SendMessage(hwnd, WM_KEYUP, VK_SPACE, 0);
793     expect(0, r);
794
795     item.iItem = 3;
796     item.mask = LVIF_STATE;
797     item.stateMask = 0xffff;
798     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
799     ok(item.state == 0x2aab, "state %x\n", item.state);
800
801     r = SendMessage(hwnd, WM_KEYDOWN, VK_SPACE, 0);
802     expect(0, r);
803     r = SendMessage(hwnd, WM_KEYUP, VK_SPACE, 0);
804     expect(0, r);
805
806     item.iItem = 3;
807     item.mask = LVIF_STATE;
808     item.stateMask = 0xffff;
809     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
810     ok(item.state == 0x1aab, "state %x\n", item.state);
811
812     DestroyWindow(hwnd);
813 }
814
815 static void insert_column(HWND hwnd, int idx)
816 {
817     LVCOLUMN column;
818     DWORD rc;
819
820     memset(&column, 0xcc, sizeof(column));
821     column.mask = LVCF_SUBITEM;
822     column.iSubItem = idx;
823
824     rc = ListView_InsertColumn(hwnd, idx, &column);
825     expect(idx, rc);
826 }
827
828 static void insert_item(HWND hwnd, int idx)
829 {
830     static CHAR text[] = "foo";
831
832     LVITEMA item;
833     DWORD rc;
834
835     memset(&item, 0xcc, sizeof (item));
836     item.mask = LVIF_TEXT;
837     item.iItem = idx;
838     item.iSubItem = 0;
839     item.pszText = text;
840
841     rc = ListView_InsertItem(hwnd, &item);
842     expect(idx, rc);
843 }
844
845 static void test_items(void)
846 {
847     const LPARAM lparamTest = 0x42;
848     HWND hwnd;
849     LVITEMA item;
850     DWORD r;
851     static CHAR text[] = "Text";
852
853     hwnd = CreateWindowEx(0, "SysListView32", "foo", LVS_REPORT,
854                 10, 10, 100, 200, hwndparent, NULL, NULL, NULL);
855     ok(hwnd != NULL, "failed to create listview window\n");
856
857     /*
858      * Test setting/getting item params
859      */
860
861     /* Set up two columns */
862     insert_column(hwnd, 0);
863     insert_column(hwnd, 1);
864
865     /* LVIS_SELECTED with zero stateMask */
866     /* set */
867     memset (&item, 0, sizeof (item));
868     item.mask = LVIF_STATE;
869     item.state = LVIS_SELECTED;
870     item.stateMask = 0;
871     item.iItem = 0;
872     item.iSubItem = 0;
873     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
874     ok(r == 0, "ret %d\n", r);
875     /* get */
876     memset (&item, 0xcc, sizeof (item));
877     item.mask = LVIF_STATE;
878     item.stateMask = LVIS_SELECTED;
879     item.state = 0;
880     item.iItem = 0;
881     item.iSubItem = 0;
882     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
883     ok(r != 0, "ret %d\n", r);
884     ok(item.state & LVIS_SELECTED, "Expected LVIS_SELECTED\n");
885     SendMessage(hwnd, LVM_DELETEITEM, 0, 0);
886
887     /* LVIS_SELECTED with zero stateMask */
888     /* set */
889     memset (&item, 0, sizeof (item));
890     item.mask = LVIF_STATE;
891     item.state = LVIS_FOCUSED;
892     item.stateMask = 0;
893     item.iItem = 0;
894     item.iSubItem = 0;
895     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
896     ok(r == 0, "ret %d\n", r);
897     /* get */
898     memset (&item, 0xcc, sizeof (item));
899     item.mask = LVIF_STATE;
900     item.stateMask = LVIS_FOCUSED;
901     item.state = 0;
902     item.iItem = 0;
903     item.iSubItem = 0;
904     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
905     ok(r != 0, "ret %d\n", r);
906     ok(item.state & LVIS_FOCUSED, "Expected LVIS_FOCUSED\n");
907     SendMessage(hwnd, LVM_DELETEITEM, 0, 0);
908
909     /* LVIS_CUT with LVIS_FOCUSED stateMask */
910     /* set */
911     memset (&item, 0, sizeof (item));
912     item.mask = LVIF_STATE;
913     item.state = LVIS_CUT;
914     item.stateMask = LVIS_FOCUSED;
915     item.iItem = 0;
916     item.iSubItem = 0;
917     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
918     ok(r == 0, "ret %d\n", r);
919     /* get */
920     memset (&item, 0xcc, sizeof (item));
921     item.mask = LVIF_STATE;
922     item.stateMask = LVIS_CUT;
923     item.state = 0;
924     item.iItem = 0;
925     item.iSubItem = 0;
926     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
927     ok(r != 0, "ret %d\n", r);
928     ok(item.state & LVIS_CUT, "Expected LVIS_CUT\n");
929     SendMessage(hwnd, LVM_DELETEITEM, 0, 0);
930
931     /* Insert an item with just a param */
932     memset (&item, 0xcc, sizeof (item));
933     item.mask = LVIF_PARAM;
934     item.iItem = 0;
935     item.iSubItem = 0;
936     item.lParam = lparamTest;
937     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
938     ok(r == 0, "ret %d\n", r);
939
940     /* Test getting of the param */
941     memset (&item, 0xcc, sizeof (item));
942     item.mask = LVIF_PARAM;
943     item.iItem = 0;
944     item.iSubItem = 0;
945     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
946     ok(r != 0, "ret %d\n", r);
947     ok(item.lParam == lparamTest, "got lParam %lx, expected %lx\n", item.lParam, lparamTest);
948
949     /* Set up a subitem */
950     memset (&item, 0xcc, sizeof (item));
951     item.mask = LVIF_TEXT;
952     item.iItem = 0;
953     item.iSubItem = 1;
954     item.pszText = text;
955     r = SendMessage(hwnd, LVM_SETITEMA, 0, (LPARAM) &item);
956     ok(r != 0, "ret %d\n", r);
957
958     /* Query param from subitem: returns main item param */
959     memset (&item, 0xcc, sizeof (item));
960     item.mask = LVIF_PARAM;
961     item.iItem = 0;
962     item.iSubItem = 1;
963     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
964     ok(r != 0, "ret %d\n", r);
965     ok(item.lParam == lparamTest, "got lParam %lx, expected %lx\n", item.lParam, lparamTest);
966
967     /* Set up param on first subitem: no effect */
968     memset (&item, 0xcc, sizeof (item));
969     item.mask = LVIF_PARAM;
970     item.iItem = 0;
971     item.iSubItem = 1;
972     item.lParam = lparamTest+1;
973     r = SendMessage(hwnd, LVM_SETITEMA, 0, (LPARAM) &item);
974     ok(r == 0, "ret %d\n", r);
975
976     /* Query param from subitem again: should still return main item param */
977     memset (&item, 0xcc, sizeof (item));
978     item.mask = LVIF_PARAM;
979     item.iItem = 0;
980     item.iSubItem = 1;
981     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
982     ok(r != 0, "ret %d\n", r);
983     ok(item.lParam == lparamTest, "got lParam %lx, expected %lx\n", item.lParam, lparamTest);
984
985     /**** Some tests of state highlighting ****/
986     memset (&item, 0xcc, sizeof (item));
987     item.mask = LVIF_STATE;
988     item.iItem = 0;
989     item.iSubItem = 0;
990     item.state = LVIS_SELECTED;
991     item.stateMask = LVIS_SELECTED | LVIS_DROPHILITED;
992     r = SendMessage(hwnd, LVM_SETITEM, 0, (LPARAM) &item);
993     ok(r != 0, "ret %d\n", r);
994     item.iSubItem = 1;
995     item.state = LVIS_DROPHILITED;
996     r = SendMessage(hwnd, LVM_SETITEM, 0, (LPARAM) &item);
997     ok(r != 0, "ret %d\n", r);
998
999     memset (&item, 0xcc, sizeof (item));
1000     item.mask = LVIF_STATE;
1001     item.iItem = 0;
1002     item.iSubItem = 0;
1003     item.stateMask = -1;
1004     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
1005     ok(r != 0, "ret %d\n", r);
1006     ok(item.state == LVIS_SELECTED, "got state %x, expected %x\n", item.state, LVIS_SELECTED);
1007     item.iSubItem = 1;
1008     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
1009     ok(r != 0, "ret %d\n", r);
1010     todo_wine ok(item.state == LVIS_DROPHILITED, "got state %x, expected %x\n", item.state, LVIS_DROPHILITED);
1011
1012     /* some notnull but meaningless masks */
1013     memset (&item, 0, sizeof(item));
1014     item.mask = LVIF_NORECOMPUTE;
1015     item.iItem = 0;
1016     item.iSubItem = 0;
1017     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
1018     ok(r != 0, "ret %d\n", r);
1019     memset (&item, 0, sizeof(item));
1020     item.mask = LVIF_DI_SETITEM;
1021     item.iItem = 0;
1022     item.iSubItem = 0;
1023     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
1024     ok(r != 0, "ret %d\n", r);
1025
1026     /* set text to callback value already having it */
1027     r = SendMessage(hwnd, LVM_DELETEALLITEMS, 0, 0);
1028     expect(TRUE, r);
1029     memset (&item, 0, sizeof (item));
1030     item.mask  = LVIF_TEXT;
1031     item.pszText = LPSTR_TEXTCALLBACK;
1032     item.iItem = 0;
1033     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
1034     ok(r == 0, "ret %d\n", r);
1035     memset (&item, 0, sizeof (item));
1036
1037     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1038
1039     item.pszText = LPSTR_TEXTCALLBACK;
1040     r = SendMessage(hwnd, LVM_SETITEMTEXT, 0 , (LPARAM) &item);
1041     expect(TRUE, r);
1042
1043     ok_sequence(sequences, PARENT_SEQ_INDEX, textcallback_set_again_parent_seq,
1044                 "check callback text comparison rule", TRUE);
1045
1046     DestroyWindow(hwnd);
1047 }
1048
1049 static void test_columns(void)
1050 {
1051     HWND hwnd, hwndheader;
1052     LVCOLUMN column;
1053     DWORD rc;
1054     INT order[2];
1055
1056     hwnd = CreateWindowEx(0, "SysListView32", "foo", LVS_REPORT,
1057                 10, 10, 100, 200, hwndparent, NULL, NULL, NULL);
1058     ok(hwnd != NULL, "failed to create listview window\n");
1059
1060     /* Add a column with no mask */
1061     memset(&column, 0xcc, sizeof(column));
1062     column.mask = 0;
1063     rc = ListView_InsertColumn(hwnd, 0, &column);
1064     ok(rc==0, "Inserting column with no mask failed with %d\n", rc);
1065
1066     /* Check its width */
1067     rc = ListView_GetColumnWidth(hwnd, 0);
1068     ok(rc==10 ||
1069        broken(rc==0), /* win9x */
1070        "Inserting column with no mask failed to set width to 10 with %d\n", rc);
1071
1072     DestroyWindow(hwnd);
1073
1074     /* LVM_GETCOLUMNORDERARRAY */
1075     hwnd = create_listview_control(0);
1076     hwndheader = subclass_header(hwnd);
1077
1078     memset(&column, 0, sizeof(column));
1079     column.mask = LVCF_WIDTH;
1080     column.cx = 100;
1081     rc = ListView_InsertColumn(hwnd, 0, &column);
1082     ok(rc == 0, "Inserting column failed with %d\n", rc);
1083
1084     column.cx = 200;
1085     rc = ListView_InsertColumn(hwnd, 1, &column);
1086     ok(rc == 1, "Inserting column failed with %d\n", rc);
1087
1088     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1089
1090     rc = SendMessage(hwnd, LVM_GETCOLUMNORDERARRAY, 2, (LPARAM)&order);
1091     ok(rc != 0, "Expected LVM_GETCOLUMNORDERARRAY to succeed\n");
1092     ok(order[0] == 0, "Expected order 0, got %d\n", order[0]);
1093     ok(order[1] == 1, "Expected order 1, got %d\n", order[1]);
1094
1095     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_getorderarray_seq, "get order array", FALSE);
1096
1097     DestroyWindow(hwnd);
1098 }
1099 /* test setting imagelist between WM_NCCREATE and WM_CREATE */
1100 static WNDPROC listviewWndProc;
1101 static HIMAGELIST test_create_imagelist;
1102
1103 static LRESULT CALLBACK create_test_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1104 {
1105     LRESULT ret;
1106
1107     if (uMsg == WM_CREATE)
1108     {
1109         LPCREATESTRUCT lpcs = (LPCREATESTRUCT)lParam;
1110         lpcs->style |= LVS_REPORT;
1111     }
1112     ret = CallWindowProc(listviewWndProc, hwnd, uMsg, wParam, lParam);
1113     if (uMsg == WM_CREATE) SendMessage(hwnd, LVM_SETIMAGELIST, 0, (LPARAM)test_create_imagelist);
1114     return ret;
1115 }
1116
1117 static void test_create(void)
1118 {
1119     HWND hList;
1120     HWND hHeader;
1121     LONG_PTR ret;
1122     LONG r;
1123     LVCOLUMNA col;
1124     RECT rect;
1125     WNDCLASSEX cls;
1126     cls.cbSize = sizeof(WNDCLASSEX);
1127     ok(GetClassInfoEx(GetModuleHandle(NULL), "SysListView32", &cls), "GetClassInfoEx failed\n");
1128     listviewWndProc = cls.lpfnWndProc;
1129     cls.lpfnWndProc = create_test_wndproc;
1130     cls.lpszClassName = "MyListView32";
1131     ok(RegisterClassEx(&cls), "RegisterClassEx failed\n");
1132
1133     test_create_imagelist = ImageList_Create(16, 16, 0, 5, 10);
1134     hList = CreateWindow("MyListView32", "Test", WS_VISIBLE, 0, 0, 100, 100, NULL, NULL, GetModuleHandle(NULL), 0);
1135     ok((HIMAGELIST)SendMessage(hList, LVM_GETIMAGELIST, 0, 0) == test_create_imagelist, "Image list not obtained\n");
1136     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1137     ok(IsWindow(hHeader) && IsWindowVisible(hHeader), "Listview not in report mode\n");
1138     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
1139     DestroyWindow(hList);
1140
1141     /* header isn't created on LVS_ICON and LVS_LIST styles */
1142     hList = CreateWindow("SysListView32", "Test", WS_VISIBLE, 0, 0, 100, 100, NULL, NULL,
1143                           GetModuleHandle(NULL), 0);
1144     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1145     ok(!IsWindow(hHeader), "Header shouldn't be created\n");
1146     ok(NULL == GetDlgItem(hList, 0), "NULL dialog item expected\n");
1147     /* insert column */
1148     memset(&col, 0, sizeof(LVCOLUMNA));
1149     col.mask = LVCF_WIDTH;
1150     col.cx = 100;
1151     r = SendMessage(hList, LVM_INSERTCOLUMN, 0, (LPARAM)&col);
1152     ok(r == 0, "Expected 0 column's inserted\n");
1153     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1154     ok(IsWindow(hHeader), "Header should be created\n");
1155     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
1156     DestroyWindow(hList);
1157
1158     hList = CreateWindow("SysListView32", "Test", WS_VISIBLE|LVS_LIST, 0, 0, 100, 100, NULL, NULL,
1159                           GetModuleHandle(NULL), 0);
1160     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1161     ok(!IsWindow(hHeader), "Header shouldn't be created\n");
1162     ok(NULL == GetDlgItem(hList, 0), "NULL dialog item expected\n");
1163     /* insert column */
1164     memset(&col, 0, sizeof(LVCOLUMNA));
1165     col.mask = LVCF_WIDTH;
1166     col.cx = 100;
1167     r = SendMessage(hList, LVM_INSERTCOLUMN, 0, (LPARAM)&col);
1168     ok(r == 0, "Expected 0 column's inserted\n");
1169     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1170     ok(IsWindow(hHeader), "Header should be created\n");
1171     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
1172     DestroyWindow(hList);
1173
1174     /* try to switch LVS_ICON -> LVS_REPORT and back LVS_ICON -> LVS_REPORT */
1175     hList = CreateWindow("SysListView32", "Test", WS_VISIBLE, 0, 0, 100, 100, NULL, NULL,
1176                           GetModuleHandle(NULL), 0);
1177     ret = SetWindowLongPtr(hList, GWL_STYLE, GetWindowLongPtr(hList, GWL_STYLE) | LVS_REPORT);
1178     ok(ret & WS_VISIBLE, "Style wrong, should have WS_VISIBLE\n");
1179     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1180     ok(IsWindow(hHeader), "Header should be created\n");
1181     ret = SetWindowLongPtr(hList, GWL_STYLE, GetWindowLong(hList, GWL_STYLE) & ~LVS_REPORT);
1182     ok((ret & WS_VISIBLE) && (ret & LVS_REPORT), "Style wrong, should have WS_VISIBLE|LVS_REPORT\n");
1183     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1184     ok(IsWindow(hHeader), "Header should be created\n");
1185     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
1186     DestroyWindow(hList);
1187
1188     /* try to switch LVS_LIST -> LVS_REPORT and back LVS_LIST -> LVS_REPORT */
1189     hList = CreateWindow("SysListView32", "Test", WS_VISIBLE|LVS_LIST, 0, 0, 100, 100, NULL, NULL,
1190                           GetModuleHandle(NULL), 0);
1191     ret = SetWindowLongPtr(hList, GWL_STYLE,
1192                           (GetWindowLongPtr(hList, GWL_STYLE) & ~LVS_LIST) | LVS_REPORT);
1193     ok(((ret & WS_VISIBLE) && (ret & LVS_LIST)), "Style wrong, should have WS_VISIBLE|LVS_LIST\n");
1194     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1195     ok(IsWindow(hHeader), "Header should be created\n");
1196     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
1197     ret = SetWindowLongPtr(hList, GWL_STYLE,
1198                           (GetWindowLongPtr(hList, GWL_STYLE) & ~LVS_REPORT) | LVS_LIST);
1199     ok(((ret & WS_VISIBLE) && (ret & LVS_REPORT)), "Style wrong, should have WS_VISIBLE|LVS_REPORT\n");
1200     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1201     ok(IsWindow(hHeader), "Header should be created\n");
1202     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
1203     DestroyWindow(hList);
1204
1205     /* LVS_REPORT without WS_VISIBLE */
1206     hList = CreateWindow("SysListView32", "Test", LVS_REPORT, 0, 0, 100, 100, NULL, NULL,
1207                           GetModuleHandle(NULL), 0);
1208     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1209     ok(!IsWindow(hHeader), "Header shouldn't be created\n");
1210     ok(NULL == GetDlgItem(hList, 0), "NULL dialog item expected\n");
1211     /* insert column */
1212     memset(&col, 0, sizeof(LVCOLUMNA));
1213     col.mask = LVCF_WIDTH;
1214     col.cx = 100;
1215     r = SendMessage(hList, LVM_INSERTCOLUMN, 0, (LPARAM)&col);
1216     ok(r == 0, "Expected 0 column's inserted\n");
1217     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1218     ok(IsWindow(hHeader), "Header should be created\n");
1219     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
1220     DestroyWindow(hList);
1221
1222     /* LVS_REPORT without WS_VISIBLE, try to show it */
1223     hList = CreateWindow("SysListView32", "Test", LVS_REPORT, 0, 0, 100, 100, NULL, NULL,
1224                           GetModuleHandle(NULL), 0);
1225     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1226     ok(!IsWindow(hHeader), "Header shouldn't be created\n");
1227     ok(NULL == GetDlgItem(hList, 0), "NULL dialog item expected\n");
1228     ShowWindow(hList, SW_SHOW);
1229     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1230     ok(IsWindow(hHeader), "Header should be created\n");
1231     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
1232     DestroyWindow(hList);
1233
1234     /* LVS_REPORT with LVS_NOCOLUMNHEADER */
1235     hList = CreateWindow("SysListView32", "Test", LVS_REPORT|LVS_NOCOLUMNHEADER|WS_VISIBLE,
1236                           0, 0, 100, 100, NULL, NULL, GetModuleHandle(NULL), 0);
1237     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1238     ok(IsWindow(hHeader), "Header should be created\n");
1239     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
1240     /* HDS_DRAGDROP set by default */
1241     ok(GetWindowLongPtr(hHeader, GWL_STYLE) & HDS_DRAGDROP, "Expected header to have HDS_DRAGDROP\n");
1242     DestroyWindow(hList);
1243
1244     /* setting LVS_EX_HEADERDRAGDROP creates header */
1245     hList = CreateWindow("SysListView32", "Test", LVS_REPORT, 0, 0, 100, 100, NULL, NULL,
1246                           GetModuleHandle(NULL), 0);
1247     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1248     ok(!IsWindow(hHeader), "Header shouldn't be created\n");
1249     ok(NULL == GetDlgItem(hList, 0), "NULL dialog item expected\n");
1250     SendMessage(hList, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_HEADERDRAGDROP);
1251     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1252     ok(IsWindow(hHeader), "Header should be created\n");
1253     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
1254     DestroyWindow(hList);
1255
1256     /* not report style accepts LVS_EX_HEADERDRAGDROP too */
1257     hList = create_custom_listview_control(0);
1258     SendMessage(hList, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_HEADERDRAGDROP);
1259     r = SendMessage(hList, LVM_GETEXTENDEDLISTVIEWSTYLE, 0, 0);
1260     ok(r & LVS_EX_HEADERDRAGDROP, "Expected LVS_EX_HEADERDRAGDROP to be set\n");
1261     DestroyWindow(hList);
1262
1263     /* requesting header info with LVM_GETSUBITEMRECT doesn't create it */
1264     hList = CreateWindow("SysListView32", "Test", LVS_REPORT, 0, 0, 100, 100, NULL, NULL,
1265                           GetModuleHandle(NULL), 0);
1266     ok(!IsWindow(hHeader), "Header shouldn't be created\n");
1267     ok(NULL == GetDlgItem(hList, 0), "NULL dialog item expected\n");
1268
1269     rect.left = LVIR_BOUNDS;
1270     rect.top  = 1;
1271     rect.right = rect.bottom = -10;
1272     r = SendMessage(hList, LVM_GETSUBITEMRECT, -1, (LPARAM)&rect);
1273     ok(r != 0, "Expected not-null LRESULT\n");
1274
1275     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1276     ok(!IsWindow(hHeader), "Header shouldn't be created\n");
1277     ok(NULL == GetDlgItem(hList, 0), "NULL dialog item expected\n");
1278
1279     DestroyWindow(hList);
1280 }
1281
1282 static void test_redraw(void)
1283 {
1284     HWND hwnd, hwndheader;
1285     HDC hdc;
1286     BOOL res;
1287     DWORD r;
1288
1289     hwnd = create_listview_control(0);
1290     hwndheader = subclass_header(hwnd);
1291
1292     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1293
1294     trace("invalidate & update\n");
1295     InvalidateRect(hwnd, NULL, TRUE);
1296     UpdateWindow(hwnd);
1297     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, redraw_listview_seq, "redraw listview", FALSE);
1298
1299     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1300
1301     /* forward WM_ERASEBKGND to parent on CLR_NONE background color */
1302     /* 1. Without backbuffer */
1303     res = ListView_SetBkColor(hwnd, CLR_NONE);
1304     expect(TRUE, res);
1305
1306     hdc = GetWindowDC(hwndparent);
1307
1308     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1309     r = SendMessageA(hwnd, WM_ERASEBKGND, (WPARAM)hdc, 0);
1310     ok(r != 0, "Expected not zero result\n");
1311     ok_sequence(sequences, PARENT_FULL_SEQ_INDEX, forward_erasebkgnd_parent_seq,
1312                 "forward WM_ERASEBKGND on CLR_NONE", FALSE);
1313
1314     res = ListView_SetBkColor(hwnd, CLR_DEFAULT);
1315     expect(TRUE, res);
1316
1317     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1318     r = SendMessageA(hwnd, WM_ERASEBKGND, (WPARAM)hdc, 0);
1319     ok(r != 0, "Expected not zero result\n");
1320     ok_sequence(sequences, PARENT_FULL_SEQ_INDEX, empty_seq,
1321                 "don't forward WM_ERASEBKGND on non-CLR_NONE", FALSE);
1322
1323     /* 2. With backbuffer */
1324     SendMessageA(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_DOUBLEBUFFER,
1325                                                      LVS_EX_DOUBLEBUFFER);
1326     res = ListView_SetBkColor(hwnd, CLR_NONE);
1327     expect(TRUE, res);
1328
1329     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1330     r = SendMessageA(hwnd, WM_ERASEBKGND, (WPARAM)hdc, 0);
1331     ok(r != 0, "Expected not zero result\n");
1332     ok_sequence(sequences, PARENT_FULL_SEQ_INDEX, forward_erasebkgnd_parent_seq,
1333                 "forward WM_ERASEBKGND on CLR_NONE", FALSE);
1334
1335     res = ListView_SetBkColor(hwnd, CLR_DEFAULT);
1336     expect(TRUE, res);
1337
1338     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1339     r = SendMessageA(hwnd, WM_ERASEBKGND, (WPARAM)hdc, 0);
1340     todo_wine ok(r != 0, "Expected not zero result\n");
1341     ok_sequence(sequences, PARENT_FULL_SEQ_INDEX, empty_seq,
1342                 "don't forward WM_ERASEBKGND on non-CLR_NONE", FALSE);
1343
1344     ReleaseDC(hwndparent, hdc);
1345
1346     DestroyWindow(hwnd);
1347 }
1348
1349 static LRESULT WINAPI cd_wndproc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
1350 {
1351     COLORREF clr, c0ffee = RGB(0xc0, 0xff, 0xee);
1352
1353     if(msg == WM_NOTIFY) {
1354         NMHDR *nmhdr = (PVOID)lp;
1355         if(nmhdr->code == NM_CUSTOMDRAW) {
1356             NMLVCUSTOMDRAW *nmlvcd = (PVOID)nmhdr;
1357             trace("NMCUSTOMDRAW (0x%.8x)\n", nmlvcd->nmcd.dwDrawStage);
1358             switch(nmlvcd->nmcd.dwDrawStage) {
1359             case CDDS_PREPAINT:
1360                 SetBkColor(nmlvcd->nmcd.hdc, c0ffee);
1361                 return CDRF_NOTIFYITEMDRAW;
1362             case CDDS_ITEMPREPAINT:
1363                 nmlvcd->clrTextBk = CLR_DEFAULT;
1364                 return CDRF_NOTIFYSUBITEMDRAW;
1365             case CDDS_ITEMPREPAINT | CDDS_SUBITEM:
1366                 clr = GetBkColor(nmlvcd->nmcd.hdc);
1367                 todo_wine ok(clr == c0ffee, "clr=%.8x\n", clr);
1368                 return CDRF_NOTIFYPOSTPAINT;
1369             case CDDS_ITEMPOSTPAINT | CDDS_SUBITEM:
1370                 clr = GetBkColor(nmlvcd->nmcd.hdc);
1371                 todo_wine ok(clr == c0ffee, "clr=%.8x\n", clr);
1372                 return CDRF_DODEFAULT;
1373             }
1374             return CDRF_DODEFAULT;
1375         }
1376     }
1377
1378     return DefWindowProcA(hwnd, msg, wp, lp);
1379 }
1380
1381 static void test_customdraw(void)
1382 {
1383     HWND hwnd;
1384     WNDPROC oldwndproc;
1385
1386     hwnd = create_listview_control(0);
1387
1388     insert_column(hwnd, 0);
1389     insert_column(hwnd, 1);
1390     insert_item(hwnd, 0);
1391
1392     oldwndproc = (WNDPROC)SetWindowLongPtr(hwndparent, GWLP_WNDPROC,
1393                                            (LONG_PTR)cd_wndproc);
1394
1395     InvalidateRect(hwnd, NULL, TRUE);
1396     UpdateWindow(hwnd);
1397
1398     SetWindowLongPtr(hwndparent, GWLP_WNDPROC, (LONG_PTR)oldwndproc);
1399
1400     DestroyWindow(hwnd);
1401 }
1402
1403 static void test_icon_spacing(void)
1404 {
1405     /* LVM_SETICONSPACING */
1406     /* note: LVM_SETICONSPACING returns the previous icon spacing if successful */
1407
1408     HWND hwnd;
1409     WORD w, h;
1410     DWORD r;
1411
1412     hwnd = create_custom_listview_control(LVS_ICON);
1413     ok(hwnd != NULL, "failed to create a listview window\n");
1414
1415     r = SendMessage(hwnd, WM_NOTIFYFORMAT, (WPARAM)hwndparent, (LPARAM)NF_REQUERY);
1416     expect(NFR_ANSI, r);
1417
1418     /* reset the icon spacing to defaults */
1419     SendMessage(hwnd, LVM_SETICONSPACING, 0, MAKELPARAM(-1, -1));
1420
1421     /* now we can request what the defaults are */
1422     r = SendMessage(hwnd, LVM_SETICONSPACING, 0, MAKELPARAM(-1, -1));
1423     w = LOWORD(r);
1424     h = HIWORD(r);
1425
1426     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1427
1428     trace("test icon spacing\n");
1429
1430     r = SendMessage(hwnd, LVM_SETICONSPACING, 0, MAKELPARAM(20, 30));
1431     ok(r == MAKELONG(w, h) ||
1432        broken(r == MAKELONG(w, w)), /* win98 */
1433        "Expected %d, got %d\n", MAKELONG(w, h), r);
1434
1435     r = SendMessage(hwnd, LVM_SETICONSPACING, 0, MAKELPARAM(25, 35));
1436     expect(MAKELONG(20,30), r);
1437
1438     r = SendMessage(hwnd, LVM_SETICONSPACING, 0, MAKELPARAM(-1,-1));
1439     expect(MAKELONG(25,35), r);
1440
1441     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_icon_spacing_seq, "test icon spacing seq", FALSE);
1442
1443     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1444     DestroyWindow(hwnd);
1445 }
1446
1447 static void test_color(void)
1448 {
1449     /* SETBKCOLOR/GETBKCOLOR, SETTEXTCOLOR/GETTEXTCOLOR, SETTEXTBKCOLOR/GETTEXTBKCOLOR */
1450
1451     HWND hwnd;
1452     DWORD r;
1453     int i;
1454
1455     COLORREF color;
1456     COLORREF colors[4] = {RGB(0,0,0), RGB(100,50,200), CLR_NONE, RGB(255,255,255)};
1457
1458     hwnd = create_listview_control(0);
1459     ok(hwnd != NULL, "failed to create a listview window\n");
1460
1461     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1462
1463     trace("test color seq\n");
1464     for (i = 0; i < 4; i++)
1465     {
1466         color = colors[i];
1467
1468         r = SendMessage(hwnd, LVM_SETBKCOLOR, 0, color);
1469         expect(TRUE, r);
1470         r = SendMessage(hwnd, LVM_GETBKCOLOR, 0, color);
1471         expect(color, r);
1472
1473         r = SendMessage(hwnd, LVM_SETTEXTCOLOR, 0, color);
1474         expect (TRUE, r);
1475         r = SendMessage(hwnd, LVM_GETTEXTCOLOR, 0, color);
1476         expect(color, r);
1477
1478         r = SendMessage(hwnd, LVM_SETTEXTBKCOLOR, 0, color);
1479         expect(TRUE, r);
1480         r = SendMessage(hwnd, LVM_GETTEXTBKCOLOR, 0, color);
1481         expect(color, r);
1482     }
1483
1484     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_color_seq, "test color seq", FALSE);
1485
1486     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1487     DestroyWindow(hwnd);
1488 }
1489
1490 static void test_item_count(void)
1491 {
1492     /* LVM_INSERTITEM, LVM_DELETEITEM, LVM_DELETEALLITEMS, LVM_GETITEMCOUNT */
1493
1494     HWND hwnd;
1495     DWORD r;
1496
1497     LVITEM item0;
1498     LVITEM item1;
1499     LVITEM item2;
1500     static CHAR item0text[] = "item0";
1501     static CHAR item1text[] = "item1";
1502     static CHAR item2text[] = "item2";
1503
1504     hwnd = create_listview_control(0);
1505     ok(hwnd != NULL, "failed to create a listview window\n");
1506
1507     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1508
1509     trace("test item count\n");
1510
1511     r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
1512     expect(0, r);
1513
1514     /* [item0] */
1515     item0.mask = LVIF_TEXT;
1516     item0.iItem = 0;
1517     item0.iSubItem = 0;
1518     item0.pszText = item0text;
1519     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item0);
1520     expect(0, r);
1521
1522     /* [item0, item1] */
1523     item1.mask = LVIF_TEXT;
1524     item1.iItem = 1;
1525     item1.iSubItem = 0;
1526     item1.pszText = item1text;
1527     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item1);
1528     expect(1, r);
1529
1530     /* [item0, item1, item2] */
1531     item2.mask = LVIF_TEXT;
1532     item2.iItem = 2;
1533     item2.iSubItem = 0;
1534     item2.pszText = item2text;
1535     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item2);
1536     expect(2, r);
1537
1538     r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
1539     expect(3, r);
1540
1541     /* [item0, item1] */
1542     r = SendMessage(hwnd, LVM_DELETEITEM, 2, 0);
1543     expect(TRUE, r);
1544
1545     r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
1546     expect(2, r);
1547
1548     /* [] */
1549     r = SendMessage(hwnd, LVM_DELETEALLITEMS, 0, 0);
1550     expect(TRUE, r);
1551
1552     r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
1553     expect(0, r);
1554
1555     /* [item0] */
1556     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item1);
1557     expect(0, r);
1558
1559     /* [item0, item1] */
1560     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item1);
1561     expect(1, r);
1562
1563     r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
1564     expect(2, r);
1565
1566     /* [item0, item1, item2] */
1567     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item2);
1568     expect(2, r);
1569
1570     r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
1571     expect(3, r);
1572
1573     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_item_count_seq, "test item count seq", FALSE);
1574
1575     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1576     DestroyWindow(hwnd);
1577 }
1578
1579 static void test_item_position(void)
1580 {
1581     /* LVM_SETITEMPOSITION/LVM_GETITEMPOSITION */
1582
1583     HWND hwnd;
1584     DWORD r;
1585     POINT position;
1586
1587     LVITEM item0;
1588     LVITEM item1;
1589     LVITEM item2;
1590     static CHAR item0text[] = "item0";
1591     static CHAR item1text[] = "item1";
1592     static CHAR item2text[] = "item2";
1593
1594     hwnd = create_custom_listview_control(LVS_ICON);
1595     ok(hwnd != NULL, "failed to create a listview window\n");
1596
1597     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1598
1599     trace("test item position\n");
1600
1601     /* [item0] */
1602     item0.mask = LVIF_TEXT;
1603     item0.iItem = 0;
1604     item0.iSubItem = 0;
1605     item0.pszText = item0text;
1606     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item0);
1607     expect(0, r);
1608
1609     /* [item0, item1] */
1610     item1.mask = LVIF_TEXT;
1611     item1.iItem = 1;
1612     item1.iSubItem = 0;
1613     item1.pszText = item1text;
1614     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item1);
1615     expect(1, r);
1616
1617     /* [item0, item1, item2] */
1618     item2.mask = LVIF_TEXT;
1619     item2.iItem = 2;
1620     item2.iSubItem = 0;
1621     item2.pszText = item2text;
1622     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item2);
1623     expect(2, r);
1624
1625     r = SendMessage(hwnd, LVM_SETITEMPOSITION, 1, MAKELPARAM(10,5));
1626     expect(TRUE, r);
1627     r = SendMessage(hwnd, LVM_GETITEMPOSITION, 1, (LPARAM) &position);
1628     expect(TRUE, r);
1629     expect2(10, 5, position.x, position.y);
1630
1631     r = SendMessage(hwnd, LVM_SETITEMPOSITION, 2, MAKELPARAM(0,0));
1632     expect(TRUE, r);
1633     r = SendMessage(hwnd, LVM_GETITEMPOSITION, 2, (LPARAM) &position);
1634     expect(TRUE, r);
1635     expect2(0, 0, position.x, position.y);
1636
1637     r = SendMessage(hwnd, LVM_SETITEMPOSITION, 0, MAKELPARAM(20,20));
1638     expect(TRUE, r);
1639     r = SendMessage(hwnd, LVM_GETITEMPOSITION, 0, (LPARAM) &position);
1640     expect(TRUE, r);
1641     expect2(20, 20, position.x, position.y);
1642
1643     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_itempos_seq, "test item position seq", TRUE);
1644
1645     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1646     DestroyWindow(hwnd);
1647 }
1648
1649 static void test_getorigin(void)
1650 {
1651     /* LVM_GETORIGIN */
1652
1653     HWND hwnd;
1654     DWORD r;
1655     POINT position;
1656
1657     position.x = position.y = 0;
1658
1659     hwnd = create_custom_listview_control(LVS_ICON);
1660     ok(hwnd != NULL, "failed to create a listview window\n");
1661     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1662     trace("test get origin results\n");
1663     r = SendMessage(hwnd, LVM_GETORIGIN, 0, (LPARAM)&position);
1664     expect(TRUE, r);
1665     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1666     DestroyWindow(hwnd);
1667
1668     hwnd = create_custom_listview_control(LVS_SMALLICON);
1669     ok(hwnd != NULL, "failed to create a listview window\n");
1670     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1671     trace("test get origin results\n");
1672     r = SendMessage(hwnd, LVM_GETORIGIN, 0, (LPARAM)&position);
1673     expect(TRUE, r);
1674     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1675     DestroyWindow(hwnd);
1676
1677     hwnd = create_custom_listview_control(LVS_LIST);
1678     ok(hwnd != NULL, "failed to create a listview window\n");
1679     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1680     trace("test get origin results\n");
1681     r = SendMessage(hwnd, LVM_GETORIGIN, 0, (LPARAM)&position);
1682     expect(FALSE, r);
1683     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1684     DestroyWindow(hwnd);
1685
1686     hwnd = create_custom_listview_control(LVS_REPORT);
1687     ok(hwnd != NULL, "failed to create a listview window\n");
1688     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1689     trace("test get origin results\n");
1690     r = SendMessage(hwnd, LVM_GETORIGIN, 0, (LPARAM)&position);
1691     expect(FALSE, r);
1692     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1693     DestroyWindow(hwnd);
1694
1695 }
1696
1697 static void test_multiselect(void)
1698 {
1699     typedef struct t_select_task
1700     {
1701         const char *descr;
1702         int initPos;
1703         int loopVK;
1704         int count;
1705         int result;
1706     } select_task;
1707
1708     HWND hwnd;
1709     DWORD r;
1710     int i,j,item_count,selected_count;
1711     static const int items=5;
1712     BYTE kstate[256];
1713     select_task task;
1714     LONG_PTR style;
1715     LVITEMA item;
1716
1717     static struct t_select_task task_list[] = {
1718         { "using VK_DOWN", 0, VK_DOWN, -1, -1 },
1719         { "using VK_UP", -1, VK_UP, -1, -1 },
1720         { "using VK_END", 0, VK_END, 1, -1 },
1721         { "using VK_HOME", -1, VK_HOME, 1, -1 }
1722     };
1723
1724
1725     hwnd = create_listview_control(0);
1726
1727     for (i=0;i<items;i++) {
1728             insert_item(hwnd, 0);
1729     }
1730
1731     item_count = (int)SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
1732
1733     expect(items,item_count);
1734
1735     for (i=0;i<4;i++) {
1736         task = task_list[i];
1737
1738         /* deselect all items */
1739         ListView_SetItemState(hwnd, -1, 0, LVIS_SELECTED);
1740         SendMessage(hwnd, LVM_SETSELECTIONMARK, 0, -1);
1741
1742         /* set initial position */
1743         SendMessage(hwnd, LVM_SETSELECTIONMARK, 0, (task.initPos == -1 ? item_count -1 : task.initPos));
1744         ListView_SetItemState(hwnd,(task.initPos == -1 ? item_count -1 : task.initPos),LVIS_SELECTED ,LVIS_SELECTED);
1745
1746         selected_count = (int)SendMessage(hwnd, LVM_GETSELECTEDCOUNT, 0, 0);
1747
1748         ok(selected_count == 1, "There should be only one selected item at the beginning (is %d)\n",selected_count);
1749
1750         /* Set SHIFT key pressed */
1751         GetKeyboardState(kstate);
1752         kstate[VK_SHIFT]=0x80;
1753         SetKeyboardState(kstate);
1754
1755         for (j=1;j<=(task.count == -1 ? item_count : task.count);j++) {
1756             r = SendMessage(hwnd, WM_KEYDOWN, task.loopVK, 0);
1757             expect(0,r);
1758             r = SendMessage(hwnd, WM_KEYUP, task.loopVK, 0);
1759             expect(0,r);
1760         }
1761
1762         selected_count = (int)SendMessage(hwnd, LVM_GETSELECTEDCOUNT, 0, 0);
1763
1764         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);
1765
1766         /* Set SHIFT key released */
1767         GetKeyboardState(kstate);
1768         kstate[VK_SHIFT]=0x00;
1769         SetKeyboardState(kstate);
1770     }
1771     DestroyWindow(hwnd);
1772
1773     /* make multiple selection, then switch to LVS_SINGLESEL */
1774     hwnd = create_listview_control(0);
1775     for (i=0;i<items;i++) {
1776             insert_item(hwnd, 0);
1777     }
1778     item_count = (int)SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
1779     expect(items,item_count);
1780     /* deselect all items */
1781     ListView_SetItemState(hwnd, -1, 0, LVIS_SELECTED);
1782     SendMessage(hwnd, LVM_SETSELECTIONMARK, 0, -1);
1783     for (i=0;i<3;i++) {
1784         ListView_SetItemState(hwnd, i, LVIS_SELECTED, LVIS_SELECTED);
1785     }
1786
1787     r = SendMessage(hwnd, LVM_GETSELECTEDCOUNT, 0, 0);
1788     expect(3, r);
1789     r = SendMessage(hwnd, LVM_GETSELECTIONMARK, 0, 0);
1790 todo_wine
1791     expect(-1, r);
1792
1793     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
1794     ok(!(style & LVS_SINGLESEL), "LVS_SINGLESEL isn't expected\n");
1795     SetWindowLongPtrA(hwnd, GWL_STYLE, style | LVS_SINGLESEL);
1796     /* check that style is accepted */
1797     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
1798     ok(style & LVS_SINGLESEL, "LVS_SINGLESEL expected\n");
1799
1800     for (i=0;i<3;i++) {
1801         r = ListView_GetItemState(hwnd, i, LVIS_SELECTED);
1802         ok(r & LVIS_SELECTED, "Expected item %d to be selected\n", i);
1803     }
1804     r = SendMessage(hwnd, LVM_GETSELECTEDCOUNT, 0, 0);
1805     expect(3, r);
1806     SendMessage(hwnd, LVM_GETSELECTIONMARK, 0, 0);
1807     expect(3, r);
1808
1809     /* select one more */
1810     ListView_SetItemState(hwnd, 3, LVIS_SELECTED, LVIS_SELECTED);
1811
1812     for (i=0;i<3;i++) {
1813         r = ListView_GetItemState(hwnd, i, LVIS_SELECTED);
1814         ok(!(r & LVIS_SELECTED), "Expected item %d to be unselected\n", i);
1815     }
1816     r = ListView_GetItemState(hwnd, 3, LVIS_SELECTED);
1817     ok(r & LVIS_SELECTED, "Expected item %d to be selected\n", i);
1818
1819     r = SendMessage(hwnd, LVM_GETSELECTEDCOUNT, 0, 0);
1820     expect(1, r);
1821     r = SendMessage(hwnd, LVM_GETSELECTIONMARK, 0, 0);
1822 todo_wine
1823     expect(-1, r);
1824
1825     /* try to select all on LVS_SINGLESEL */
1826     memset(&item, 0, sizeof(item));
1827     item.stateMask = LVIS_SELECTED;
1828     r = SendMessage(hwnd, LVM_SETITEMSTATE, -1, (LPARAM)&item);
1829     expect(TRUE, r);
1830     SendMessage(hwnd, LVM_SETSELECTIONMARK, 0, -1);
1831
1832     item.stateMask = LVIS_SELECTED;
1833     item.state     = LVIS_SELECTED;
1834     r = SendMessage(hwnd, LVM_SETITEMSTATE, -1, (LPARAM)&item);
1835     expect(FALSE, r);
1836
1837     r = ListView_GetSelectedCount(hwnd);
1838     expect(0, r);
1839     r = ListView_GetSelectionMark(hwnd);
1840     expect(-1, r);
1841
1842     /* try to deselect all on LVS_SINGLESEL */
1843     item.stateMask = LVIS_SELECTED;
1844     item.state     = LVIS_SELECTED;
1845     r = SendMessage(hwnd, LVM_SETITEMSTATE, 0, (LPARAM)&item);
1846     expect(TRUE, r);
1847
1848     item.stateMask = LVIS_SELECTED;
1849     item.state     = 0;
1850     r = SendMessage(hwnd, LVM_SETITEMSTATE, -1, (LPARAM)&item);
1851     expect(TRUE, r);
1852     r = ListView_GetSelectedCount(hwnd);
1853     expect(0, r);
1854
1855     DestroyWindow(hwnd);
1856 }
1857
1858 static void test_subitem_rect(void)
1859 {
1860     HWND hwnd;
1861     DWORD r;
1862     LVCOLUMN col;
1863     RECT rect;
1864
1865     /* test LVM_GETSUBITEMRECT for header */
1866     hwnd = create_listview_control(0);
1867     ok(hwnd != NULL, "failed to create a listview window\n");
1868     /* add some columns */
1869     memset(&col, 0, sizeof(LVCOLUMN));
1870     col.mask = LVCF_WIDTH;
1871     col.cx = 100;
1872     r = -1;
1873     r = SendMessage(hwnd, LVM_INSERTCOLUMN, 0, (LPARAM)&col);
1874     expect(0, r);
1875     col.cx = 150;
1876     r = -1;
1877     r = SendMessage(hwnd, LVM_INSERTCOLUMN, 1, (LPARAM)&col);
1878     expect(1, r);
1879     col.cx = 200;
1880     r = -1;
1881     r = SendMessage(hwnd, LVM_INSERTCOLUMN, 2, (LPARAM)&col);
1882     expect(2, r);
1883     /* item = -1 means header, subitem index is 1 based */
1884     rect.left = LVIR_BOUNDS;
1885     rect.top  = 0;
1886     rect.right = rect.bottom = 0;
1887     r = SendMessage(hwnd, LVM_GETSUBITEMRECT, -1, (LPARAM)&rect);
1888     expect(0, r);
1889
1890     rect.left = LVIR_BOUNDS;
1891     rect.top  = 1;
1892     rect.right = rect.bottom = 0;
1893     r = SendMessage(hwnd, LVM_GETSUBITEMRECT, -1, (LPARAM)&rect);
1894
1895     ok(r != 0, "Expected not-null LRESULT\n");
1896     expect(100, rect.left);
1897     expect(250, rect.right);
1898 todo_wine
1899     expect(3, rect.top);
1900
1901     rect.left = LVIR_BOUNDS;
1902     rect.top  = 2;
1903     rect.right = rect.bottom = 0;
1904     r = SendMessage(hwnd, LVM_GETSUBITEMRECT, -1, (LPARAM)&rect);
1905
1906     ok(r != 0, "Expected not-null LRESULT\n");
1907     expect(250, rect.left);
1908     expect(450, rect.right);
1909 todo_wine
1910     expect(3, rect.top);
1911
1912     /* item LVS_REPORT padding isn't applied to subitems */
1913     insert_item(hwnd, 0);
1914
1915     rect.left = LVIR_BOUNDS;
1916     rect.top  = 1;
1917     rect.right = rect.bottom = 0;
1918     r = SendMessage(hwnd, LVM_GETSUBITEMRECT, 0, (LPARAM)&rect);
1919     ok(r != 0, "Expected not-null LRESULT\n");
1920     expect(100, rect.left);
1921     expect(250, rect.right);
1922
1923     rect.left = LVIR_ICON;
1924     rect.top  = 1;
1925     rect.right = rect.bottom = 0;
1926     r = SendMessage(hwnd, LVM_GETSUBITEMRECT, 0, (LPARAM)&rect);
1927     ok(r != 0, "Expected not-null LRESULT\n");
1928     /* no icon attached - zero width rectangle, with no left padding */
1929     expect(100, rect.left);
1930     expect(100, rect.right);
1931
1932     rect.left = LVIR_LABEL;
1933     rect.top  = 1;
1934     rect.right = rect.bottom = 0;
1935     r = SendMessage(hwnd, LVM_GETSUBITEMRECT, 0, (LPARAM)&rect);
1936     ok(r != 0, "Expected not-null LRESULT\n");
1937     /* same as full LVIR_BOUNDS */
1938     expect(100, rect.left);
1939     expect(250, rect.right);
1940
1941     DestroyWindow(hwnd);
1942
1943     /* try it for non LVS_REPORT style */
1944     hwnd = CreateWindow("SysListView32", "Test", LVS_ICON, 0, 0, 100, 100, NULL, NULL,
1945                          GetModuleHandle(NULL), 0);
1946     rect.left = LVIR_BOUNDS;
1947     rect.top  = 1;
1948     rect.right = rect.bottom = -10;
1949     r = SendMessage(hwnd, LVM_GETSUBITEMRECT, -1, (LPARAM)&rect);
1950     ok(r == 0, "Expected not-null LRESULT\n");
1951     /* rect is unchanged */
1952     expect(0, rect.left);
1953     expect(-10, rect.right);
1954     expect(1, rect.top);
1955     expect(-10, rect.bottom);
1956     DestroyWindow(hwnd);
1957 }
1958
1959 /* comparison callback for test_sorting */
1960 static INT WINAPI test_CallBackCompare(LPARAM first, LPARAM second, LPARAM lParam)
1961 {
1962     if (first == second) return 0;
1963     return (first > second ? 1 : -1);
1964 }
1965
1966 static void test_sorting(void)
1967 {
1968     HWND hwnd;
1969     LVITEMA item = {0};
1970     DWORD r;
1971     LONG_PTR style;
1972     static CHAR names[][5] = {"A", "B", "C", "D", "0"};
1973     CHAR buff[10];
1974
1975     hwnd = create_listview_control(0);
1976     ok(hwnd != NULL, "failed to create a listview window\n");
1977
1978     /* insert some items */
1979     item.mask = LVIF_PARAM | LVIF_STATE;
1980     item.state = LVIS_SELECTED;
1981     item.iItem = 0;
1982     item.iSubItem = 0;
1983     item.lParam = 3;
1984     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
1985     expect(0, r);
1986
1987     item.mask = LVIF_PARAM;
1988     item.iItem = 1;
1989     item.iSubItem = 0;
1990     item.lParam = 2;
1991     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
1992     expect(1, r);
1993
1994     item.mask = LVIF_STATE | LVIF_PARAM;
1995     item.state = LVIS_SELECTED;
1996     item.iItem = 2;
1997     item.iSubItem = 0;
1998     item.lParam = 4;
1999     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
2000     expect(2, r);
2001
2002     r = SendMessage(hwnd, LVM_GETSELECTIONMARK, 0, 0);
2003     expect(-1, r);
2004
2005     r = SendMessage(hwnd, LVM_GETSELECTEDCOUNT, 0, 0);
2006     expect(2, r);
2007
2008     r = SendMessage(hwnd, LVM_SORTITEMS, 0, (LPARAM)test_CallBackCompare);
2009     expect(TRUE, r);
2010
2011     r = SendMessage(hwnd, LVM_GETSELECTEDCOUNT, 0, 0);
2012     expect(2, r);
2013     r = SendMessage(hwnd, LVM_GETSELECTIONMARK, 0, 0);
2014     expect(-1, r);
2015     r = SendMessage(hwnd, LVM_GETITEMSTATE, 0, LVIS_SELECTED);
2016     expect(0, r);
2017     r = SendMessage(hwnd, LVM_GETITEMSTATE, 1, LVIS_SELECTED);
2018     expect(LVIS_SELECTED, r);
2019     r = SendMessage(hwnd, LVM_GETITEMSTATE, 2, LVIS_SELECTED);
2020     expect(LVIS_SELECTED, r);
2021
2022     DestroyWindow(hwnd);
2023
2024     /* switch to LVS_SORTASCENDING when some items added */
2025     hwnd = create_listview_control(0);
2026     ok(hwnd != NULL, "failed to create a listview window\n");
2027
2028     item.mask = LVIF_TEXT;
2029     item.iItem = 0;
2030     item.iSubItem = 0;
2031     item.pszText = names[1];
2032     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
2033     expect(0, r);
2034
2035     item.mask = LVIF_TEXT;
2036     item.iItem = 1;
2037     item.iSubItem = 0;
2038     item.pszText = names[2];
2039     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
2040     expect(1, r);
2041
2042     item.mask = LVIF_TEXT;
2043     item.iItem = 2;
2044     item.iSubItem = 0;
2045     item.pszText = names[0];
2046     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
2047     expect(2, r);
2048
2049     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2050     SetWindowLongPtrA(hwnd, GWL_STYLE, style | LVS_SORTASCENDING);
2051     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2052     ok(style & LVS_SORTASCENDING, "Expected LVS_SORTASCENDING to be set\n");
2053
2054     /* no sorting performed when switched to LVS_SORTASCENDING */
2055     item.mask = LVIF_TEXT;
2056     item.iItem = 0;
2057     item.pszText = buff;
2058     item.cchTextMax = sizeof(buff);
2059     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
2060     expect(TRUE, r);
2061     ok(lstrcmp(buff, names[1]) == 0, "Expected '%s', got '%s'\n", names[1], buff);
2062
2063     item.iItem = 1;
2064     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
2065     expect(TRUE, r);
2066     ok(lstrcmp(buff, names[2]) == 0, "Expected '%s', got '%s'\n", names[2], buff);
2067
2068     item.iItem = 2;
2069     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
2070     expect(TRUE, r);
2071     ok(lstrcmp(buff, names[0]) == 0, "Expected '%s', got '%s'\n", names[0], buff);
2072
2073     /* adding new item doesn't resort list */
2074     item.mask = LVIF_TEXT;
2075     item.iItem = 3;
2076     item.iSubItem = 0;
2077     item.pszText = names[3];
2078     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
2079     expect(3, r);
2080
2081     item.mask = LVIF_TEXT;
2082     item.iItem = 0;
2083     item.pszText = buff;
2084     item.cchTextMax = sizeof(buff);
2085     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
2086     expect(TRUE, r);
2087     ok(lstrcmp(buff, names[1]) == 0, "Expected '%s', got '%s'\n", names[1], buff);
2088
2089     item.iItem = 1;
2090     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
2091     expect(TRUE, r);
2092     ok(lstrcmp(buff, names[2]) == 0, "Expected '%s', got '%s'\n", names[2], buff);
2093
2094     item.iItem = 2;
2095     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
2096     expect(TRUE, r);
2097     ok(lstrcmp(buff, names[0]) == 0, "Expected '%s', got '%s'\n", names[0], buff);
2098
2099     item.iItem = 3;
2100     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
2101     expect(TRUE, r);
2102     ok(lstrcmp(buff, names[3]) == 0, "Expected '%s', got '%s'\n", names[3], buff);
2103
2104     /* corner case - item should be placed at first position */
2105     item.mask = LVIF_TEXT;
2106     item.iItem = 4;
2107     item.iSubItem = 0;
2108     item.pszText = names[4];
2109     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
2110     expect(0, r);
2111
2112     item.iItem = 0;
2113     item.pszText = buff;
2114     item.cchTextMax = sizeof(buff);
2115     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
2116     expect(TRUE, r);
2117     ok(lstrcmp(buff, names[4]) == 0, "Expected '%s', got '%s'\n", names[4], buff);
2118
2119     item.iItem = 1;
2120     item.pszText = buff;
2121     item.cchTextMax = sizeof(buff);
2122     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
2123     expect(TRUE, r);
2124     ok(lstrcmp(buff, names[1]) == 0, "Expected '%s', got '%s'\n", names[1], buff);
2125
2126     item.iItem = 2;
2127     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
2128     expect(TRUE, r);
2129     ok(lstrcmp(buff, names[2]) == 0, "Expected '%s', got '%s'\n", names[2], buff);
2130
2131     item.iItem = 3;
2132     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
2133     expect(TRUE, r);
2134     ok(lstrcmp(buff, names[0]) == 0, "Expected '%s', got '%s'\n", names[0], buff);
2135
2136     item.iItem = 4;
2137     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
2138     expect(TRUE, r);
2139     ok(lstrcmp(buff, names[3]) == 0, "Expected '%s', got '%s'\n", names[3], buff);
2140
2141     DestroyWindow(hwnd);
2142 }
2143
2144 static void test_ownerdata(void)
2145 {
2146     HWND hwnd;
2147     LONG_PTR style, ret;
2148     DWORD res;
2149     LVITEMA item;
2150
2151     /* it isn't possible to set LVS_OWNERDATA after creation */
2152     hwnd = create_listview_control(0);
2153     ok(hwnd != NULL, "failed to create a listview window\n");
2154     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2155     ok(!(style & LVS_OWNERDATA) && style, "LVS_OWNERDATA isn't expected\n");
2156
2157     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2158
2159     ret = SetWindowLongPtrA(hwnd, GWL_STYLE, style | LVS_OWNERDATA);
2160     ok(ret == style, "Expected set GWL_STYLE to succeed\n");
2161     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_ownerdata_switchto_seq,
2162                 "try to switch to LVS_OWNERDATA seq", FALSE);
2163
2164     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2165     ok(!(style & LVS_OWNERDATA), "LVS_OWNERDATA isn't expected\n");
2166     DestroyWindow(hwnd);
2167
2168     /* try to set LVS_OWNERDATA after creation just having it */
2169     hwnd = create_listview_control(LVS_OWNERDATA);
2170     ok(hwnd != NULL, "failed to create a listview window\n");
2171     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2172     ok(style & LVS_OWNERDATA, "LVS_OWNERDATA is expected\n");
2173
2174     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2175
2176     ret = SetWindowLongPtrA(hwnd, GWL_STYLE, style | LVS_OWNERDATA);
2177     ok(ret == style, "Expected set GWL_STYLE to succeed\n");
2178     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_ownerdata_switchto_seq,
2179                 "try to switch to LVS_OWNERDATA seq", FALSE);
2180     DestroyWindow(hwnd);
2181
2182     /* try to remove LVS_OWNERDATA after creation just having it */
2183     hwnd = create_listview_control(LVS_OWNERDATA);
2184     ok(hwnd != NULL, "failed to create a listview window\n");
2185     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2186     ok(style & LVS_OWNERDATA, "LVS_OWNERDATA is expected\n");
2187
2188     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2189
2190     ret = SetWindowLongPtrA(hwnd, GWL_STYLE, style & ~LVS_OWNERDATA);
2191     ok(ret == style, "Expected set GWL_STYLE to succeed\n");
2192     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_ownerdata_switchto_seq,
2193                 "try to switch to LVS_OWNERDATA seq", FALSE);
2194     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2195     ok(style & LVS_OWNERDATA, "LVS_OWNERDATA is expected\n");
2196     DestroyWindow(hwnd);
2197
2198     /* try select an item */
2199     hwnd = create_listview_control(LVS_OWNERDATA);
2200     ok(hwnd != NULL, "failed to create a listview window\n");
2201     res = SendMessageA(hwnd, LVM_SETITEMCOUNT, 1, 0);
2202     ok(res != 0, "Expected LVM_SETITEMCOUNT to succeed\n");
2203     res = SendMessageA(hwnd, LVM_GETSELECTEDCOUNT, 0, 0);
2204     expect(0, res);
2205     memset(&item, 0, sizeof(item));
2206     item.stateMask = LVIS_SELECTED;
2207     item.state     = LVIS_SELECTED;
2208     res = SendMessageA(hwnd, LVM_SETITEMSTATE, 0, (LPARAM)&item);
2209     expect(TRUE, res);
2210     res = SendMessageA(hwnd, LVM_GETSELECTEDCOUNT, 0, 0);
2211     expect(1, res);
2212     res = SendMessageA(hwnd, LVM_GETITEMCOUNT, 0, 0);
2213     expect(1, res);
2214     DestroyWindow(hwnd);
2215
2216     /* LVM_SETITEM is unsupported on LVS_OWNERDATA */
2217     hwnd = create_listview_control(LVS_OWNERDATA);
2218     ok(hwnd != NULL, "failed to create a listview window\n");
2219     res = SendMessageA(hwnd, LVM_SETITEMCOUNT, 1, 0);
2220     ok(res != 0, "Expected LVM_SETITEMCOUNT to succeed\n");
2221     res = SendMessageA(hwnd, LVM_GETITEMCOUNT, 0, 0);
2222     expect(1, res);
2223     memset(&item, 0, sizeof(item));
2224     item.mask = LVIF_STATE;
2225     item.iItem = 0;
2226     item.stateMask = LVIS_SELECTED;
2227     item.state     = LVIS_SELECTED;
2228     res = SendMessageA(hwnd, LVM_SETITEM, 0, (LPARAM)&item);
2229     expect(FALSE, res);
2230     DestroyWindow(hwnd);
2231
2232     /* check notifications after focused/selected changed */
2233     hwnd = create_listview_control(LVS_OWNERDATA);
2234     ok(hwnd != NULL, "failed to create a listview window\n");
2235     res = SendMessageA(hwnd, LVM_SETITEMCOUNT, 1, 0);
2236     ok(res != 0, "Expected LVM_SETITEMCOUNT to succeed\n");
2237
2238     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2239
2240     memset(&item, 0, sizeof(item));
2241     item.stateMask = LVIS_SELECTED;
2242     item.state     = LVIS_SELECTED;
2243     res = SendMessageA(hwnd, LVM_SETITEMSTATE, 0, (LPARAM)&item);
2244     expect(TRUE, res);
2245
2246     ok_sequence(sequences, PARENT_SEQ_INDEX, ownderdata_select_focus_parent_seq,
2247                 "ownerdata select notification", TRUE);
2248
2249     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2250
2251     memset(&item, 0, sizeof(item));
2252     item.stateMask = LVIS_FOCUSED;
2253     item.state     = LVIS_FOCUSED;
2254     res = SendMessageA(hwnd, LVM_SETITEMSTATE, 0, (LPARAM)&item);
2255     expect(TRUE, res);
2256
2257     ok_sequence(sequences, PARENT_SEQ_INDEX, ownderdata_select_focus_parent_seq,
2258                 "ownerdata focus notification", TRUE);
2259     DestroyWindow(hwnd);
2260
2261     /* check notifications on LVM_GETITEM */
2262     /* zero callback mask */
2263     hwnd = create_listview_control(LVS_OWNERDATA);
2264     ok(hwnd != NULL, "failed to create a listview window\n");
2265     res = SendMessageA(hwnd, LVM_SETITEMCOUNT, 1, 0);
2266     ok(res != 0, "Expected LVM_SETITEMCOUNT to succeed\n");
2267
2268     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2269
2270     memset(&item, 0, sizeof(item));
2271     item.stateMask = LVIS_SELECTED;
2272     item.mask      = LVIF_STATE;
2273     res = SendMessageA(hwnd, LVM_GETITEMA, 0, (LPARAM)&item);
2274     expect(TRUE, res);
2275
2276     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq,
2277                 "ownerdata getitem selected state 1", FALSE);
2278
2279     /* non zero callback mask but not we asking for */
2280     res = SendMessageA(hwnd, LVM_SETCALLBACKMASK, LVIS_OVERLAYMASK, 0);
2281     expect(TRUE, res);
2282
2283     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2284
2285     memset(&item, 0, sizeof(item));
2286     item.stateMask = LVIS_SELECTED;
2287     item.mask      = LVIF_STATE;
2288     res = SendMessageA(hwnd, LVM_GETITEMA, 0, (LPARAM)&item);
2289     expect(TRUE, res);
2290
2291     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq,
2292                 "ownerdata getitem selected state 2", FALSE);
2293
2294     /* LVIS_OVERLAYMASK callback mask, asking for index */
2295     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2296
2297     memset(&item, 0, sizeof(item));
2298     item.stateMask = LVIS_OVERLAYMASK;
2299     item.mask      = LVIF_STATE;
2300     res = SendMessageA(hwnd, LVM_GETITEMA, 0, (LPARAM)&item);
2301     expect(TRUE, res);
2302
2303     ok_sequence(sequences, PARENT_SEQ_INDEX, single_getdispinfo_parent_seq,
2304                 "ownerdata getitem selected state 2", FALSE);
2305
2306     DestroyWindow(hwnd);
2307
2308     /* LVS_SORTASCENDING/LVS_SORTDESCENDING aren't compatible with LVS_OWNERDATA */
2309     hwnd = create_listview_control(LVS_OWNERDATA | LVS_SORTASCENDING);
2310     ok(hwnd != NULL, "failed to create a listview window\n");
2311     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2312     ok(style & LVS_OWNERDATA, "Expected LVS_OWNERDATA\n");
2313     ok(style & LVS_SORTASCENDING, "Expected LVS_SORTASCENDING to be set\n");
2314     SetWindowLongPtrA(hwnd, GWL_STYLE, style & ~LVS_SORTASCENDING);
2315     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2316     ok(!(style & LVS_SORTASCENDING), "Expected LVS_SORTASCENDING not set\n");
2317     DestroyWindow(hwnd);
2318     /* apparently it's allowed to switch these style on after creation */
2319     hwnd = create_listview_control(LVS_OWNERDATA);
2320     ok(hwnd != NULL, "failed to create a listview window\n");
2321     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2322     ok(style & LVS_OWNERDATA, "Expected LVS_OWNERDATA\n");
2323     SetWindowLongPtrA(hwnd, GWL_STYLE, style | LVS_SORTASCENDING);
2324     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2325     ok(style & LVS_SORTASCENDING, "Expected LVS_SORTASCENDING to be set\n");
2326     DestroyWindow(hwnd);
2327
2328     hwnd = create_listview_control(LVS_OWNERDATA);
2329     ok(hwnd != NULL, "failed to create a listview window\n");
2330     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2331     ok(style & LVS_OWNERDATA, "Expected LVS_OWNERDATA\n");
2332     SetWindowLongPtrA(hwnd, GWL_STYLE, style | LVS_SORTDESCENDING);
2333     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
2334     ok(style & LVS_SORTDESCENDING, "Expected LVS_SORTDESCENDING to be set\n");
2335     DestroyWindow(hwnd);
2336 }
2337
2338 static void test_norecompute(void)
2339 {
2340     static CHAR testA[] = "test";
2341     CHAR buff[10];
2342     LVITEMA item;
2343     HWND hwnd;
2344     DWORD res;
2345
2346     /* self containing control */
2347     hwnd = create_listview_control(0);
2348     ok(hwnd != NULL, "failed to create a listview window\n");
2349     memset(&item, 0, sizeof(item));
2350     item.mask = LVIF_TEXT | LVIF_STATE;
2351     item.iItem = 0;
2352     item.stateMask = LVIS_SELECTED;
2353     item.state     = LVIS_SELECTED;
2354     item.pszText   = testA;
2355     res = SendMessageA(hwnd, LVM_INSERTITEM, 0, (LPARAM)&item);
2356     expect(0, res);
2357     /* retrieve with LVIF_NORECOMPUTE */
2358     item.mask  = LVIF_TEXT | LVIF_NORECOMPUTE;
2359     item.iItem = 0;
2360     item.pszText    = buff;
2361     item.cchTextMax = sizeof(buff)/sizeof(CHAR);
2362     res = SendMessageA(hwnd, LVM_GETITEM, 0, (LPARAM)&item);
2363     expect(TRUE, res);
2364     ok(lstrcmp(buff, testA) == 0, "Expected (%s), got (%s)\n", testA, buff);
2365
2366     item.mask = LVIF_TEXT;
2367     item.iItem = 1;
2368     item.pszText = LPSTR_TEXTCALLBACK;
2369     res = SendMessageA(hwnd, LVM_INSERTITEM, 0, (LPARAM)&item);
2370     expect(1, res);
2371
2372     item.mask  = LVIF_TEXT | LVIF_NORECOMPUTE;
2373     item.iItem = 1;
2374     item.pszText    = buff;
2375     item.cchTextMax = sizeof(buff)/sizeof(CHAR);
2376
2377     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2378     res = SendMessageA(hwnd, LVM_GETITEM, 0, (LPARAM)&item);
2379     expect(TRUE, res);
2380     ok(item.pszText == LPSTR_TEXTCALLBACK, "Expected (%p), got (%p)\n",
2381        LPSTR_TEXTCALLBACK, (VOID*)item.pszText);
2382     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq, "retrieve with LVIF_NORECOMPUTE seq", FALSE);
2383
2384     DestroyWindow(hwnd);
2385
2386     /* LVS_OWNERDATA */
2387     hwnd = create_listview_control(LVS_OWNERDATA);
2388     ok(hwnd != NULL, "failed to create a listview window\n");
2389
2390     item.mask = LVIF_STATE;
2391     item.stateMask = LVIS_SELECTED;
2392     item.state     = LVIS_SELECTED;
2393     item.iItem = 0;
2394     res = SendMessageA(hwnd, LVM_INSERTITEM, 0, (LPARAM)&item);
2395     expect(0, res);
2396
2397     item.mask  = LVIF_TEXT | LVIF_NORECOMPUTE;
2398     item.iItem = 0;
2399     item.pszText    = buff;
2400     item.cchTextMax = sizeof(buff)/sizeof(CHAR);
2401     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2402     res = SendMessageA(hwnd, LVM_GETITEM, 0, (LPARAM)&item);
2403     expect(TRUE, res);
2404     ok(item.pszText == LPSTR_TEXTCALLBACK, "Expected (%p), got (%p)\n",
2405        LPSTR_TEXTCALLBACK, (VOID*)item.pszText);
2406     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq, "retrieve with LVIF_NORECOMPUTE seq 2", FALSE);
2407
2408     DestroyWindow(hwnd);
2409 }
2410
2411 static void test_nosortheader(void)
2412 {
2413     HWND hwnd, header;
2414     LONG_PTR style;
2415
2416     hwnd = create_listview_control(0);
2417     ok(hwnd != NULL, "failed to create a listview window\n");
2418
2419     header = (HWND)SendMessageA(hwnd, LVM_GETHEADER, 0, 0);
2420     ok(IsWindow(header), "header expected\n");
2421
2422     style = GetWindowLongPtr(header, GWL_STYLE);
2423     ok(style & HDS_BUTTONS, "expected header to have HDS_BUTTONS\n");
2424
2425     style = GetWindowLongPtr(hwnd, GWL_STYLE);
2426     SetWindowLongPtr(hwnd, GWL_STYLE, style | LVS_NOSORTHEADER);
2427     /* HDS_BUTTONS retained */
2428     style = GetWindowLongPtr(header, GWL_STYLE);
2429     ok(style & HDS_BUTTONS, "expected header to retain HDS_BUTTONS\n");
2430
2431     DestroyWindow(hwnd);
2432
2433     /* create with LVS_NOSORTHEADER */
2434     hwnd = create_listview_control(LVS_NOSORTHEADER);
2435     ok(hwnd != NULL, "failed to create a listview window\n");
2436
2437     header = (HWND)SendMessageA(hwnd, LVM_GETHEADER, 0, 0);
2438     ok(IsWindow(header), "header expected\n");
2439
2440     style = GetWindowLongPtr(header, GWL_STYLE);
2441     ok(!(style & HDS_BUTTONS), "expected header to have no HDS_BUTTONS\n");
2442
2443     style = GetWindowLongPtr(hwnd, GWL_STYLE);
2444     SetWindowLongPtr(hwnd, GWL_STYLE, style & ~LVS_NOSORTHEADER);
2445     /* not changed here */
2446     style = GetWindowLongPtr(header, GWL_STYLE);
2447     ok(!(style & HDS_BUTTONS), "expected header to have no HDS_BUTTONS\n");
2448
2449     DestroyWindow(hwnd);
2450 }
2451
2452 static void test_setredraw(void)
2453 {
2454     HWND hwnd;
2455     DWORD_PTR style;
2456     DWORD ret;
2457
2458     hwnd = create_listview_control(0);
2459     ok(hwnd != NULL, "failed to create a listview window\n");
2460
2461     /* Passing WM_SETREDRAW to DefWinProc removes WS_VISIBLE.
2462        ListView seems to handle it internally without DefWinProc */
2463
2464     /* default value first */
2465     ret = SendMessage(hwnd, WM_SETREDRAW, TRUE, 0);
2466     expect(0, ret);
2467     /* disable */
2468     style = GetWindowLongPtr(hwnd, GWL_STYLE);
2469     ok(style & WS_VISIBLE, "Expected WS_VISIBLE to be set\n");
2470     ret = SendMessage(hwnd, WM_SETREDRAW, FALSE, 0);
2471     expect(0, ret);
2472     style = GetWindowLongPtr(hwnd, GWL_STYLE);
2473     ok(style & WS_VISIBLE, "Expected WS_VISIBLE to be set\n");
2474
2475     DestroyWindow(hwnd);
2476 }
2477
2478 static void test_hittest(void)
2479 {
2480     HWND hwnd;
2481     DWORD r;
2482     RECT bounds;
2483     LVITEMA item;
2484     static CHAR text[] = "1234567890ABCDEFGHIJKLMNOPQRST";
2485     POINT pos;
2486     INT x, y;
2487     HIMAGELIST himl, himl2;
2488     HBITMAP hbmp;
2489
2490     hwnd = create_listview_control(0);
2491     ok(hwnd != NULL, "failed to create a listview window\n");
2492
2493     /* LVS_REPORT with a single subitem (2 columns) */
2494     insert_column(hwnd, 0);
2495     insert_column(hwnd, 1);
2496     insert_item(hwnd, 0);
2497
2498     item.iSubItem = 0;
2499     /* the only purpose of that line is to be as long as a half item rect */
2500     item.pszText  = text;
2501     r = SendMessage(hwnd, LVM_SETITEMTEXT, 0, (LPARAM)&item);
2502     expect(TRUE, r);
2503
2504     r = SendMessage(hwnd, LVM_SETCOLUMNWIDTH, 0, MAKELPARAM(100, 0));
2505     expect(TRUE, r);
2506     r = SendMessage(hwnd, LVM_SETCOLUMNWIDTH, 1, MAKELPARAM(100, 0));
2507     expect(TRUE, r);
2508
2509     memset(&bounds, 0, sizeof(bounds));
2510     bounds.left = LVIR_BOUNDS;
2511     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&bounds);
2512     ok(bounds.bottom - bounds.top > 0, "Expected non zero item height\n");
2513     ok(bounds.right - bounds.left > 0, "Expected non zero item width\n");
2514     r = SendMessage(hwnd, LVM_GETITEMPOSITION, 0, (LPARAM)&pos);
2515     expect(TRUE, r);
2516
2517     /* LVS_EX_FULLROWSELECT not set, no icons attached */
2518     x = pos.x + 50; /* column half width */
2519     y = pos.y + (bounds.bottom - bounds.top) / 2;
2520     test_lvm_hittest(hwnd, x, y, 0, LVHT_ONITEMLABEL, FALSE, FALSE, __LINE__);
2521     test_lvm_subitemhittest(hwnd, x, y, 0, 0, LVHT_ONITEMLABEL, FALSE, FALSE, FALSE, __LINE__);
2522     x = pos.x + 150; /* outside column */
2523     y = pos.y + (bounds.bottom - bounds.top) / 2;
2524     test_lvm_hittest(hwnd, x, y, -1, LVHT_TORIGHT, FALSE, FALSE, __LINE__);
2525     test_lvm_subitemhittest(hwnd, x, y, 0, 1, LVHT_ONITEMLABEL, TRUE, TRUE, TRUE, __LINE__);
2526     /* parent client area is 100x100 by default */
2527     MoveWindow(hwnd, 0, 0, 300, 100, FALSE);
2528     x = pos.x + 150; /* outside column */
2529     y = pos.y + (bounds.bottom - bounds.top) / 2;
2530     test_lvm_hittest(hwnd, x, y, -1, LVHT_NOWHERE, FALSE, FALSE, __LINE__);
2531     test_lvm_subitemhittest(hwnd, x, y, 0, 1, LVHT_ONITEMLABEL, FALSE, FALSE, FALSE, __LINE__);
2532     /* the same with LVS_EX_FULLROWSELECT */
2533     SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);
2534     x = pos.x + 150; /* outside column */
2535     y = pos.y + (bounds.bottom - bounds.top) / 2;
2536     test_lvm_hittest(hwnd, x, y, 0, LVHT_ONITEM, FALSE, FALSE, __LINE__);
2537     test_lvm_subitemhittest(hwnd, x, y, 0, 1, LVHT_ONITEMLABEL, FALSE, FALSE, FALSE, __LINE__);
2538     MoveWindow(hwnd, 0, 0, 100, 100, FALSE);
2539     x = pos.x + 150; /* outside column */
2540     y = pos.y + (bounds.bottom - bounds.top) / 2;
2541     test_lvm_hittest(hwnd, x, y, -1, LVHT_TORIGHT, FALSE, FALSE, __LINE__);
2542     test_lvm_subitemhittest(hwnd, x, y, 0, 1, LVHT_ONITEMLABEL, TRUE, TRUE, TRUE, __LINE__);
2543     /* try with icons, state icons index is 1 based so at least 2 bitmaps needed */
2544     himl = ImageList_Create(16, 16, 0, 4, 4);
2545     ok(himl != NULL, "failed to create imagelist\n");
2546     hbmp = CreateBitmap(16, 16, 1, 1, NULL);
2547     ok(hbmp != NULL, "failed to create bitmap\n");
2548     r = ImageList_Add(himl, hbmp, 0);
2549     ok(r == 0, "should be zero\n");
2550     hbmp = CreateBitmap(16, 16, 1, 1, NULL);
2551     ok(hbmp != NULL, "failed to create bitmap\n");
2552     r = ImageList_Add(himl, hbmp, 0);
2553     ok(r == 1, "should be one\n");
2554
2555     r = SendMessage(hwnd, LVM_SETIMAGELIST, LVSIL_STATE, (LPARAM)himl);
2556     ok(r == 0, "should return zero\n");
2557
2558     item.mask = LVIF_IMAGE;
2559     item.iImage = 0;
2560     item.iItem = 0;
2561     item.iSubItem = 0;
2562     r = SendMessage(hwnd, LVM_SETITEM, 0, (LPARAM)&item);
2563     expect(TRUE, r);
2564     /* on state icon */
2565     x = pos.x + 8;
2566     y = pos.y + (bounds.bottom - bounds.top) / 2;
2567     test_lvm_hittest(hwnd, x, y, 0, LVHT_ONITEMSTATEICON, FALSE, FALSE, __LINE__);
2568     test_lvm_subitemhittest(hwnd, x, y, 0, 0, LVHT_ONITEMSTATEICON, FALSE, FALSE, FALSE, __LINE__);
2569
2570     /* state icons indices are 1 based, check with valid index */
2571     item.mask = LVIF_STATE;
2572     item.state = INDEXTOSTATEIMAGEMASK(1);
2573     item.stateMask = LVIS_STATEIMAGEMASK;
2574     item.iItem = 0;
2575     item.iSubItem = 0;
2576     r = SendMessage(hwnd, LVM_SETITEM, 0, (LPARAM)&item);
2577     expect(TRUE, r);
2578     /* on state icon */
2579     x = pos.x + 8;
2580     y = pos.y + (bounds.bottom - bounds.top) / 2;
2581     test_lvm_hittest(hwnd, x, y, 0, LVHT_ONITEMSTATEICON, FALSE, FALSE, __LINE__);
2582     test_lvm_subitemhittest(hwnd, x, y, 0, 0, LVHT_ONITEMSTATEICON, FALSE, FALSE, FALSE, __LINE__);
2583
2584     himl2 = (HIMAGELIST)SendMessage(hwnd, LVM_SETIMAGELIST, LVSIL_STATE, (LPARAM)NULL);
2585     ok(himl2 == himl, "should return handle\n");
2586
2587     r = SendMessage(hwnd, LVM_SETIMAGELIST, LVSIL_SMALL, (LPARAM)himl);
2588     ok(r == 0, "should return zero\n");
2589     /* on item icon */
2590     x = pos.x + 8;
2591     y = pos.y + (bounds.bottom - bounds.top) / 2;
2592     test_lvm_hittest(hwnd, x, y, 0, LVHT_ONITEMICON, FALSE, FALSE, __LINE__);
2593     test_lvm_subitemhittest(hwnd, x, y, 0, 0, LVHT_ONITEMICON, FALSE, FALSE, FALSE, __LINE__);
2594
2595     DestroyWindow(hwnd);
2596 }
2597
2598 static void test_getviewrect(void)
2599 {
2600     HWND hwnd;
2601     DWORD r;
2602     RECT rect;
2603     LVITEMA item;
2604
2605     hwnd = create_listview_control(0);
2606     ok(hwnd != NULL, "failed to create a listview window\n");
2607
2608     /* empty */
2609     r = SendMessage(hwnd, LVM_GETVIEWRECT, 0, (LPARAM)&rect);
2610     expect(TRUE, r);
2611
2612     insert_column(hwnd, 0);
2613     insert_column(hwnd, 1);
2614
2615     memset(&item, 0, sizeof(item));
2616     item.iItem = 0;
2617     item.iSubItem = 0;
2618     SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM)&item);
2619
2620     r = SendMessage(hwnd, LVM_SETCOLUMNWIDTH, 0, MAKELPARAM(100, 0));
2621     expect(TRUE, r);
2622     r = SendMessage(hwnd, LVM_SETCOLUMNWIDTH, 1, MAKELPARAM(120, 0));
2623     expect(TRUE, r);
2624
2625     rect.left = rect.right = rect.top = rect.bottom = -1;
2626     r = SendMessage(hwnd, LVM_GETVIEWRECT, 0, (LPARAM)&rect);
2627     expect(TRUE, r);
2628     /* left is set to (2e31-1) - XP SP2 */
2629     expect(0, rect.right);
2630     expect(0, rect.top);
2631     expect(0, rect.bottom);
2632
2633     /* switch to LVS_ICON */
2634     SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~LVS_REPORT);
2635
2636     rect.left = rect.right = rect.top = rect.bottom = -1;
2637     r = SendMessage(hwnd, LVM_GETVIEWRECT, 0, (LPARAM)&rect);
2638     expect(TRUE, r);
2639     expect(0, rect.left);
2640     expect(0, rect.top);
2641     /* precise value differs for 2k, XP and Vista */
2642     ok(rect.bottom > 0, "Expected positive bottom value, got %d\n", rect.bottom);
2643     ok(rect.right  > 0, "Expected positive right value, got %d\n", rect.right);
2644
2645     DestroyWindow(hwnd);
2646 }
2647
2648 static void test_getitemposition(void)
2649 {
2650     HWND hwnd, header;
2651     DWORD r;
2652     POINT pt;
2653     RECT rect;
2654
2655     hwnd = create_listview_control(0);
2656     ok(hwnd != NULL, "failed to create a listview window\n");
2657     header = subclass_header(hwnd);
2658
2659     /* LVS_REPORT, single item, no columns added */
2660     insert_item(hwnd, 0);
2661
2662     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2663
2664     pt.x = pt.y = -1;
2665     r = SendMessage(hwnd, LVM_GETITEMPOSITION, 0, (LPARAM)&pt);
2666     expect(TRUE, r);
2667     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, getitemposition_seq1, "get item position 1", FALSE);
2668
2669     /* LVS_REPORT, single item, single column */
2670     insert_column(hwnd, 0);
2671
2672     flush_sequences(sequences, NUM_MSG_SEQUENCES);
2673
2674     pt.x = pt.y = -1;
2675     r = SendMessage(hwnd, LVM_GETITEMPOSITION, 0, (LPARAM)&pt);
2676     expect(TRUE, r);
2677     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, getitemposition_seq2, "get item position 2", TRUE);
2678
2679     memset(&rect, 0, sizeof(rect));
2680     SendMessage(header, HDM_GETITEMRECT, 0, (LPARAM)&rect);
2681     /* some padding? */
2682     expect(2, pt.x);
2683     /* offset by header height */
2684     expect(rect.bottom - rect.top, pt.y);
2685
2686     DestroyWindow(hwnd);
2687 }
2688
2689 static void test_columnscreation(void)
2690 {
2691     HWND hwnd, header;
2692     DWORD r;
2693
2694     hwnd = create_listview_control(0);
2695     ok(hwnd != NULL, "failed to create a listview window\n");
2696
2697     insert_item(hwnd, 0);
2698
2699     /* headers columns aren't created automatically */
2700     header = (HWND)SendMessage(hwnd, LVM_GETHEADER, 0, 0);
2701     ok(IsWindow(header), "Expected header handle\n");
2702     r = SendMessage(header, HDM_GETITEMCOUNT, 0, 0);
2703     expect(0, r);
2704
2705     DestroyWindow(hwnd);
2706 }
2707
2708 static void test_getitemrect(void)
2709 {
2710     HWND hwnd;
2711     HIMAGELIST himl;
2712     HBITMAP hbm;
2713     RECT rect;
2714     DWORD r;
2715     LVITEMA item;
2716     LVCOLUMNA col;
2717     INT order[2];
2718     POINT pt;
2719
2720     hwnd = create_listview_control(0);
2721     ok(hwnd != NULL, "failed to create a listview window\n");
2722
2723     /* empty item */
2724     memset(&item, 0, sizeof(item));
2725     item.iItem = 0;
2726     item.iSubItem = 0;
2727     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM)&item);
2728     expect(0, r);
2729
2730     rect.left = LVIR_BOUNDS;
2731     rect.right = rect.top = rect.bottom = -1;
2732     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
2733     expect(TRUE, r);
2734
2735     /* zero width rectangle with no padding */
2736     expect(0, rect.left);
2737     todo_wine expect(0, rect.right);
2738
2739     insert_column(hwnd, 0);
2740     insert_column(hwnd, 1);
2741
2742     col.mask = LVCF_WIDTH;
2743     col.cx   = 50;
2744     r = SendMessage(hwnd, LVM_SETCOLUMN, 0, (LPARAM)&col);
2745     expect(TRUE, r);
2746
2747     col.mask = LVCF_WIDTH;
2748     col.cx   = 100;
2749     r = SendMessage(hwnd, LVM_SETCOLUMN, 1, (LPARAM)&col);
2750     expect(TRUE, r);
2751
2752     rect.left = LVIR_BOUNDS;
2753     rect.right = rect.top = rect.bottom = -1;
2754     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
2755     expect(TRUE, r);
2756
2757     /* still no left padding */
2758     expect(0, rect.left);
2759     expect(150, rect.right);
2760
2761     rect.left = LVIR_SELECTBOUNDS;
2762     rect.right = rect.top = rect.bottom = -1;
2763     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
2764     expect(TRUE, r);
2765     /* padding */
2766     expect(2, rect.left);
2767
2768     rect.left = LVIR_LABEL;
2769     rect.right = rect.top = rect.bottom = -1;
2770     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
2771     expect(TRUE, r);
2772     /* padding, column width */
2773     expect(2, rect.left);
2774     expect(50, rect.right);
2775
2776     /* no icons attached */
2777     rect.left = LVIR_ICON;
2778     rect.right = rect.top = rect.bottom = -1;
2779     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
2780     expect(TRUE, r);
2781     /* padding */
2782     expect(2, rect.left);
2783     expect(2, rect.right);
2784
2785     /* change order */
2786     order[0] = 1; order[1] = 0;
2787     r = SendMessage(hwnd, LVM_SETCOLUMNORDERARRAY, 2, (LPARAM)&order);
2788     expect(TRUE, r);
2789     pt.x = -1;
2790     r = SendMessage(hwnd, LVM_GETITEMPOSITION, 0, (LPARAM)&pt);
2791     expect(TRUE, r);
2792     /* 1 indexed column width + padding */
2793     todo_wine expect(102, pt.x);
2794     /* rect is at zero too */
2795     rect.left = LVIR_BOUNDS;
2796     rect.right = rect.top = rect.bottom = -1;
2797     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
2798     expect(TRUE, r);
2799     expect(0, rect.left);
2800     /* just width sum */
2801     expect(150, rect.right);
2802
2803     rect.left = LVIR_SELECTBOUNDS;
2804     rect.right = rect.top = rect.bottom = -1;
2805     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
2806     expect(TRUE, r);
2807     /* column width + padding */
2808     todo_wine expect(102, rect.left);
2809
2810     /* back to initial order */
2811     order[0] = 0; order[1] = 1;
2812     r = SendMessage(hwnd, LVM_SETCOLUMNORDERARRAY, 2, (LPARAM)&order);
2813     expect(TRUE, r);
2814
2815     /* state icons */
2816     himl = ImageList_Create(16, 16, 0, 2, 2);
2817     ok(himl != NULL, "failed to create imagelist\n");
2818     hbm = CreateBitmap(16, 16, 1, 1, NULL);
2819     ok(hbm != NULL, "failed to create bitmap\n");
2820     r = ImageList_Add(himl, hbm, 0);
2821     ok(r == 0, "should be zero\n");
2822     hbm = CreateBitmap(16, 16, 1, 1, NULL);
2823     ok(hbm != NULL, "failed to create bitmap\n");
2824     r = ImageList_Add(himl, hbm, 0);
2825     ok(r == 1, "should be one\n");
2826
2827     r = SendMessage(hwnd, LVM_SETIMAGELIST, LVSIL_STATE, (LPARAM)himl);
2828     ok(r == 0, "should return zero\n");
2829
2830     item.mask = LVIF_STATE;
2831     item.state = INDEXTOSTATEIMAGEMASK(1);
2832     item.stateMask = LVIS_STATEIMAGEMASK;
2833     item.iItem = 0;
2834     item.iSubItem = 0;
2835     r = SendMessage(hwnd, LVM_SETITEM, 0, (LPARAM)&item);
2836     expect(TRUE, r);
2837
2838     /* icon bounds */
2839     rect.left = LVIR_ICON;
2840     rect.right = rect.top = rect.bottom = -1;
2841     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
2842     expect(TRUE, r);
2843     /* padding + stateicon width */
2844     expect(18, rect.left);
2845     expect(18, rect.right);
2846     /* label bounds */
2847     rect.left = LVIR_LABEL;
2848     rect.right = rect.top = rect.bottom = -1;
2849     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
2850     expect(TRUE, r);
2851     /* padding + stateicon width -> column width */
2852     expect(18, rect.left);
2853     expect(50, rect.right);
2854
2855     r = SendMessage(hwnd, LVM_SETIMAGELIST, LVSIL_STATE, (LPARAM)NULL);
2856     ok(r != 0, "should return current list handle\n");
2857
2858     r = SendMessage(hwnd, LVM_SETIMAGELIST, LVSIL_SMALL, (LPARAM)himl);
2859     ok(r == 0, "should return zero\n");
2860
2861     item.mask = LVIF_STATE | LVIF_IMAGE;
2862     item.iImage = 1;
2863     item.state = 0;
2864     item.stateMask = ~0;
2865     item.iItem = 0;
2866     item.iSubItem = 0;
2867     r = SendMessage(hwnd, LVM_SETITEM, 0, (LPARAM)&item);
2868     expect(TRUE, r);
2869
2870     /* icon bounds */
2871     rect.left = LVIR_ICON;
2872     rect.right = rect.top = rect.bottom = -1;
2873     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
2874     expect(TRUE, r);
2875     /* padding, icon width */
2876     expect(2, rect.left);
2877     expect(18, rect.right);
2878     /* label bounds */
2879     rect.left = LVIR_LABEL;
2880     rect.right = rect.top = rect.bottom = -1;
2881     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
2882     expect(TRUE, r);
2883     /* padding + icon width -> column width */
2884     expect(18, rect.left);
2885     expect(50, rect.right);
2886
2887     /* select bounds */
2888     rect.left = LVIR_SELECTBOUNDS;
2889     rect.right = rect.top = rect.bottom = -1;
2890     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
2891     expect(TRUE, r);
2892     /* padding, column width */
2893     expect(2, rect.left);
2894     todo_wine expect(50, rect.right);
2895
2896     /* try with indentation */
2897     item.mask = LVIF_INDENT;
2898     item.iIndent = 1;
2899     item.iItem = 0;
2900     item.iSubItem = 0;
2901     r = SendMessage(hwnd, LVM_SETITEM, 0, (LPARAM)&item);
2902     expect(TRUE, r);
2903
2904     /* bounds */
2905     rect.left = LVIR_BOUNDS;
2906     rect.right = rect.top = rect.bottom = -1;
2907     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
2908     expect(TRUE, r);
2909     /* padding + 1 icon width, column width */
2910     expect(0, rect.left);
2911     expect(150, rect.right);
2912
2913     /* select bounds */
2914     rect.left = LVIR_SELECTBOUNDS;
2915     rect.right = rect.top = rect.bottom = -1;
2916     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
2917     expect(TRUE, r);
2918     /* padding + 1 icon width, column width */
2919     expect(2 + 16, rect.left);
2920     todo_wine expect(50, rect.right);
2921
2922     /* label bounds */
2923     rect.left = LVIR_LABEL;
2924     rect.right = rect.top = rect.bottom = -1;
2925     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
2926     expect(TRUE, r);
2927     /* padding + 2 icon widths, column width */
2928     expect(2 + 16*2, rect.left);
2929     expect(50, rect.right);
2930
2931     /* icon bounds */
2932     rect.left = LVIR_ICON;
2933     rect.right = rect.top = rect.bottom = -1;
2934     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM)&rect);
2935     expect(TRUE, r);
2936     /* padding + 1 icon width indentation, icon width */
2937     expect(2 + 16, rect.left);
2938     expect(34, rect.right);
2939
2940
2941     DestroyWindow(hwnd);
2942 }
2943
2944 static void test_editbox(void)
2945 {
2946     HWND hwnd, hwndedit, hwndedit2;
2947     LVITEMA item;
2948     DWORD r;
2949     static CHAR testitemA[]  = "testitem";
2950     static CHAR testitem1A[] = "testitem1";
2951     static CHAR buffer[10];
2952
2953     hwnd = create_listview_control(LVS_EDITLABELS);
2954     ok(hwnd != NULL, "failed to create a listview window\n");
2955
2956     insert_column(hwnd, 0);
2957
2958     memset(&item, 0, sizeof(item));
2959     item.mask = LVIF_TEXT;
2960     item.pszText = testitemA;
2961     item.iItem = 0;
2962     item.iSubItem = 0;
2963     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM)&item);
2964     expect(0, r);
2965
2966     /* setting focus is necessary */
2967     SetFocus(hwnd);
2968     hwndedit = (HWND)SendMessage(hwnd, LVM_EDITLABEL, 0, 0);
2969     ok(IsWindow(hwndedit), "Expected Edit window to be created\n");
2970
2971     /* modify initial string */
2972     r = SendMessage(hwndedit, WM_SETTEXT, 0, (LPARAM)testitem1A);
2973     expect(TRUE, r);
2974     /* return focus to listview */
2975     SetFocus(hwnd);
2976
2977     memset(&item, 0, sizeof(item));
2978     item.mask = LVIF_TEXT;
2979     item.pszText = buffer;
2980     item.cchTextMax = 10;
2981     item.iItem = 0;
2982     item.iSubItem = 0;
2983     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM)&item);
2984     expect(TRUE, r);
2985
2986     ok(strcmp(buffer, testitem1A) == 0, "Expected item text to change\n");
2987
2988     /* send LVM_EDITLABEL on already created edit */
2989     SetFocus(hwnd);
2990     hwndedit = (HWND)SendMessage(hwnd, LVM_EDITLABEL, 0, 0);
2991     ok(IsWindow(hwndedit), "Expected Edit window to be created\n");
2992     /* focus will be set to edit */
2993     ok(GetFocus() == hwndedit, "Expected Edit window to be focused\n");
2994     hwndedit2 = (HWND)SendMessage(hwnd, LVM_EDITLABEL, 0, 0);
2995     ok(IsWindow(hwndedit2), "Expected Edit window to be created\n");
2996
2997     /* creating label disabled when control isn't focused */
2998     SetFocus(0);
2999     hwndedit = (HWND)SendMessage(hwnd, LVM_EDITLABEL, 0, 0);
3000     todo_wine ok(hwndedit == NULL, "Expected Edit window not to be created\n");
3001
3002     /* check EN_KILLFOCUS handling */
3003     memset(&item, 0, sizeof(item));
3004     item.pszText = testitemA;
3005     item.iItem = 0;
3006     item.iSubItem = 0;
3007     r = SendMessage(hwnd, LVM_SETITEMTEXTA, 0, (LPARAM)&item);
3008     expect(TRUE, r);
3009
3010     SetFocus(hwnd);
3011     hwndedit = (HWND)SendMessage(hwnd, LVM_EDITLABEL, 0, 0);
3012     ok(IsWindow(hwndedit), "Expected Edit window to be created\n");
3013     /* modify edit and notify control that it lost focus */
3014     r = SendMessage(hwndedit, WM_SETTEXT, 0, (LPARAM)testitem1A);
3015     expect(TRUE, r);
3016     r = SendMessage(hwnd, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)hwndedit);
3017     expect(0, r);
3018     memset(&item, 0, sizeof(item));
3019     item.pszText = buffer;
3020     item.cchTextMax = 10;
3021     item.iItem = 0;
3022     item.iSubItem = 0;
3023     r = SendMessage(hwnd, LVM_GETITEMTEXTA, 0, (LPARAM)&item);
3024     expect(lstrlen(item.pszText), r);
3025     ok(strcmp(buffer, testitem1A) == 0, "Expected item text to change\n");
3026     /* end edit without saving */
3027     r = SendMessage(hwndedit, WM_KEYDOWN, VK_ESCAPE, 0);
3028     expect(0, r);
3029     memset(&item, 0, sizeof(item));
3030     item.pszText = buffer;
3031     item.cchTextMax = 10;
3032     item.iItem = 0;
3033     item.iSubItem = 0;
3034     r = SendMessage(hwnd, LVM_GETITEMTEXTA, 0, (LPARAM)&item);
3035     expect(lstrlen(item.pszText), r);
3036     ok(strcmp(buffer, testitem1A) == 0, "Expected item text to change\n");
3037
3038     /* LVM_EDITLABEL with -1 destroys current edit */
3039     hwndedit = (HWND)SendMessage(hwnd, LVM_GETEDITCONTROL, 0, 0);
3040     ok(hwndedit == NULL, "Expected Edit window not to be created\n");
3041     /* no edit present */
3042     hwndedit = (HWND)SendMessage(hwnd, LVM_EDITLABEL, -1, 0);
3043     ok(hwndedit == NULL, "Expected Edit window not to be created\n");
3044     hwndedit = (HWND)SendMessage(hwnd, LVM_EDITLABEL, 0, 0);
3045     ok(IsWindow(hwndedit), "Expected Edit window to be created\n");
3046     /* edit present */
3047     ok(GetFocus() == hwndedit, "Expected Edit to be focused\n");
3048     hwndedit2 = (HWND)SendMessage(hwnd, LVM_EDITLABEL, -1, 0);
3049     ok(hwndedit2 == NULL, "Expected Edit window not to be created\n");
3050     ok(!IsWindow(hwndedit), "Expected Edit window to be destroyed\n");
3051     ok(GetFocus() == hwnd, "Expected List to be focused\n");
3052     /* check another negative value */
3053     hwndedit = (HWND)SendMessage(hwnd, LVM_EDITLABEL, 0, 0);
3054     ok(IsWindow(hwndedit), "Expected Edit window to be created\n");
3055     ok(GetFocus() == hwndedit, "Expected Edit to be focused\n");
3056     hwndedit2 = (HWND)SendMessage(hwnd, LVM_EDITLABEL, -2, 0);
3057     ok(hwndedit2 == NULL, "Expected Edit window not to be created\n");
3058     ok(!IsWindow(hwndedit), "Expected Edit window to be destroyed\n");
3059     ok(GetFocus() == hwnd, "Expected List to be focused\n");
3060     /* and value greater then max item index */
3061     hwndedit = (HWND)SendMessage(hwnd, LVM_EDITLABEL, 0, 0);
3062     ok(IsWindow(hwndedit), "Expected Edit window to be created\n");
3063     ok(GetFocus() == hwndedit, "Expected Edit to be focused\n");
3064     r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
3065     hwndedit2 = (HWND)SendMessage(hwnd, LVM_EDITLABEL, r, 0);
3066     ok(hwndedit2 == NULL, "Expected Edit window not to be created\n");
3067     ok(!IsWindow(hwndedit), "Expected Edit window to be destroyed\n");
3068     ok(GetFocus() == hwnd, "Expected List to be focused\n");
3069
3070     /* messaging tests */
3071     SetFocus(hwnd);
3072     flush_sequences(sequences, NUM_MSG_SEQUENCES);
3073     blockEdit = FALSE;
3074     hwndedit = (HWND)SendMessage(hwnd, LVM_EDITLABEL, 0, 0);
3075     ok(IsWindow(hwndedit), "Expected Edit window to be created\n");
3076     /* testing only sizing messages */
3077     ok_sequence(sequences, EDITBOX_SEQ_INDEX, editbox_create_pos,
3078                 "edit box create - sizing", TRUE);
3079
3080     DestroyWindow(hwnd);
3081 }
3082
3083 START_TEST(listview)
3084 {
3085     HMODULE hComctl32;
3086     BOOL (WINAPI *pInitCommonControlsEx)(const INITCOMMONCONTROLSEX*);
3087
3088     hComctl32 = GetModuleHandleA("comctl32.dll");
3089     pInitCommonControlsEx = (void*)GetProcAddress(hComctl32, "InitCommonControlsEx");
3090     if (pInitCommonControlsEx)
3091     {
3092         INITCOMMONCONTROLSEX iccex;
3093         iccex.dwSize = sizeof(iccex);
3094         iccex.dwICC  = ICC_LISTVIEW_CLASSES;
3095         pInitCommonControlsEx(&iccex);
3096     }
3097     else
3098         InitCommonControls();
3099
3100     init_msg_sequences(sequences, NUM_MSG_SEQUENCES);
3101
3102     flush_sequences(sequences, NUM_MSG_SEQUENCES);
3103     hwndparent = create_parent_window();
3104     ok_sequence(sequences, PARENT_SEQ_INDEX, create_parent_wnd_seq, "create parent window", TRUE);
3105     flush_sequences(sequences, NUM_MSG_SEQUENCES);
3106
3107     test_images();
3108     test_checkboxes();
3109     test_items();
3110     test_create();
3111     test_redraw();
3112     test_customdraw();
3113     test_icon_spacing();
3114     test_color();
3115     test_item_count();
3116     test_item_position();
3117     test_columns();
3118     test_getorigin();
3119     test_multiselect();
3120     test_getitemrect();
3121     test_subitem_rect();
3122     test_sorting();
3123     test_ownerdata();
3124     test_norecompute();
3125     test_nosortheader();
3126     test_setredraw();
3127     test_hittest();
3128     test_getviewrect();
3129     test_getitemposition();
3130     test_columnscreation();
3131     test_editbox();
3132
3133     DestroyWindow(hwndparent);
3134 }