d3drm: Implement IDirect3DRMMesh_AddGroup.
[wine] / dlls / comctl32 / tests / treeview.c
1 /* Unit tests for treeview.
2  *
3  * Copyright 2005 Krzysztof Foltman
4  * Copyright 2007 Christopher James Peterson
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <assert.h>
22 #include <stdarg.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "winuser.h"
28 #include "winnls.h"
29 #include "winreg.h"
30 #include "commctrl.h" 
31
32 #include "wine/test.h"
33 #include "v6util.h"
34 #include "msg.h"
35
36 static const char *TEST_CALLBACK_TEXT = "callback_text";
37
38 static TVITEMA g_item_expanding, g_item_expanded;
39 static BOOL g_get_from_expand;
40 static BOOL g_get_rect_in_expand;
41 static BOOL g_disp_A_to_W;
42 static BOOL g_disp_set_stateimage;
43 static BOOL g_beginedit_alter_text;
44
45 #define NUM_MSG_SEQUENCES   2
46 #define TREEVIEW_SEQ_INDEX  0
47 #define PARENT_SEQ_INDEX    1
48
49 #define expect(expected, got) ok(got == expected, "Expected %d, got %d\n", expected, got)
50
51 static struct msg_sequence *sequences[NUM_MSG_SEQUENCES];
52 static struct msg_sequence *item_sequence[1];
53
54 static const struct message FillRootSeq[] = {
55     { TVM_INSERTITEM, sent },
56     { TVM_INSERTITEM, sent },
57     { 0 }
58 };
59
60 static const struct message rootnone_select_seq[] = {
61     { TVM_SELECTITEM, sent|wparam, 9 },
62     { TVM_SELECTITEM, sent|wparam, 9 },
63     { TVM_SELECTITEM, sent|wparam, 9 },
64     { TVM_SELECTITEM, sent|wparam, 9 },
65     { TVM_SELECTITEM, sent|wparam, 9 },
66     { TVM_SELECTITEM, sent|wparam, 9 },
67     { 0 }
68 };
69
70 static const struct message rootchild_select_seq[] = {
71     { TVM_SELECTITEM, sent|wparam, 9 },
72     { TVM_SELECTITEM, sent|wparam, 9 },
73     { TVM_SELECTITEM, sent|wparam, 9 },
74     { TVM_SELECTITEM, sent|wparam, 9 },
75     { TVM_SELECTITEM, sent|wparam, 9 },
76     { TVM_SELECTITEM, sent|wparam, 9 },
77     { 0 }
78 };
79
80 static const struct message getitemtext_seq[] = {
81     { TVM_INSERTITEMA, sent },
82     { TVM_GETITEMA, sent },
83     { TVM_DELETEITEM, sent },
84     { 0 }
85 };
86
87 static const struct message focus_seq[] = {
88     { TVM_INSERTITEM, sent },
89     { TVM_INSERTITEM, sent },
90     { TVM_SELECTITEM, sent|wparam, 9 },
91     /* The following end up out of order in wine */
92     { WM_WINDOWPOSCHANGING, sent|defwinproc },
93     { WM_NCCALCSIZE, sent|wparam|defwinproc, TRUE },
94     { WM_WINDOWPOSCHANGED, sent|defwinproc },
95     { WM_SIZE, sent|defwinproc },
96     { WM_WINDOWPOSCHANGING, sent|defwinproc|optional },
97     { WM_NCCALCSIZE, sent|wparam|defwinproc|optional, TRUE },
98     { WM_WINDOWPOSCHANGED, sent|defwinproc|optional },
99     { WM_SIZE, sent|defwinproc|optional },
100     { WM_PAINT, sent|defwinproc },
101     { WM_NCPAINT, sent|wparam|defwinproc, 1 },
102     { WM_ERASEBKGND, sent|defwinproc },
103     { TVM_EDITLABELA, sent },
104     { WM_COMMAND, sent|wparam|defwinproc, MAKEWPARAM(0, EN_UPDATE) },
105     { WM_COMMAND, sent|wparam|defwinproc, MAKEWPARAM(0, EN_CHANGE) },
106     { WM_PARENTNOTIFY, sent|wparam|defwinproc, MAKEWPARAM(WM_CREATE, 0) },
107     { WM_KILLFOCUS, sent|defwinproc },
108     { WM_PAINT, sent|defwinproc },
109     { WM_IME_SETCONTEXT, sent|defwinproc|optional },
110     { WM_COMMAND, sent|wparam|defwinproc, MAKEWPARAM(0, EN_SETFOCUS) },
111     { WM_ERASEBKGND, sent|defwinproc|optional },
112     { WM_CTLCOLOREDIT, sent|defwinproc|optional },
113     { WM_CTLCOLOREDIT, sent|defwinproc|optional },
114     { 0 }
115 };
116
117 static const struct message test_get_set_bkcolor_seq[] = {
118     { TVM_GETBKCOLOR, sent|wparam|lparam, 0, 0 },
119     { TVM_SETBKCOLOR, sent|wparam|lparam, 0, 0 },
120     { TVM_GETBKCOLOR, sent|wparam|lparam, 0, 0 },
121     { TVM_SETBKCOLOR, sent|wparam|lparam, 0, 0x00ffffff },
122     { TVM_GETBKCOLOR, sent|wparam|lparam, 0, 0 },
123     { TVM_SETBKCOLOR, sent|wparam|lparam, 0, -1 },
124     { 0 }
125 };
126
127 static const struct message test_get_set_imagelist_seq[] = {
128     { TVM_SETIMAGELIST, sent|wparam|lparam, 0, 0 },
129     { TVM_GETIMAGELIST, sent|wparam|lparam, 0, 0 },
130     { 0 }
131 };
132
133 static const struct message test_get_set_indent_seq[] = {
134     { TVM_SETINDENT, sent|wparam|lparam, 0, 0 },
135     { TVM_GETINDENT, sent|wparam|lparam, 0, 0 },
136     /* The actual amount to indent is dependent on the system for this message */
137     { TVM_SETINDENT, sent },
138     { TVM_GETINDENT, sent|wparam|lparam, 0, 0 },
139     { 0 }
140 };
141
142 static const struct message test_get_set_insertmarkcolor_seq[] = {
143     { TVM_SETINSERTMARKCOLOR, sent|wparam|lparam, 0, 0 },
144     { TVM_GETINSERTMARKCOLOR, sent|wparam|lparam, 0, 0 },
145     { 0 }
146 };
147
148 static const struct message test_get_set_item_seq[] = {
149     { TVM_GETITEMA, sent },
150     { TVM_SETITEMA, sent },
151     { TVM_GETITEMA, sent },
152     { TVM_SETITEMA, sent },
153     { 0 }
154 };
155
156 static const struct message test_get_set_itemheight_seq[] = {
157     { TVM_GETITEMHEIGHT, sent|wparam|lparam, 0, 0 },
158     { TVM_SETITEMHEIGHT, sent|wparam|lparam, -1, 0 },
159     { TVM_GETITEMHEIGHT, sent|wparam|lparam, 0, 0 },
160     { TVM_SETITEMHEIGHT, sent|lparam, 0xcccccccc, 0 },
161     { TVM_GETITEMHEIGHT, sent|wparam|lparam|optional, 0, 0 },
162     { TVM_SETITEMHEIGHT, sent|wparam|lparam|optional, 9, 0 },
163     { TVM_GETITEMHEIGHT, sent|wparam|lparam, 0, 0 },
164     { 0 }
165 };
166
167 static const struct message test_get_set_scrolltime_seq[] = {
168     { TVM_SETSCROLLTIME, sent|wparam|lparam, 20, 0 },
169     { TVM_GETSCROLLTIME, sent|wparam|lparam, 0, 0 },
170     { 0 }
171 };
172
173 static const struct message test_get_set_textcolor_seq[] = {
174     { TVM_GETTEXTCOLOR, sent|wparam|lparam, 0, 0 },
175     { TVM_SETTEXTCOLOR, sent|wparam|lparam, 0, 0 },
176     { TVM_GETTEXTCOLOR, sent|wparam|lparam, 0, 0 },
177     { TVM_SETTEXTCOLOR, sent|wparam|lparam, 0, RGB(255, 255, 255) },
178     { TVM_GETTEXTCOLOR, sent|wparam|lparam, 0, 0 },
179     { TVM_SETTEXTCOLOR, sent|wparam|lparam, 0, CLR_NONE },
180     { 0 }
181 };
182
183 static const struct message test_get_set_tooltips_seq[] = {
184     { WM_KILLFOCUS,    sent },
185     { WM_IME_SETCONTEXT, sent|optional },
186     { WM_IME_NOTIFY, sent|optional },
187     { TVM_SETTOOLTIPS, sent|wparam|lparam, 0, 0 },
188     { TVM_GETTOOLTIPS, sent|wparam|lparam, 0, 0 },
189     { 0 }
190 };
191
192 static const struct message test_get_set_unicodeformat_seq[] = {
193     { TVM_SETUNICODEFORMAT, sent|wparam|lparam, TRUE, 0 },
194     { TVM_GETUNICODEFORMAT, sent|wparam|lparam, 0, 0 },
195     { TVM_SETUNICODEFORMAT, sent|wparam|lparam, 0, 0 },
196     { TVM_GETUNICODEFORMAT, sent|wparam|lparam, 0, 0 },
197     { TVM_SETUNICODEFORMAT, sent|wparam|lparam, 0, 0 },
198     { 0 }
199 };
200
201 static const struct message parent_expand_seq[] = {
202     { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDINGA },
203     { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDEDA },
204     { 0 }
205 };
206
207 static const struct message parent_expand_kb_seq[] = {
208     { WM_NOTIFY, sent|id|optional, 0, 0, TVN_KEYDOWN },
209     { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDINGA },
210     { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDEDA },
211     { WM_CHANGEUISTATE, sent|optional },
212     { 0 }
213 };
214
215 static const struct message parent_expand_empty_kb_seq[] = {
216     { WM_NOTIFY, sent|id|optional, 0, 0, TVN_KEYDOWN },
217     { WM_CHANGEUISTATE, sent|optional },
218     { 0 }
219 };
220
221 static const struct message parent_singleexpand_seq[] = {
222     { WM_NOTIFY, sent|id, 0, 0, TVN_SELCHANGINGA },
223     { WM_NOTIFY, sent|id, 0, 0, TVN_SELCHANGEDA },
224     { WM_NOTIFY, sent|id, 0, 0, TVN_SINGLEEXPAND },
225     { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDINGA },
226     { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDEDA },
227     { 0 }
228 };
229
230 static const struct message parent_get_dispinfo_seq[] = {
231     { WM_NOTIFY, sent|id, 0, 0, TVN_GETDISPINFOA },
232     { 0 }
233 };
234
235 static const struct message empty_seq[] = {
236     { 0 }
237 };
238
239 static HWND hMainWnd;
240
241 static HTREEITEM hRoot, hChild;
242
243 static int pos = 0;
244 static char sequence[256];
245
246 static void Clear(void)
247 {
248     pos = 0;
249     sequence[0] = '\0';
250 }
251
252 static void AddItem(char ch)
253 {
254     sequence[pos++] = ch;
255     sequence[pos] = '\0';
256 }
257
258 static void IdentifyItem(HTREEITEM hItem)
259 {
260     if (hItem == hRoot) {
261         AddItem('R');
262         return;
263     }
264     if (hItem == hChild) {
265         AddItem('C');
266         return;
267     }
268     if (hItem == NULL) {
269         AddItem('n');
270         return;
271     }
272     AddItem('?');
273 }
274
275 /* This function hooks in and records all messages to the treeview control */
276 static LRESULT WINAPI TreeviewWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
277 {
278     static LONG defwndproc_counter = 0;
279     LRESULT ret;
280     struct message msg;
281     WNDPROC lpOldProc = (WNDPROC)GetWindowLongPtrA(hwnd, GWLP_USERDATA);
282
283     msg.message = message;
284     msg.flags = sent|wparam|lparam;
285     if (defwndproc_counter) msg.flags |= defwinproc;
286     msg.wParam = wParam;
287     msg.lParam = lParam;
288     add_message(sequences, TREEVIEW_SEQ_INDEX, &msg);
289
290     defwndproc_counter++;
291     ret = CallWindowProcA(lpOldProc, hwnd, message, wParam, lParam);
292     defwndproc_counter--;
293
294     return ret;
295 }
296
297 static HWND create_treeview_control(DWORD style)
298 {
299     WNDPROC pOldWndProc;
300     HWND hTree;
301
302     hTree = CreateWindowExA(WS_EX_CLIENTEDGE, WC_TREEVIEWA, NULL, WS_CHILD|WS_VISIBLE|
303             TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS|TVS_EDITLABELS|style,
304             0, 0, 120, 100, hMainWnd, (HMENU)100, GetModuleHandleA(0), 0);
305
306     SetFocus(hTree);
307
308     /* Record the old WNDPROC so we can call it after recording the messages */
309     pOldWndProc = (WNDPROC)SetWindowLongPtrA(hTree, GWLP_WNDPROC, (LONG_PTR)TreeviewWndProc);
310     SetWindowLongPtrA(hTree, GWLP_USERDATA, (LONG_PTR)pOldWndProc);
311
312     return hTree;
313 }
314
315 static void fill_tree(HWND hTree)
316 {
317     TVINSERTSTRUCTA ins;
318     static CHAR root[]  = "Root",
319                 child[] = "Child";
320
321     ins.hParent = TVI_ROOT;
322     ins.hInsertAfter = TVI_ROOT;
323     U(ins).item.mask = TVIF_TEXT;
324     U(ins).item.pszText = root;
325     hRoot = TreeView_InsertItem(hTree, &ins);
326
327     ins.hParent = hRoot;
328     ins.hInsertAfter = TVI_FIRST;
329     U(ins).item.mask = TVIF_TEXT;
330     U(ins).item.pszText = child;
331     hChild = TreeView_InsertItem(hTree, &ins);
332 }
333
334 static void test_fillroot(void)
335 {
336     TVITEMA tvi;
337     HWND hTree;
338
339     hTree = create_treeview_control(0);
340
341     flush_sequences(sequences, NUM_MSG_SEQUENCES);
342
343     fill_tree(hTree);
344
345     Clear();
346     AddItem('A');
347     assert(hRoot);
348     AddItem('B');
349     assert(hChild);
350     AddItem('.');
351     ok_sequence(sequences, TREEVIEW_SEQ_INDEX, FillRootSeq, "FillRoot", FALSE);
352     ok(!strcmp(sequence, "AB."), "Item creation\n");
353
354     /* UMLPad 1.15 depends on this being not -1 (I_IMAGECALLBACK) */
355     tvi.hItem = hRoot;
356     tvi.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE;
357     SendMessage( hTree, TVM_GETITEMA, 0, (LPARAM)&tvi );
358     ok(tvi.iImage == 0, "tvi.iImage=%d\n", tvi.iImage);
359     ok(tvi.iSelectedImage == 0, "tvi.iSelectedImage=%d\n", tvi.iSelectedImage);
360
361     DestroyWindow(hTree);
362 }
363
364 static void test_callback(void)
365 {
366     HTREEITEM hRoot;
367     HTREEITEM hItem1, hItem2;
368     TVINSERTSTRUCTA ins;
369     TVITEMA tvi;
370     CHAR test_string[] = "Test_string";
371     static const CHAR test2A[] = "TEST2";
372     CHAR buf[128];
373     HWND hTree;
374     DWORD ret;
375
376     hTree = create_treeview_control(0);
377
378     ret = TreeView_DeleteAllItems(hTree);
379     expect(TRUE, ret);
380     ins.hParent = TVI_ROOT;
381     ins.hInsertAfter = TVI_ROOT;
382     U(ins).item.mask = TVIF_TEXT;
383     U(ins).item.pszText = LPSTR_TEXTCALLBACK;
384     hRoot = TreeView_InsertItem(hTree, &ins);
385     assert(hRoot);
386
387     tvi.hItem = hRoot;
388     tvi.mask = TVIF_TEXT;
389     tvi.pszText = buf;
390     tvi.cchTextMax = sizeof(buf)/sizeof(buf[0]);
391     ret = TreeView_GetItem(hTree, &tvi);
392     expect(TRUE, ret);
393     ok(strcmp(tvi.pszText, TEST_CALLBACK_TEXT) == 0, "Callback item text mismatch %s vs %s\n",
394         tvi.pszText, TEST_CALLBACK_TEXT);
395
396     ins.hParent = hRoot;
397     ins.hInsertAfter = TVI_FIRST;
398     U(ins).item.mask = TVIF_TEXT;
399     U(ins).item.pszText = test_string;
400     hItem1 = TreeView_InsertItem(hTree, &ins);
401     assert(hItem1);
402
403     tvi.hItem = hItem1;
404     ret = TreeView_GetItem(hTree, &tvi);
405     expect(TRUE, ret);
406     ok(strcmp(tvi.pszText, test_string) == 0, "Item text mismatch %s vs %s\n",
407         tvi.pszText, test_string);
408
409     /* undocumented: pszText of NULL also means LPSTR_CALLBACK: */
410     tvi.pszText = NULL;
411     ret = TreeView_SetItem(hTree, &tvi);
412     expect(TRUE, ret);
413     tvi.pszText = buf;
414     ret = TreeView_GetItem(hTree, &tvi);
415     expect(TRUE, ret);
416     ok(strcmp(tvi.pszText, TEST_CALLBACK_TEXT) == 0, "Item text mismatch %s vs %s\n",
417         tvi.pszText, TEST_CALLBACK_TEXT);
418
419     U(ins).item.pszText = NULL;
420     hItem2 = TreeView_InsertItem(hTree, &ins);
421     assert(hItem2);
422     tvi.hItem = hItem2;
423     memset(buf, 0, sizeof(buf));
424     ret = TreeView_GetItem(hTree, &tvi);
425     expect(TRUE, ret);
426     ok(strcmp(tvi.pszText, TEST_CALLBACK_TEXT) == 0, "Item text mismatch %s vs %s\n",
427         tvi.pszText, TEST_CALLBACK_TEXT);
428
429     /* notification handler changed A->W */
430     g_disp_A_to_W = TRUE;
431     tvi.hItem = hItem2;
432     memset(buf, 0, sizeof(buf));
433     ret = TreeView_GetItem(hTree, &tvi);
434     expect(TRUE, ret);
435     ok(strcmp(tvi.pszText, test2A) == 0, "got %s, expected %s\n",
436         tvi.pszText, test2A);
437     g_disp_A_to_W = FALSE;
438
439     /* handler changes state image index */
440     SetWindowLongA(hTree, GWL_STYLE, GetWindowLongA(hTree, GWL_STYLE) | TVS_CHECKBOXES);
441
442     /* clear selection, handler will set selected state */
443     ret = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, 0);
444     expect(TRUE, ret);
445
446     flush_sequences(sequences, NUM_MSG_SEQUENCES);
447
448     tvi.hItem = hRoot;
449     tvi.mask = TVIF_STATE;
450     tvi.state = TVIS_SELECTED;
451     ret = TreeView_GetItem(hTree, &tvi);
452     expect(TRUE, ret);
453     ok(tvi.state == INDEXTOSTATEIMAGEMASK(1), "got 0x%x\n", tvi.state);
454
455     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq,
456                 "no TVN_GETDISPINFO for a state seq", FALSE);
457
458     tvi.hItem     = hRoot;
459     tvi.mask      = TVIF_IMAGE | TVIF_STATE;
460     tvi.state     = TVIS_FOCUSED;
461     tvi.stateMask = TVIS_FOCUSED;
462     tvi.iImage    = I_IMAGECALLBACK;
463     ret = TreeView_SetItem(hTree, &tvi);
464     expect(TRUE, ret);
465
466     /* ask for item image index through callback - state is also set with state image index */
467     flush_sequences(sequences, NUM_MSG_SEQUENCES);
468
469     tvi.hItem = hRoot;
470     tvi.mask = TVIF_IMAGE;
471     tvi.state = 0;
472     ret = TreeView_GetItem(hTree, &tvi);
473     expect(TRUE, ret);
474     ok(tvi.state == (INDEXTOSTATEIMAGEMASK(1) | TVIS_FOCUSED), "got 0x%x\n", tvi.state);
475
476     ok_sequence(sequences, PARENT_SEQ_INDEX, parent_get_dispinfo_seq,
477                 "callback for state/overlay image index, noop seq", FALSE);
478
479     /* ask for image again and overwrite state to some value in handler */
480     flush_sequences(sequences, NUM_MSG_SEQUENCES);
481
482     g_disp_set_stateimage = TRUE;
483     tvi.hItem = hRoot;
484     tvi.mask = TVIF_IMAGE;
485     tvi.state = INDEXTOSTATEIMAGEMASK(1);
486     tvi.stateMask = 0;
487     ret = TreeView_GetItem(hTree, &tvi);
488     expect(TRUE, ret);
489     /* handler sets TVIS_SELECTED as well */
490     ok(tvi.state == (TVIS_FOCUSED | TVIS_SELECTED | INDEXTOSTATEIMAGEMASK(2) | INDEXTOOVERLAYMASK(3)), "got 0x%x\n", tvi.state);
491     g_disp_set_stateimage = FALSE;
492
493     ok_sequence(sequences, PARENT_SEQ_INDEX, parent_get_dispinfo_seq,
494                 "callback for state/overlay image index seq", FALSE);
495
496     DestroyWindow(hTree);
497 }
498
499 static void test_select(void)
500 {
501     BOOL r;
502     HWND hTree;
503
504     hTree = create_treeview_control(0);
505     fill_tree(hTree);
506
507     /* root-none select tests */
508     flush_sequences(sequences, NUM_MSG_SEQUENCES);
509     r = TreeView_SelectItem(hTree, NULL);
510     expect(TRUE, r);
511     Clear();
512     AddItem('1');
513     r = TreeView_SelectItem(hTree, hRoot);
514     expect(TRUE, r);
515     AddItem('2');
516     r = TreeView_SelectItem(hTree, hRoot);
517     expect(TRUE, r);
518     AddItem('3');
519     r = TreeView_SelectItem(hTree, NULL);
520     expect(TRUE, r);
521     AddItem('4');
522     r = TreeView_SelectItem(hTree, NULL);
523     expect(TRUE, r);
524     AddItem('5');
525     r = TreeView_SelectItem(hTree, hRoot);
526     expect(TRUE, r);
527     AddItem('.');
528     ok(!strcmp(sequence, "1(nR)nR23(Rn)Rn45(nR)nR."), "root-none select test\n");
529     ok_sequence(sequences, TREEVIEW_SEQ_INDEX, rootnone_select_seq,
530                 "root-none select seq", FALSE);
531
532     /* root-child select tests */
533     flush_sequences(sequences, NUM_MSG_SEQUENCES);
534     r = TreeView_SelectItem(hTree, NULL);
535     expect(TRUE, r);
536
537     Clear();
538     AddItem('1');
539     r = TreeView_SelectItem(hTree, hRoot);
540     expect(TRUE, r);
541     AddItem('2');
542     r = TreeView_SelectItem(hTree, hRoot);
543     expect(TRUE, r);
544     AddItem('3');
545     r = TreeView_SelectItem(hTree, hChild);
546     expect(TRUE, r);
547     AddItem('4');
548     r = TreeView_SelectItem(hTree, hChild);
549     expect(TRUE, r);
550     AddItem('5');
551     r = TreeView_SelectItem(hTree, hRoot);
552     expect(TRUE, r);
553     AddItem('.');
554     ok(!strcmp(sequence, "1(nR)nR23(RC)RC45(CR)CR."), "root-child select test\n");
555     ok_sequence(sequences, TREEVIEW_SEQ_INDEX, rootchild_select_seq,
556                 "root-child select seq", FALSE);
557
558     DestroyWindow(hTree);
559 }
560
561 static void test_getitemtext(void)
562 {
563     TVINSERTSTRUCTA ins;
564     HTREEITEM hChild;
565     TVITEMA tvi;
566     HWND hTree;
567
568     CHAR szBuffer[80] = "Blah";
569     int nBufferSize = sizeof(szBuffer)/sizeof(CHAR);
570
571     hTree = create_treeview_control(0);
572     fill_tree(hTree);
573
574     flush_sequences(sequences, NUM_MSG_SEQUENCES);
575
576     /* add an item without TVIF_TEXT mask and pszText == NULL */
577     ins.hParent = hRoot;
578     ins.hInsertAfter = TVI_ROOT;
579     U(ins).item.mask = 0;
580     U(ins).item.pszText = NULL;
581     U(ins).item.cchTextMax = 0;
582     hChild = TreeView_InsertItem(hTree, &ins);
583     assert(hChild);
584
585     /* retrieve it with TVIF_TEXT mask */
586     tvi.hItem = hChild;
587     tvi.mask = TVIF_TEXT;
588     tvi.cchTextMax = nBufferSize;
589     tvi.pszText = szBuffer;
590
591     SendMessageA( hTree, TVM_GETITEMA, 0, (LPARAM)&tvi );
592     ok(!strcmp(szBuffer, ""), "szBuffer=\"%s\", expected \"\"\n", szBuffer);
593     ok(SendMessageA(hTree, TVM_DELETEITEM, 0, (LPARAM)hChild), "DeleteItem failed\n");
594     ok_sequence(sequences, TREEVIEW_SEQ_INDEX, getitemtext_seq, "get item text seq", FALSE);
595
596     DestroyWindow(hTree);
597 }
598
599 static void test_focus(void)
600 {
601     TVINSERTSTRUCTA ins;
602     static CHAR child1[]  = "Edit",
603                 child2[]  = "A really long string";
604     HTREEITEM hChild1, hChild2;
605     HWND hTree;
606     HWND hEdit;
607
608     hTree = create_treeview_control(0);
609     fill_tree(hTree);
610
611     flush_sequences(sequences, NUM_MSG_SEQUENCES);
612
613     /* This test verifies that when a label is being edited, scrolling
614      * the treeview does not cause the label to lose focus. To test
615      * this, first some additional entries are added to generate
616      * scrollbars.
617      */
618     ins.hParent = hRoot;
619     ins.hInsertAfter = hChild;
620     U(ins).item.mask = TVIF_TEXT;
621     U(ins).item.pszText = child1;
622     hChild1 = TreeView_InsertItem(hTree, &ins);
623     assert(hChild1);
624     ins.hInsertAfter = hChild1;
625     U(ins).item.mask = TVIF_TEXT;
626     U(ins).item.pszText = child2;
627     hChild2 = TreeView_InsertItem(hTree, &ins);
628     assert(hChild2);
629
630     ShowWindow(hMainWnd,SW_SHOW);
631     SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hChild);
632     hEdit = TreeView_EditLabel(hTree, hChild);
633     ScrollWindowEx(hTree, -10, 0, NULL, NULL, NULL, NULL, SW_SCROLLCHILDREN);
634     ok(GetFocus() == hEdit, "Edit control should have focus\n");
635     ok_sequence(sequences, TREEVIEW_SEQ_INDEX, focus_seq, "focus test", TRUE);
636
637     DestroyWindow(hTree);
638 }
639
640 static void test_get_set_bkcolor(void)
641 {
642     COLORREF crColor = RGB(0,0,0);
643     HWND hTree;
644
645     hTree = create_treeview_control(0);
646     fill_tree(hTree);
647
648     flush_sequences(sequences, NUM_MSG_SEQUENCES);
649
650     /* If the value is -1, the control is using the system color for the background color. */
651     crColor = (COLORREF)SendMessage( hTree, TVM_GETBKCOLOR, 0, 0 );
652     ok(crColor == ~0u, "Default background color reported as 0x%.8x\n", crColor);
653
654     /* Test for black background */
655     SendMessage( hTree, TVM_SETBKCOLOR, 0, RGB(0,0,0) );
656     crColor = (COLORREF)SendMessage( hTree, TVM_GETBKCOLOR, 0, 0 );
657     ok(crColor == RGB(0,0,0), "Black background color reported as 0x%.8x\n", crColor);
658
659     /* Test for white background */
660     SendMessage( hTree, TVM_SETBKCOLOR, 0, RGB(255,255,255) );
661     crColor = (COLORREF)SendMessage( hTree, TVM_GETBKCOLOR, 0, 0 );
662     ok(crColor == RGB(255,255,255), "White background color reported as 0x%.8x\n", crColor);
663
664     /* Reset the default background */
665     SendMessage( hTree, TVM_SETBKCOLOR, 0, -1 );
666
667     ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_bkcolor_seq,
668         "test get set bkcolor", FALSE);
669
670     DestroyWindow(hTree);
671 }
672
673 static void test_get_set_imagelist(void)
674 {
675     HIMAGELIST himl;
676     HWND hTree;
677
678     hTree = create_treeview_control(0);
679     fill_tree(hTree);
680
681     flush_sequences(sequences, NUM_MSG_SEQUENCES);
682
683     /* Test a NULL HIMAGELIST */
684     SendMessage( hTree, TVM_SETIMAGELIST, TVSIL_NORMAL, 0 );
685     himl = (HIMAGELIST)SendMessage( hTree, TVM_GETIMAGELIST, TVSIL_NORMAL, 0 );
686     ok(himl == NULL, "NULL image list, reported as %p, expected 0.\n", himl);
687
688     /* TODO: Test an actual image list */
689
690     ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_imagelist_seq,
691         "test get imagelist", FALSE);
692
693     DestroyWindow(hTree);
694 }
695
696 static void test_get_set_indent(void)
697 {
698     int ulIndent = -1;
699     int ulMinIndent = -1;
700     int ulMoreThanTwiceMin = -1;
701     HWND hTree;
702
703     hTree = create_treeview_control(0);
704     fill_tree(hTree);
705
706     flush_sequences(sequences, NUM_MSG_SEQUENCES);
707
708     /* Finding the minimum indent */
709     SendMessage( hTree, TVM_SETINDENT, 0, 0 );
710     ulMinIndent = (int)SendMessage( hTree, TVM_GETINDENT, 0, 0 );
711
712     /* Checking an indent that is more than twice the default indent */
713     ulMoreThanTwiceMin = 2*ulMinIndent+1;
714     SendMessage( hTree, TVM_SETINDENT, ulMoreThanTwiceMin, 0 );
715     ulIndent = (DWORD)SendMessage( hTree, TVM_GETINDENT, 0, 0 );
716     ok(ulIndent == ulMoreThanTwiceMin, "Indent reported as %d, expected %d\n", ulIndent, ulMoreThanTwiceMin);
717
718     ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_indent_seq,
719         "test get set indent", FALSE);
720
721     DestroyWindow(hTree);
722 }
723
724 static void test_get_set_insertmark(void)
725 {
726     COLORREF crColor = RGB(0,0,0);
727     HWND hTree;
728
729     hTree = create_treeview_control(0);
730     fill_tree(hTree);
731
732     flush_sequences(sequences, NUM_MSG_SEQUENCES);
733
734     SendMessage( hTree, TVM_SETINSERTMARKCOLOR, 0, crColor );
735     crColor = (COLORREF)SendMessage( hTree, TVM_GETINSERTMARKCOLOR, 0, 0 );
736     ok(crColor == RGB(0,0,0), "Insert mark color reported as 0x%.8x, expected 0x00000000\n", crColor);
737
738     ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_insertmarkcolor_seq,
739         "test get set insertmark color", FALSE);
740
741     DestroyWindow(hTree);
742 }
743
744 static void test_get_set_item(void)
745 {
746     TVITEMA tviRoot = {0};
747     int nBufferSize = 80;
748     char szBuffer[80] = {0};
749     DWORD ret;
750     HWND hTree;
751
752     hTree = create_treeview_control(0);
753     fill_tree(hTree);
754
755     tviRoot.hItem = hRoot;
756     tviRoot.mask  = TVIF_STATE;
757     tviRoot.state = TVIS_FOCUSED;
758     tviRoot.stateMask = TVIS_FOCUSED;
759     ret = SendMessage( hTree, TVM_SETITEMA, 0, (LPARAM)&tviRoot );
760     expect(TRUE, ret);
761
762     flush_sequences(sequences, NUM_MSG_SEQUENCES);
763
764     /* Test the root item, state is set even when not requested */
765     tviRoot.hItem = hRoot;
766     tviRoot.mask = TVIF_TEXT;
767     tviRoot.state = 0;
768     tviRoot.stateMask = 0;
769     tviRoot.cchTextMax = nBufferSize;
770     tviRoot.pszText = szBuffer;
771     ret = SendMessage( hTree, TVM_GETITEMA, 0, (LPARAM)&tviRoot );
772     expect(TRUE, ret);
773     ok(!strcmp("Root", szBuffer), "GetItem: szBuffer=\"%s\", expected \"Root\"\n", szBuffer);
774     ok(tviRoot.state == TVIS_FOCUSED, "got 0x%0x\n", tviRoot.state);
775
776     /* Change the root text */
777     strncpy(szBuffer, "Testing123", nBufferSize);
778     ret = SendMessage( hTree, TVM_SETITEMA, 0, (LPARAM)&tviRoot );
779     expect(TRUE, ret);
780     memset(szBuffer, 0, nBufferSize);
781     ret = SendMessage( hTree, TVM_GETITEMA, 0, (LPARAM)&tviRoot );
782     expect(TRUE, ret);
783     ok(!strcmp("Testing123", szBuffer), "GetItem: szBuffer=\"%s\", expected \"Testing123\"\n", szBuffer);
784
785     /* Reset the root text */
786     memset(szBuffer, 0, nBufferSize);
787     strncpy(szBuffer, "Root", nBufferSize);
788     ret = SendMessage( hTree, TVM_SETITEMA, 0, (LPARAM)&tviRoot );
789     expect(TRUE, ret);
790
791     ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_item_seq,
792         "test get set item", FALSE);
793
794     DestroyWindow(hTree);
795 }
796
797 static void test_get_set_itemheight(void)
798 {
799     int ulOldHeight = 0;
800     int ulNewHeight = 0;
801     HWND hTree;
802
803     hTree = create_treeview_control(0);
804     fill_tree(hTree);
805
806     flush_sequences(sequences, NUM_MSG_SEQUENCES);
807
808     /* Assuming default height to begin with */
809     ulOldHeight = (int) SendMessage( hTree, TVM_GETITEMHEIGHT, 0, 0 );
810
811     /* Explicitly setting and getting the default height */
812     SendMessage( hTree, TVM_SETITEMHEIGHT, -1, 0 );
813     ulNewHeight = (int) SendMessage( hTree, TVM_GETITEMHEIGHT, 0, 0 );
814     ok(ulNewHeight == ulOldHeight, "Default height not set properly, reported %d, expected %d\n", ulNewHeight, ulOldHeight);
815
816     /* Explicitly setting and getting the height of twice the normal */
817     SendMessage( hTree, TVM_SETITEMHEIGHT, 2*ulOldHeight, 0 );
818     ulNewHeight = (int) SendMessage( hTree, TVM_GETITEMHEIGHT, 0, 0 );
819     ok(ulNewHeight == 2*ulOldHeight, "New height not set properly, reported %d, expected %d\n", ulNewHeight, 2*ulOldHeight);
820
821     /* Assuming tree doesn't have TVS_NONEVENHEIGHT set, so a set of 9 will round down to 8 */
822     SendMessage( hTree, TVM_SETITEMHEIGHT, 9, 0 );
823     ulNewHeight = (int) SendMessage( hTree, TVM_GETITEMHEIGHT, 0, 0 );
824     ok(ulNewHeight == 8, "Uneven height not set properly, reported %d, expected %d\n", ulNewHeight, 8);
825
826     ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_itemheight_seq,
827         "test get set item height", FALSE);
828
829     /* without TVS_NONEVENHEIGHT */
830     SetWindowLong(hTree, GWL_STYLE, GetWindowLong(hTree, GWL_STYLE) & ~TVS_NONEVENHEIGHT);
831     /* odd value */
832     ulOldHeight = SendMessage( hTree, TVM_SETITEMHEIGHT, 3, 0);
833     ok(ulOldHeight == 8, "got %d, expected %d\n", ulOldHeight, 8);
834     ulNewHeight = (int) SendMessage( hTree, TVM_GETITEMHEIGHT, 0, 0 );
835     ok(ulNewHeight == 2, "got %d, expected %d\n", ulNewHeight, 2);
836
837     ulOldHeight = SendMessage( hTree, TVM_SETITEMHEIGHT, 4, 0);
838     ok(ulOldHeight == 2, "got %d, expected %d\n", ulOldHeight, 2);
839     ulNewHeight = (int) SendMessage( hTree, TVM_GETITEMHEIGHT, 0, 0 );
840     ok(ulNewHeight == 4, "got %d, expected %d\n", ulNewHeight, 4);
841
842     /* with TVS_NONEVENHEIGHT */
843     SetWindowLong(hTree, GWL_STYLE, GetWindowLong(hTree, GWL_STYLE) | TVS_NONEVENHEIGHT);
844     /* odd value */
845     ulOldHeight = SendMessage( hTree, TVM_SETITEMHEIGHT, 3, 0);
846     ok(ulOldHeight == 4, "got %d, expected %d\n", ulOldHeight, 4);
847     ulNewHeight = (int) SendMessage( hTree, TVM_GETITEMHEIGHT, 0, 0 );
848     ok(ulNewHeight == 3, "got %d, expected %d\n", ulNewHeight, 3);
849     /* even value */
850     ulOldHeight = SendMessage( hTree, TVM_SETITEMHEIGHT, 10, 0);
851     ok(ulOldHeight == 3, "got %d, expected %d\n", ulOldHeight, 3);
852     ulNewHeight = (int) SendMessage( hTree, TVM_GETITEMHEIGHT, 0, 0 );
853     ok(ulNewHeight == 10, "got %d, expected %d\n", ulNewHeight, 10);
854
855     DestroyWindow(hTree);
856 }
857
858 static void test_get_set_scrolltime(void)
859 {
860     int ulExpectedTime = 20;
861     int ulTime = 0;
862     HWND hTree;
863
864     hTree = create_treeview_control(0);
865     fill_tree(hTree);
866
867     flush_sequences(sequences, NUM_MSG_SEQUENCES);
868
869     SendMessage( hTree, TVM_SETSCROLLTIME, ulExpectedTime, 0 );
870     ulTime = (int)SendMessage( hTree, TVM_GETSCROLLTIME, 0, 0 );
871     ok(ulTime == ulExpectedTime, "Scroll time reported as %d, expected %d\n", ulTime, ulExpectedTime);
872
873     ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_scrolltime_seq,
874         "test get set scroll time", FALSE);
875
876     DestroyWindow(hTree);
877 }
878
879 static void test_get_set_textcolor(void)
880 {
881     /* If the value is -1, the control is using the system color for the text color. */
882     COLORREF crColor = RGB(0,0,0);
883     HWND hTree;
884
885     hTree = create_treeview_control(0);
886     fill_tree(hTree);
887
888     flush_sequences(sequences, NUM_MSG_SEQUENCES);
889
890     crColor = (COLORREF)SendMessage( hTree, TVM_GETTEXTCOLOR, 0, 0 );
891     ok(crColor == ~0u, "Default text color reported as 0x%.8x\n", crColor);
892
893     /* Test for black text */
894     SendMessage( hTree, TVM_SETTEXTCOLOR, 0, RGB(0,0,0) );
895     crColor = (COLORREF)SendMessage( hTree, TVM_GETTEXTCOLOR, 0, 0 );
896     ok(crColor == RGB(0,0,0), "Black text color reported as 0x%.8x\n", crColor);
897
898     /* Test for white text */
899     SendMessage( hTree, TVM_SETTEXTCOLOR, 0, RGB(255,255,255) );
900     crColor = (COLORREF)SendMessage( hTree, TVM_GETTEXTCOLOR, 0, 0 );
901     ok(crColor == RGB(255,255,255), "White text color reported as 0x%.8x\n", crColor);
902
903     /* Reset the default text color */
904     SendMessage( hTree, TVM_SETTEXTCOLOR, 0, CLR_NONE );
905
906     ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_textcolor_seq,
907         "test get set text color", FALSE);
908
909     DestroyWindow(hTree);
910 }
911
912 static void test_get_set_tooltips(void)
913 {
914     HWND hwndLastToolTip = NULL;
915     HWND hPopupTreeView;
916     HWND hTree;
917
918     hTree = create_treeview_control(0);
919     fill_tree(hTree);
920
921     flush_sequences(sequences, NUM_MSG_SEQUENCES);
922
923     /* show even WS_POPUP treeview don't send NM_TOOLTIPSCREATED */
924     hPopupTreeView = CreateWindow(WC_TREEVIEW, NULL, WS_POPUP|WS_VISIBLE, 0, 0, 100, 100, hMainWnd, NULL, NULL, NULL);
925     DestroyWindow(hPopupTreeView);
926
927     /* Testing setting a NULL ToolTip */
928     SendMessage( hTree, TVM_SETTOOLTIPS, 0, 0 );
929     hwndLastToolTip = (HWND)SendMessage( hTree, TVM_GETTOOLTIPS, 0, 0 );
930     ok(hwndLastToolTip == NULL, "NULL tool tip, reported as 0x%p, expected 0.\n", hwndLastToolTip);
931
932     ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_tooltips_seq,
933         "test get set tooltips", TRUE);
934
935     /* TODO: Add a test of an actual tooltip */
936     DestroyWindow(hTree);
937 }
938
939 static void test_get_set_unicodeformat(void)
940 {
941     BOOL bPreviousSetting = 0;
942     BOOL bNewSetting = 0;
943     HWND hTree;
944
945     hTree = create_treeview_control(0);
946     fill_tree(hTree);
947
948     flush_sequences(sequences, NUM_MSG_SEQUENCES);
949
950     /* Set to Unicode */
951     bPreviousSetting = (BOOL)SendMessage( hTree, TVM_SETUNICODEFORMAT, 1, 0 );
952     bNewSetting = (BOOL)SendMessage( hTree, TVM_GETUNICODEFORMAT, 0, 0 );
953     ok(bNewSetting == 1, "Unicode setting did not work.\n");
954
955     /* Set to ANSI */
956     SendMessage( hTree, TVM_SETUNICODEFORMAT, 0, 0 );
957     bNewSetting = (BOOL)SendMessage( hTree, TVM_GETUNICODEFORMAT, 0, 0 );
958     ok(bNewSetting == 0, "ANSI setting did not work.\n");
959
960     /* Revert to original setting */
961     SendMessage( hTree, TVM_SETUNICODEFORMAT, bPreviousSetting, 0 );
962
963     ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_unicodeformat_seq,
964         "test get set unicode format", FALSE);
965
966     DestroyWindow(hTree);
967 }
968
969 static LRESULT CALLBACK parent_wnd_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
970 {
971     static LONG defwndproc_counter = 0;
972     struct message msg;
973     LRESULT ret;
974     RECT rect;
975     HTREEITEM visibleItem;
976
977     msg.message = message;
978     msg.flags = sent|wparam|lparam;
979     if (defwndproc_counter) msg.flags |= defwinproc;
980     msg.wParam = wParam;
981     msg.lParam = lParam;
982     if (message == WM_NOTIFY && lParam) msg.id = ((NMHDR*)lParam)->code;
983
984     /* log system messages, except for painting */
985     if (message < WM_USER &&
986         message != WM_PAINT &&
987         message != WM_ERASEBKGND &&
988         message != WM_NCPAINT &&
989         message != WM_NCHITTEST &&
990         message != WM_GETTEXT &&
991         message != WM_GETICON &&
992         message != WM_DEVICECHANGE)
993     {
994         trace("parent: %p, %04x, %08lx, %08lx\n", hWnd, message, wParam, lParam);
995         add_message(sequences, PARENT_SEQ_INDEX, &msg);
996     }
997
998     switch(message) {
999     case WM_NOTIFY:
1000     {
1001         NMHDR *pHdr = (NMHDR *)lParam;
1002     
1003         ok(pHdr->code != NM_TOOLTIPSCREATED, "Treeview should not send NM_TOOLTIPSCREATED\n");
1004         if (pHdr->idFrom == 100)
1005         {
1006             NMTREEVIEWA *pTreeView = (LPNMTREEVIEWA) lParam;
1007             switch(pHdr->code)
1008             {
1009             case TVN_SELCHANGINGA:
1010                 AddItem('(');
1011                 IdentifyItem(pTreeView->itemOld.hItem);
1012                 IdentifyItem(pTreeView->itemNew.hItem);
1013                 break;
1014             case TVN_SELCHANGEDA:
1015                 AddItem(')');
1016                 IdentifyItem(pTreeView->itemOld.hItem);
1017                 IdentifyItem(pTreeView->itemNew.hItem);
1018                 break;
1019             case TVN_GETDISPINFOA: {
1020                 NMTVDISPINFOA *disp = (NMTVDISPINFOA *)lParam;
1021                 if (disp->item.mask & TVIF_TEXT) {
1022                     lstrcpyn(disp->item.pszText, TEST_CALLBACK_TEXT, disp->item.cchTextMax);
1023                 }
1024
1025                 if (g_disp_A_to_W && (disp->item.mask & TVIF_TEXT)) {
1026                     static const WCHAR testW[] = {'T','E','S','T','2',0};
1027
1028                     disp->hdr.code = TVN_GETDISPINFOW;
1029                     memcpy(disp->item.pszText, testW, sizeof(testW));
1030                 }
1031
1032                 if (g_disp_set_stateimage)
1033                 {
1034                     ok(disp->item.mask == TVIF_IMAGE, "got %x\n", disp->item.mask);
1035                     /* both masks set here are necessary to change state bits */
1036                     disp->item.mask |= TVIF_STATE;
1037                     disp->item.state = TVIS_SELECTED | INDEXTOSTATEIMAGEMASK(2) | INDEXTOOVERLAYMASK(3);
1038                     disp->item.stateMask = TVIS_SELECTED | TVIS_OVERLAYMASK | TVIS_STATEIMAGEMASK;
1039                 }
1040
1041                 break;
1042               }
1043             case TVN_BEGINLABELEDIT:
1044               {
1045                 if (g_beginedit_alter_text)
1046                 {
1047                     static const char* textA = "<edittextaltered>";
1048                     HWND edit;
1049
1050                     edit = (HWND)SendMessageA(pHdr->hwndFrom, TVM_GETEDITCONTROL, 0, 0);
1051                     ok(IsWindow(edit), "failed to get edit handle\n");
1052                     SetWindowTextA(edit, textA);
1053                 }
1054
1055                 break;
1056               }
1057
1058             case TVN_ENDLABELEDIT: return TRUE;
1059             case TVN_ITEMEXPANDINGA:
1060                 ok(pTreeView->itemNew.mask ==
1061                    (TVIF_HANDLE | TVIF_SELECTEDIMAGE | TVIF_IMAGE | TVIF_PARAM | TVIF_STATE),
1062                    "got wrong mask %x\n", pTreeView->itemNew.mask);
1063                 ok((pTreeView->itemNew.state & TVIS_EXPANDED) == 0,
1064                    "got wrong state %x\n", pTreeView->itemNew.state);
1065                 ok(pTreeView->itemOld.mask == 0,
1066                    "got wrong mask %x\n", pTreeView->itemOld.mask);
1067
1068                 if (g_get_from_expand)
1069                 {
1070                   g_item_expanding.mask = TVIF_STATE;
1071                   g_item_expanding.hItem = hRoot;
1072                   ret = SendMessageA(pHdr->hwndFrom, TVM_GETITEMA, 0, (LPARAM)&g_item_expanding);
1073                   ok(ret == TRUE, "got %lu\n", ret);
1074                 }
1075                 break;
1076             case TVN_ITEMEXPANDEDA:
1077                 ok(pTreeView->itemNew.mask & TVIF_STATE, "got wrong mask %x\n", pTreeView->itemNew.mask);
1078                 ok(pTreeView->itemNew.state & (TVIS_EXPANDED|TVIS_EXPANDEDONCE),
1079                    "got wrong mask %x\n", pTreeView->itemNew.mask);
1080                 ok(pTreeView->itemOld.mask == 0,
1081                    "got wrong mask %x\n", pTreeView->itemOld.mask);
1082
1083                 if (g_get_from_expand)
1084                 {
1085                   g_item_expanded.mask = TVIF_STATE;
1086                   g_item_expanded.hItem = hRoot;
1087                   ret = SendMessageA(pHdr->hwndFrom, TVM_GETITEMA, 0, (LPARAM)&g_item_expanded);
1088                   ok(ret == TRUE, "got %lu\n", ret);
1089                 }
1090                 if (g_get_rect_in_expand)
1091                 {
1092                   visibleItem = TreeView_GetNextItem(pHdr->hwndFrom, NULL, TVGN_FIRSTVISIBLE);
1093                   ok(pTreeView->itemNew.hItem == visibleItem, "expanded item == first visible item\n");
1094                   *(HTREEITEM*)&rect = visibleItem;
1095                   ok(SendMessage(pHdr->hwndFrom, TVM_GETITEMRECT, TRUE, (LPARAM)&rect), "Failed to get rect for first visible item.\n");
1096                   visibleItem = TreeView_GetNextItem(pHdr->hwndFrom, visibleItem, TVGN_NEXTVISIBLE);
1097                   *(HTREEITEM*)&rect = visibleItem;
1098                   ok(visibleItem != NULL, "There must be a visible item after the first visisble item.\n");
1099                   ok(SendMessage(pHdr->hwndFrom, TVM_GETITEMRECT, TRUE, (LPARAM)&rect), "Failed to get rect for second visible item.\n");
1100                 }
1101                 break;
1102             case TVN_DELETEITEMA:
1103             {
1104                 struct message item;
1105
1106                 ok(pTreeView->itemNew.mask == 0, "got wrong mask 0x%x\n", pTreeView->itemNew.mask);
1107
1108                 ok(pTreeView->itemOld.mask == (TVIF_HANDLE | TVIF_PARAM), "got wrong mask 0x%x\n", pTreeView->itemOld.mask);
1109                 ok(pTreeView->itemOld.hItem != NULL, "got %p\n", pTreeView->itemOld.hItem);
1110
1111                 memset(&item, 0, sizeof(item));
1112                 item.lParam = (LPARAM)pTreeView->itemOld.hItem;
1113                 add_message(item_sequence, 0, &item);
1114
1115                 break;
1116             }
1117             }
1118         }
1119         break;
1120     }
1121
1122     case WM_DESTROY:
1123         PostQuitMessage(0);
1124         break;
1125     }
1126
1127     defwndproc_counter++;
1128     ret = DefWindowProcA(hWnd, message, wParam, lParam);
1129     defwndproc_counter--;
1130
1131     return ret;
1132 }
1133
1134 static void test_expandinvisible(void)
1135 {
1136     static CHAR nodeText[][5] = {"0", "1", "2", "3", "4"};
1137     TVINSERTSTRUCTA ins;
1138     HTREEITEM node[5];
1139     RECT dummyRect;
1140     BOOL nodeVisible;
1141     LRESULT ret;
1142     HWND hTree;
1143
1144     hTree = create_treeview_control(0);
1145
1146     /* The test builds the following tree and expands node 1, while node 0 is collapsed.
1147      *
1148      * 0
1149      * |- 1
1150      * |  |- 2
1151      * |  |- 3
1152      * |- 4
1153      *
1154      */
1155
1156     ret = TreeView_DeleteAllItems(hTree);
1157     ok(ret == TRUE, "ret\n");
1158     ins.hParent = TVI_ROOT;
1159     ins.hInsertAfter = TVI_ROOT;
1160     U(ins).item.mask = TVIF_TEXT;
1161     U(ins).item.pszText = nodeText[0];
1162     node[0] = TreeView_InsertItem(hTree, &ins);
1163     assert(node[0]);
1164
1165     ins.hInsertAfter = TVI_LAST;
1166     U(ins).item.mask = TVIF_TEXT;
1167     ins.hParent = node[0];
1168
1169     U(ins).item.pszText = nodeText[1];
1170     node[1] = TreeView_InsertItem(hTree, &ins);
1171     assert(node[1]);
1172     U(ins).item.pszText = nodeText[4];
1173     node[4] = TreeView_InsertItem(hTree, &ins);
1174     assert(node[4]);
1175
1176     ins.hParent = node[1];
1177
1178     U(ins).item.pszText = nodeText[2];
1179     node[2] = TreeView_InsertItem(hTree, &ins);
1180     assert(node[2]);
1181     U(ins).item.pszText = nodeText[3];
1182     node[3] = TreeView_InsertItem(hTree, &ins);
1183     assert(node[3]);
1184
1185
1186     nodeVisible = TreeView_GetItemRect(hTree, node[1], &dummyRect, FALSE);
1187     ok(!nodeVisible, "Node 1 should not be visible.\n");
1188     nodeVisible = TreeView_GetItemRect(hTree, node[2], &dummyRect, FALSE);
1189     ok(!nodeVisible, "Node 2 should not be visible.\n");
1190     nodeVisible = TreeView_GetItemRect(hTree, node[3], &dummyRect, FALSE);
1191     ok(!nodeVisible, "Node 3 should not be visible.\n");
1192     nodeVisible = TreeView_GetItemRect(hTree, node[4], &dummyRect, FALSE);
1193     ok(!nodeVisible, "Node 4 should not be visible.\n");
1194
1195     ok(TreeView_Expand(hTree, node[1], TVE_EXPAND), "Expand of node 1 failed.\n");
1196
1197     nodeVisible = TreeView_GetItemRect(hTree, node[1], &dummyRect, FALSE);
1198     ok(!nodeVisible, "Node 1 should not be visible.\n");
1199     nodeVisible = TreeView_GetItemRect(hTree, node[2], &dummyRect, FALSE);
1200     ok(!nodeVisible, "Node 2 should not be visible.\n");
1201     nodeVisible = TreeView_GetItemRect(hTree, node[3], &dummyRect, FALSE);
1202     ok(!nodeVisible, "Node 3 should not be visible.\n");
1203     nodeVisible = TreeView_GetItemRect(hTree, node[4], &dummyRect, FALSE);
1204     ok(!nodeVisible, "Node 4 should not be visible.\n");
1205
1206     DestroyWindow(hTree);
1207 }
1208
1209 static void test_itemedit(void)
1210 {
1211     DWORD r;
1212     HWND edit;
1213     TVITEMA item;
1214     CHAR buffA[20];
1215     HWND hTree;
1216
1217     hTree = create_treeview_control(0);
1218     fill_tree(hTree);
1219
1220     /* try with null item */
1221     edit = (HWND)SendMessage(hTree, TVM_EDITLABELA, 0, 0);
1222     ok(!IsWindow(edit), "Expected valid handle\n");
1223
1224     /* trigger edit */
1225     edit = (HWND)SendMessage(hTree, TVM_EDITLABELA, 0, (LPARAM)hRoot);
1226     ok(IsWindow(edit), "Expected valid handle\n");
1227     /* item shouldn't be selected automatically after TVM_EDITLABEL */
1228     r = SendMessage(hTree, TVM_GETITEMSTATE, (WPARAM)hRoot, TVIS_SELECTED);
1229     expect(0, r);
1230     /* try to cancel with wrong edit handle */
1231     r = SendMessage(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), 0);
1232     expect(0, r);
1233     ok(IsWindow(edit), "Expected edit control to be valid\n");
1234     r = SendMessage(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)edit);
1235     expect(0, r);
1236     ok(!IsWindow(edit), "Expected edit control to be destroyed\n");
1237     /* try to cancel without creating edit */
1238     r = SendMessage(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), 0);
1239     expect(0, r);
1240
1241     /* try to cancel with wrong (not null) handle */
1242     edit = (HWND)SendMessage(hTree, TVM_EDITLABELA, 0, (LPARAM)hRoot);
1243     ok(IsWindow(edit), "Expected valid handle\n");
1244     r = SendMessage(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)hTree);
1245     expect(0, r);
1246     ok(IsWindow(edit), "Expected edit control to be valid\n");
1247     r = SendMessage(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)edit);
1248     expect(0, r);
1249
1250     /* remove selection after starting edit */
1251     r = TreeView_SelectItem(hTree, hRoot);
1252     expect(TRUE, r);
1253     edit = (HWND)SendMessage(hTree, TVM_EDITLABELA, 0, (LPARAM)hRoot);
1254     ok(IsWindow(edit), "Expected valid handle\n");
1255     r = TreeView_SelectItem(hTree, NULL);
1256     expect(TRUE, r);
1257     /* alter text */
1258     strcpy(buffA, "x");
1259     r = SendMessage(edit, WM_SETTEXT, 0, (LPARAM)buffA);
1260     expect(TRUE, r);
1261     r = SendMessage(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)edit);
1262     expect(0, r);
1263     ok(!IsWindow(edit), "Expected edit control to be destroyed\n");
1264     /* check that text is saved */
1265     item.mask = TVIF_TEXT;
1266     item.hItem = hRoot;
1267     item.pszText = buffA;
1268     item.cchTextMax = sizeof(buffA)/sizeof(CHAR);
1269     r = SendMessage(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1270     expect(TRUE, r);
1271     ok(!strcmp("x", buffA), "Expected item text to change\n");
1272
1273     /* try A/W messages */
1274     edit = (HWND)SendMessageA(hTree, TVM_EDITLABELA, 0, (LPARAM)hRoot);
1275     ok(IsWindow(edit), "Expected valid handle\n");
1276     ok(IsWindowUnicode(edit), "got ansi window\n");
1277     r = SendMessage(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)edit);
1278     expect(0, r);
1279     ok(!IsWindow(edit), "expected invalid handle\n");
1280
1281     edit = (HWND)SendMessageA(hTree, TVM_EDITLABELW, 0, (LPARAM)hRoot);
1282     ok(IsWindow(edit), "Expected valid handle\n");
1283     ok(IsWindowUnicode(edit), "got ansi window\n");
1284     r = SendMessage(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)edit);
1285     expect(0, r);
1286
1287     /* alter text during TVM_BEGINLABELEDIT, check that it's preserved */
1288     strcpy(buffA, "<root>");
1289
1290     item.mask = TVIF_TEXT;
1291     item.hItem = hRoot;
1292     item.pszText = buffA;
1293     item.cchTextMax = 0;
1294     r = SendMessage(hTree, TVM_SETITEMA, 0, (LPARAM)&item);
1295     expect(TRUE, r);
1296
1297     g_beginedit_alter_text = TRUE;
1298     edit = (HWND)SendMessageA(hTree, TVM_EDITLABELA, 0, (LPARAM)hRoot);
1299     ok(IsWindow(edit), "Expected valid handle\n");
1300     g_beginedit_alter_text = FALSE;
1301
1302     GetWindowTextA(edit, buffA, sizeof(buffA)/sizeof(CHAR));
1303     ok(!strcmp(buffA, "<edittextaltered>"), "got string %s\n", buffA);
1304
1305     DestroyWindow(hTree);
1306 }
1307
1308 static void test_treeview_classinfo(void)
1309 {
1310     WNDCLASSA cls;
1311
1312     memset(&cls, 0, sizeof(cls));
1313     GetClassInfo(GetModuleHandleA("comctl32.dll"), WC_TREEVIEWA, &cls);
1314     ok(cls.hbrBackground == NULL, "Expected NULL background brush, got %p\n", cls.hbrBackground);
1315     ok(cls.style == (CS_GLOBALCLASS | CS_DBLCLKS), "Expected got %x\n", cls.style);
1316     expect(0, cls.cbClsExtra);
1317 }
1318
1319 static void test_get_linecolor(void)
1320 {
1321     COLORREF clr;
1322     HWND hTree;
1323
1324     hTree = create_treeview_control(0);
1325
1326     /* newly created control has default color */
1327     clr = (COLORREF)SendMessage(hTree, TVM_GETLINECOLOR, 0, 0);
1328     if (clr == 0)
1329         win_skip("TVM_GETLINECOLOR is not supported on comctl32 < 5.80\n");
1330     else
1331         expect(CLR_DEFAULT, clr);
1332
1333     DestroyWindow(hTree);
1334 }
1335
1336 static void test_get_insertmarkcolor(void)
1337 {
1338     COLORREF clr;
1339     HWND hTree;
1340
1341     hTree = create_treeview_control(0);
1342
1343     /* newly created control has default color */
1344     clr = (COLORREF)SendMessage(hTree, TVM_GETINSERTMARKCOLOR, 0, 0);
1345     if (clr == 0)
1346         win_skip("TVM_GETINSERTMARKCOLOR is not supported on comctl32 < 5.80\n");
1347     else
1348         expect(CLR_DEFAULT, clr);
1349
1350     DestroyWindow(hTree);
1351 }
1352
1353 static void test_expandnotify(void)
1354 {
1355     HWND hTree;
1356     BOOL ret;
1357     TVITEMA item;
1358
1359     hTree = create_treeview_control(0);
1360     fill_tree(hTree);
1361
1362     item.hItem = hRoot;
1363     item.mask = TVIF_STATE;
1364
1365     item.state = TVIS_EXPANDED;
1366     ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1367     expect(TRUE, ret);
1368     ok((item.state & TVIS_EXPANDED) == 0, "expected collapsed\n");
1369
1370     /* preselect root node here */
1371     ret = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hRoot);
1372     expect(TRUE, ret);
1373
1374     g_get_from_expand = TRUE;
1375     /* expand */
1376     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1377     g_item_expanding.state = 0xdeadbeef;
1378     g_item_expanded.state = 0xdeadbeef;
1379     ret = SendMessageA(hTree, TVM_EXPAND, TVE_EXPAND, (LPARAM)hRoot);
1380     expect(TRUE, ret);
1381     ok(g_item_expanding.state == TVIS_SELECTED, "got state on TVN_ITEMEXPANDING 0x%08x\n",
1382        g_item_expanding.state);
1383     ok(g_item_expanded.state == (TVIS_SELECTED|TVIS_EXPANDED), "got state on TVN_ITEMEXPANDED 0x%08x\n",
1384        g_item_expanded.state);
1385     ok_sequence(sequences, PARENT_SEQ_INDEX, parent_expand_seq, "expand notifications", FALSE);
1386     g_get_from_expand = FALSE;
1387
1388     /* check that it's expanded */
1389     item.state = TVIS_EXPANDED;
1390     ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1391     expect(TRUE, ret);
1392     ok((item.state & TVIS_EXPANDED) == TVIS_EXPANDED, "expected expanded\n");
1393
1394     /* collapse */
1395     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1396     ret = SendMessageA(hTree, TVM_EXPAND, TVE_COLLAPSE, (LPARAM)hRoot);
1397     expect(TRUE, ret);
1398     item.state = TVIS_EXPANDED;
1399     ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1400     expect(TRUE, ret);
1401     ok((item.state & TVIS_EXPANDED) == 0, "expected collapsed\n");
1402     /* all further collapse/expand attempts won't produce any notifications,
1403        the only way is to reset with all children removed */
1404     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq, "collapse after expand notifications", FALSE);
1405
1406     /* try to toggle child that doesn't have children itself */
1407     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1408     ret = SendMessageA(hTree, TVM_EXPAND, TVE_TOGGLE, (LPARAM)hChild);
1409     expect(FALSE, ret);
1410     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq, "toggle node without children", FALSE);
1411
1412     DestroyWindow(hTree);
1413
1414     /* test TVM_GETITEMRECT inside TVN_ITEMEXPANDED notification */
1415     hTree = create_treeview_control(0);
1416     fill_tree(hTree);
1417     g_get_rect_in_expand = TRUE;
1418     ret = TreeView_Select(hTree, hChild, TVGN_CARET);
1419     expect(TRUE, ret);
1420     g_get_rect_in_expand = FALSE;
1421
1422     DestroyWindow(hTree);
1423
1424     /* TVE_TOGGLE acts as any other TVM_EXPAND */
1425     hTree = create_treeview_control(0);
1426     fill_tree(hTree);
1427
1428     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1429     ret = SendMessageA(hTree, TVM_EXPAND, TVE_TOGGLE, (LPARAM)hRoot);
1430     expect(TRUE, ret);
1431     ok_sequence(sequences, PARENT_SEQ_INDEX, parent_expand_seq, "toggle node (expand)", FALSE);
1432
1433     /* toggle again - no notifications */
1434     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1435     ret = SendMessageA(hTree, TVM_EXPAND, TVE_TOGGLE, (LPARAM)hRoot);
1436     expect(TRUE, ret);
1437     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq, "toggle node (collapse)", FALSE);
1438
1439     DestroyWindow(hTree);
1440
1441     /* some keyboard events are also translated to expand */
1442     hTree = create_treeview_control(0);
1443     fill_tree(hTree);
1444
1445     /* preselect root node here */
1446     ret = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hRoot);
1447     expect(TRUE, ret);
1448
1449     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1450     ret = SendMessageA(hTree, WM_KEYDOWN, VK_ADD, 0);
1451     expect(FALSE, ret);
1452     ok_sequence(sequences, PARENT_SEQ_INDEX, parent_expand_kb_seq, "expand node", FALSE);
1453
1454     /* go to child */
1455     ret = SendMessageA(hTree, WM_KEYDOWN, VK_RIGHT, 0);
1456     expect(FALSE, ret);
1457
1458     /* try to expand child that doesn't have children itself */
1459     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1460     ret = SendMessageA(hTree, WM_KEYDOWN, VK_ADD, 0);
1461     expect(FALSE, ret);
1462     ok_sequence(sequences, PARENT_SEQ_INDEX, parent_expand_empty_kb_seq, "expand node with no children", TRUE);
1463
1464     DestroyWindow(hTree);
1465 }
1466
1467 static void test_expandedimage(void)
1468 {
1469     TVITEMEXA item;
1470     HWND hTree;
1471     BOOL ret;
1472
1473     hTree = create_treeview_control(0);
1474     fill_tree(hTree);
1475
1476     item.mask = TVIF_EXPANDEDIMAGE;
1477     item.iExpandedImage = 1;
1478     item.hItem = hRoot;
1479     ret = SendMessageA(hTree, TVM_SETITEMA, 0, (LPARAM)&item);
1480     ok(ret, "got %d\n", ret);
1481
1482     item.mask = TVIF_EXPANDEDIMAGE;
1483     item.iExpandedImage = -1;
1484     item.hItem = hRoot;
1485     ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1486     ok(ret, "got %d\n", ret);
1487
1488     if (item.iExpandedImage != 1)
1489     {
1490         win_skip("TVIF_EXPANDEDIMAGE not supported\n");
1491         DestroyWindow(hTree);
1492         return;
1493     }
1494
1495     /* test for default iExpandedImage value */
1496     item.mask = TVIF_EXPANDEDIMAGE;
1497     item.iExpandedImage = -1;
1498     item.hItem = hChild;
1499     ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1500     ok(ret, "got %d\n", ret);
1501     ok(item.iExpandedImage == (WORD)I_IMAGENONE, "got %d\n", item.iExpandedImage);
1502
1503     DestroyWindow(hTree);
1504 }
1505
1506 static void test_TVS_SINGLEEXPAND(void)
1507 {
1508     HWND hTree;
1509     BOOL ret;
1510
1511     hTree = create_treeview_control(0);
1512     SetWindowLongA(hTree, GWL_STYLE, GetWindowLong(hTree, GWL_STYLE) | TVS_SINGLEEXPAND);
1513     /* to avoid painting related notifications */
1514     ShowWindow(hTree, SW_HIDE);
1515     fill_tree(hTree);
1516
1517     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1518     ret = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hRoot);
1519     ok(ret, "got %d\n", ret);
1520     ok_sequence(sequences, PARENT_SEQ_INDEX, parent_singleexpand_seq, "singleexpand notifications", FALSE);
1521
1522     /* a workaround for NT4 that sends expand notifications when nothing is about to expand */
1523     ret = SendMessageA(hTree, TVM_DELETEITEM, 0, (LPARAM)hRoot);
1524     ok(ret, "got %d\n", ret);
1525     fill_tree(hTree);
1526     ret = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, 0);
1527     ok(ret, "got %d\n", ret);
1528
1529     DestroyWindow(hTree);
1530 }
1531
1532 static void test_WM_PAINT(void)
1533 {
1534     HWND hTree;
1535     COLORREF clr;
1536     LONG ret;
1537     RECT rc;
1538     HDC hdc;
1539
1540     hTree = create_treeview_control(0);
1541
1542     clr = SendMessageA(hTree, TVM_SETBKCOLOR, 0, RGB(255, 0, 0));
1543     ok(clr == ~0u, "got %d, expected -1\n", clr);
1544
1545     hdc = GetDC(hMainWnd);
1546
1547     GetClientRect(hMainWnd, &rc);
1548     FillRect(hdc, &rc, GetStockObject(BLACK_BRUSH));
1549
1550     clr = GetPixel(hdc, 1, 1);
1551     ok(clr == RGB(0, 0, 0), "got 0x%x\n", clr);
1552
1553     ret = SendMessageA(hTree, WM_PAINT, (WPARAM)hdc, 0);
1554     ok(ret == 0, "got %d\n", ret);
1555
1556     clr = GetPixel(hdc, 1, 1);
1557     ok(clr == RGB(255, 0, 0) || broken(clr == RGB(0, 0, 0)) /* win98 */,
1558         "got 0x%x\n", clr);
1559
1560     ReleaseDC(hMainWnd, hdc);
1561
1562     DestroyWindow(hTree);
1563 }
1564
1565 static void test_delete_items(void)
1566 {
1567     const struct message *msg;
1568     HWND hTree;
1569     INT ret;
1570
1571     hTree = create_treeview_control(0);
1572     fill_tree(hTree);
1573
1574     /* check delete order */
1575     flush_sequences(item_sequence, 1);
1576     ret = SendMessage(hTree, TVM_DELETEITEM, 0, 0);
1577     ok(ret == TRUE, "got %d\n", ret);
1578
1579     msg = item_sequence[0]->sequence;
1580     ok(item_sequence[0]->count == 2, "expected 2 items, got %d\n", item_sequence[0]->count);
1581
1582     if (item_sequence[0]->count == 2)
1583     {
1584       ok(msg[0].lParam == (LPARAM)hChild, "expected %p, got 0x%lx\n", hChild, msg[0].lParam);
1585       ok(msg[1].lParam == (LPARAM)hRoot, "expected %p, got 0x%lx\n", hRoot, msg[1].lParam);
1586     }
1587
1588     ret = SendMessageA(hTree, TVM_GETCOUNT, 0, 0);
1589     ok(ret == 0, "got %d\n", ret);
1590
1591     DestroyWindow(hTree);
1592 }
1593
1594 struct _ITEM_DATA
1595 {
1596     HTREEITEM  parent; /* for root value of parent field is unidetified */
1597     HTREEITEM  nextsibling;
1598     HTREEITEM  firstchild;
1599 };
1600
1601 static void _check_item(HTREEITEM item, HTREEITEM parent, HTREEITEM nextsibling, HTREEITEM firstchild, int line)
1602 {
1603     struct _ITEM_DATA *data = (struct _ITEM_DATA*)item;
1604
1605     ok_(__FILE__, line)(data->parent == parent, "parent %p, got %p\n", parent, data->parent);
1606     ok_(__FILE__, line)(data->nextsibling == nextsibling, "sibling %p, got %p\n", nextsibling, data->nextsibling);
1607     ok_(__FILE__, line)(data->firstchild == firstchild, "firstchild %p, got %p\n", firstchild, data->firstchild);
1608 }
1609
1610 #define check_item(a, b, c, d) _check_item(a, b, c, d, __LINE__)
1611
1612 static void test_htreeitem_layout(void)
1613 {
1614     TVINSERTSTRUCTA ins;
1615     HTREEITEM item1, item2;
1616     HWND hTree;
1617
1618     hTree = create_treeview_control(0);
1619     fill_tree(hTree);
1620
1621     /* root has some special pointer in parent field */
1622     check_item(hRoot, ((struct _ITEM_DATA*)hRoot)->parent, 0, hChild);
1623     check_item(hChild, hRoot, 0, 0);
1624
1625     ins.hParent = hChild;
1626     ins.hInsertAfter = TVI_FIRST;
1627     U(ins).item.mask = 0;
1628     item1 = TreeView_InsertItem(hTree, &ins);
1629
1630     check_item(item1, hChild, 0, 0);
1631
1632     ins.hParent = hRoot;
1633     ins.hInsertAfter = TVI_FIRST;
1634     U(ins).item.mask = 0;
1635     item2 = TreeView_InsertItem(hTree, &ins);
1636
1637     check_item(item2, hRoot, hChild, 0);
1638
1639     SendMessage(hTree, TVM_DELETEITEM, 0, (LPARAM)hChild);
1640
1641     /* without children now */
1642     check_item(hRoot, ((struct _ITEM_DATA*)hRoot)->parent, 0, item2);
1643
1644     DestroyWindow(hTree);
1645 }
1646
1647 static void test_TVS_CHECKBOXES(void)
1648 {
1649     HIMAGELIST himl, himl2;
1650     HWND hTree, hTree2;
1651     TVITEMA item;
1652     DWORD ret;
1653
1654     hTree = create_treeview_control(0);
1655     fill_tree(hTree);
1656
1657     himl = (HIMAGELIST)SendMessageA(hTree, TVM_GETIMAGELIST, TVSIL_STATE, 0);
1658     ok(himl == NULL, "got %p\n", himl);
1659
1660     item.hItem = hRoot;
1661     item.mask = TVIF_STATE;
1662     item.state = INDEXTOSTATEIMAGEMASK(1);
1663     item.stateMask = TVIS_STATEIMAGEMASK;
1664     ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1665     expect(TRUE, ret);
1666     ok(item.state == 0, "got 0x%x\n", item.state);
1667
1668     /* set some index for a child */
1669     item.hItem = hChild;
1670     item.mask = TVIF_STATE;
1671     item.state = INDEXTOSTATEIMAGEMASK(4);
1672     item.stateMask = TVIS_STATEIMAGEMASK;
1673     ret = SendMessageA(hTree, TVM_SETITEMA, 0, (LPARAM)&item);
1674     expect(TRUE, ret);
1675
1676     /* enabling check boxes set all items to 1 state image index */
1677     SetWindowLongA(hTree, GWL_STYLE, GetWindowLongA(hTree, GWL_STYLE) | TVS_CHECKBOXES);
1678     himl = (HIMAGELIST)SendMessageA(hTree, TVM_GETIMAGELIST, TVSIL_STATE, 0);
1679     ok(himl != NULL, "got %p\n", himl);
1680
1681     himl2 = (HIMAGELIST)SendMessageA(hTree, TVM_GETIMAGELIST, TVSIL_STATE, 0);
1682     ok(himl2 != NULL, "got %p\n", himl2);
1683     ok(himl2 == himl, "got %p, expected %p\n", himl2, himl);
1684
1685     item.hItem = hRoot;
1686     item.mask = TVIF_STATE;
1687     item.state = 0;
1688     item.stateMask = TVIS_STATEIMAGEMASK;
1689     ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1690     expect(TRUE, ret);
1691     ok(item.state == INDEXTOSTATEIMAGEMASK(1), "got 0x%x\n", item.state);
1692
1693     item.hItem = hChild;
1694     item.mask = TVIF_STATE;
1695     item.state = 0;
1696     item.stateMask = TVIS_STATEIMAGEMASK;
1697     ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1698     expect(TRUE, ret);
1699     ok(item.state == INDEXTOSTATEIMAGEMASK(1), "got 0x%x\n", item.state);
1700
1701     /* create another control and check its checkbox list */
1702     hTree2 = create_treeview_control(0);
1703     fill_tree(hTree2);
1704
1705     /* set some index for a child */
1706     item.hItem = hChild;
1707     item.mask = TVIF_STATE;
1708     item.state = INDEXTOSTATEIMAGEMASK(4);
1709     item.stateMask = TVIS_STATEIMAGEMASK;
1710     ret = SendMessageA(hTree2, TVM_SETITEMA, 0, (LPARAM)&item);
1711     expect(TRUE, ret);
1712
1713     /* enabling check boxes set all items to 1 state image index */
1714     SetWindowLongA(hTree2, GWL_STYLE, GetWindowLongA(hTree, GWL_STYLE) | TVS_CHECKBOXES);
1715     himl2 = (HIMAGELIST)SendMessageA(hTree2, TVM_GETIMAGELIST, TVSIL_STATE, 0);
1716     ok(himl2 != NULL, "got %p\n", himl2);
1717     ok(himl != himl2, "got %p, expected %p\n", himl2, himl);
1718
1719     DestroyWindow(hTree2);
1720     DestroyWindow(hTree);
1721
1722     /* the same, but initially created with TVS_CHECKBOXES */
1723     hTree = create_treeview_control(0);
1724     fill_tree(hTree);
1725     himl = (HIMAGELIST)SendMessageA(hTree, TVM_GETIMAGELIST, TVSIL_STATE, 0);
1726     ok(himl == NULL, "got %p\n", himl);
1727     DestroyWindow(hTree);
1728
1729     hTree = create_treeview_control(TVS_CHECKBOXES);
1730     fill_tree(hTree);
1731     himl = (HIMAGELIST)SendMessageA(hTree, TVM_GETIMAGELIST, TVSIL_STATE, 0);
1732     todo_wine ok(himl == NULL, "got %p\n", himl);
1733
1734     item.hItem = hRoot;
1735     item.mask = TVIF_STATE;
1736     item.state = 0;
1737     item.stateMask = TVIS_STATEIMAGEMASK;
1738     ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1739     expect(TRUE, ret);
1740     ok(item.state == INDEXTOSTATEIMAGEMASK(1), "got 0x%x\n", item.state);
1741
1742     item.hItem = hChild;
1743     item.mask = TVIF_STATE;
1744     item.state = 0;
1745     item.stateMask = TVIS_STATEIMAGEMASK;
1746     ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1747     expect(TRUE, ret);
1748     ok(item.state == INDEXTOSTATEIMAGEMASK(1), "got 0x%x\n", item.state);
1749
1750     DestroyWindow(hTree);
1751 }
1752
1753 static void test_TVM_GETNEXTITEM(void)
1754 {
1755     HTREEITEM item;
1756     HWND hTree;
1757
1758     hTree = create_treeview_control(0);
1759     fill_tree(hTree);
1760
1761     item = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_ROOT, 0);
1762     ok(item == hRoot, "got %p, expected %p\n", item, hRoot);
1763
1764     item = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_ROOT, (LPARAM)TVI_ROOT);
1765     ok(item == hRoot, "got %p, expected %p\n", item, hRoot);
1766
1767     item = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_ROOT, (LPARAM)hRoot);
1768     ok(item == hRoot, "got %p, expected %p\n", item, hRoot);
1769
1770     item = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_ROOT, (LPARAM)hChild);
1771     ok(item == hRoot, "got %p, expected %p\n", item, hRoot);
1772
1773     item = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_CHILD, 0);
1774     ok(item == hRoot, "got %p, expected %p\n", item, hRoot);
1775
1776     item = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hRoot);
1777     ok(item == hChild, "got %p, expected %p\n", item, hChild);
1778
1779     item = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)TVI_ROOT);
1780     ok(item == hRoot, "got %p, expected %p\n", item, hRoot);
1781
1782     item = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_PARENT, 0);
1783     ok(item == NULL, "got %p\n", item);
1784
1785     item = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_PARENT, (LPARAM)hChild);
1786     ok(item == hRoot, "got %p, expected %p\n", item, hRoot);
1787
1788     DestroyWindow(hTree);
1789 }
1790
1791 static void test_TVM_HITTEST(void)
1792 {
1793     HWND hTree;
1794     LRESULT ret;
1795     RECT rc;
1796     TVHITTESTINFO ht;
1797
1798     hTree = create_treeview_control(0);
1799     fill_tree(hTree);
1800
1801     *(HTREEITEM*)&rc = hRoot;
1802     ret = SendMessage(hTree, TVM_GETITEMRECT, TRUE, (LPARAM)&rc);
1803     expect(TRUE, (BOOL)ret);
1804
1805     ht.pt.x = rc.left-1;
1806     ht.pt.y = rc.top;
1807
1808     ret = SendMessage(hTree, TVM_HITTEST, 0, (LPARAM)&ht);
1809     ok((HTREEITEM)ret == hRoot, "got %p, expected %p\n", (HTREEITEM)ret, hRoot);
1810     ok(ht.hItem == hRoot, "got %p, expected %p\n", ht.hItem, hRoot);
1811     ok(ht.flags == TVHT_ONITEMBUTTON, "got %d, expected %d\n", ht.flags, TVHT_ONITEMBUTTON);
1812
1813     ret = SendMessageA(hTree, TVM_EXPAND, TVE_EXPAND, (LPARAM)hRoot);
1814     expect(TRUE, (BOOL)ret);
1815
1816     *(HTREEITEM*)&rc = hChild;
1817     ret = SendMessage(hTree, TVM_GETITEMRECT, TRUE, (LPARAM)&rc);
1818     expect(TRUE, (BOOL)ret);
1819
1820     ht.pt.x = rc.left-1;
1821     ht.pt.y = rc.top;
1822
1823     ret = SendMessage(hTree, TVM_HITTEST, 0, (LPARAM)&ht);
1824     ok((HTREEITEM)ret == hChild, "got %p, expected %p\n", (HTREEITEM)ret, hChild);
1825     ok(ht.hItem == hChild, "got %p, expected %p\n", ht.hItem, hChild);
1826     /* Wine returns item button here, but this item has no button */
1827     todo_wine ok(ht.flags == TVHT_ONITEMINDENT, "got %d, expected %d\n", ht.flags, TVHT_ONITEMINDENT);
1828
1829     DestroyWindow(hTree);
1830 }
1831
1832 START_TEST(treeview)
1833 {
1834     HMODULE hComctl32;
1835     BOOL (WINAPI *pInitCommonControlsEx)(const INITCOMMONCONTROLSEX*);
1836     WNDCLASSA wc;
1837     MSG msg;
1838
1839     ULONG_PTR ctx_cookie;
1840     HANDLE hCtx;
1841     HWND hwnd;
1842   
1843     hComctl32 = GetModuleHandleA("comctl32.dll");
1844     pInitCommonControlsEx = (void*)GetProcAddress(hComctl32, "InitCommonControlsEx");
1845     if (pInitCommonControlsEx)
1846     {
1847         INITCOMMONCONTROLSEX iccex;
1848         iccex.dwSize = sizeof(iccex);
1849         iccex.dwICC  = ICC_TREEVIEW_CLASSES;
1850         pInitCommonControlsEx(&iccex);
1851     }
1852     else
1853         InitCommonControls();
1854
1855     init_msg_sequences(sequences, NUM_MSG_SEQUENCES);
1856     init_msg_sequences(item_sequence, 1);
1857   
1858     wc.style = CS_HREDRAW | CS_VREDRAW;
1859     wc.cbClsExtra = 0;
1860     wc.cbWndExtra = 0;
1861     wc.hInstance = GetModuleHandleA(NULL);
1862     wc.hIcon = NULL;
1863     wc.hCursor = LoadCursorA(NULL, IDC_IBEAM);
1864     wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
1865     wc.lpszMenuName = NULL;
1866     wc.lpszClassName = "MyTestWnd";
1867     wc.lpfnWndProc = parent_wnd_proc;
1868     RegisterClassA(&wc);
1869
1870     hMainWnd = CreateWindowExA(0, "MyTestWnd", "Blah", WS_OVERLAPPEDWINDOW,
1871       CW_USEDEFAULT, CW_USEDEFAULT, 130, 105, NULL, NULL, GetModuleHandleA(NULL), 0);
1872
1873     ok(hMainWnd != NULL, "Failed to create parent window. Tests aborted.\n");
1874     if (!hMainWnd) return;
1875
1876     test_fillroot();
1877     test_select();
1878     test_getitemtext();
1879     test_focus();
1880     test_get_set_bkcolor();
1881     test_get_set_imagelist();
1882     test_get_set_indent();
1883     test_get_set_insertmark();
1884     test_get_set_item();
1885     test_get_set_itemheight();
1886     test_get_set_scrolltime();
1887     test_get_set_textcolor();
1888     test_get_linecolor();
1889     test_get_insertmarkcolor();
1890     test_get_set_tooltips();
1891     test_get_set_unicodeformat();
1892     test_callback();
1893     test_expandinvisible();
1894     test_itemedit();
1895     test_treeview_classinfo();
1896     test_expandnotify();
1897     test_TVS_SINGLEEXPAND();
1898     test_WM_PAINT();
1899     test_delete_items();
1900     test_htreeitem_layout();
1901     test_TVS_CHECKBOXES();
1902     test_TVM_GETNEXTITEM();
1903     test_TVM_HITTEST();
1904
1905     if (!load_v6_module(&ctx_cookie, &hCtx))
1906     {
1907         DestroyWindow(hMainWnd);
1908         return;
1909     }
1910
1911     /* this is a XP SP3 failure workaround */
1912     hwnd = CreateWindowExA(0, WC_TREEVIEW, "foo",
1913                            WS_CHILD | WS_BORDER | WS_VISIBLE,
1914                            0, 0, 100, 100,
1915                            hMainWnd, NULL, GetModuleHandleA(NULL), NULL);
1916     if (!IsWindow(hwnd))
1917     {
1918         win_skip("FIXME: failed to create TreeView window.\n");
1919         unload_v6_module(ctx_cookie, hCtx);
1920         DestroyWindow(hMainWnd);
1921         return;
1922     }
1923     else
1924         DestroyWindow(hwnd);
1925
1926     /* comctl32 version 6 tests start here */
1927     test_expandedimage();
1928     test_htreeitem_layout();
1929
1930     unload_v6_module(ctx_cookie, hCtx);
1931
1932     PostMessageA(hMainWnd, WM_CLOSE, 0, 0);
1933     while(GetMessageA(&msg, 0, 0, 0))
1934     {
1935         TranslateMessage(&msg);
1936         DispatchMessageA(&msg);
1937     }
1938 }