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