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