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