59036b6bcc63c4abc912480fb9366907d1c4340f
[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 /* invalid item pointer, nt4 crashes here but later versions just return 0 */
909 tviRoot.hItem = (HTREEITEM)0xdeadbeef;
910 tviRoot.mask = TVIF_STATE;
911 tviRoot.state = 0;
912 ret = SendMessageA(hTree2, TVM_GETITEMA, 0, (LPARAM)&tviRoot);
913 expect(FALSE, ret);
914
915 DestroyWindow(hTree);
916 DestroyWindow(hTree2);
917 }
918
919 static void test_get_set_itemheight(void)
920 {
921 int ulOldHeight = 0;
922 int ulNewHeight = 0;
923 HWND hTree;
924
925 hTree = create_treeview_control(0);
926 fill_tree(hTree);
927
928 flush_sequences(sequences, NUM_MSG_SEQUENCES);
929
930 /* Assuming default height to begin with */
931 ulOldHeight = SendMessageA(hTree, TVM_GETITEMHEIGHT, 0, 0);
932
933 /* Explicitly setting and getting the default height */
934 SendMessageA(hTree, TVM_SETITEMHEIGHT, -1, 0);
935 ulNewHeight = SendMessageA(hTree, TVM_GETITEMHEIGHT, 0, 0);
936 ok(ulNewHeight == ulOldHeight, "Default height not set properly, reported %d, expected %d\n", ulNewHeight, ulOldHeight);
937
938 /* Explicitly setting and getting the height of twice the normal */
939 SendMessageA(hTree, TVM_SETITEMHEIGHT, 2*ulOldHeight, 0);
940 ulNewHeight = SendMessageA(hTree, TVM_GETITEMHEIGHT, 0, 0);
941 ok(ulNewHeight == 2*ulOldHeight, "New height not set properly, reported %d, expected %d\n", ulNewHeight, 2*ulOldHeight);
942
943 /* Assuming tree doesn't have TVS_NONEVENHEIGHT set, so a set of 9 will round down to 8 */
944 SendMessageA(hTree, TVM_SETITEMHEIGHT, 9, 0);
945 ulNewHeight = SendMessageA(hTree, TVM_GETITEMHEIGHT, 0, 0);
946 ok(ulNewHeight == 8, "Uneven height not set properly, reported %d, expected %d\n", ulNewHeight, 8);
947
948 ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_itemheight_seq,
949 "test get set item height", FALSE);
950
951 /* without TVS_NONEVENHEIGHT */
952 SetWindowLongA(hTree, GWL_STYLE, GetWindowLongA(hTree, GWL_STYLE) & ~TVS_NONEVENHEIGHT);
953 /* odd value */
954 ulOldHeight = SendMessageA(hTree, TVM_SETITEMHEIGHT, 3, 0);
955 ok(ulOldHeight == 8, "got %d, expected %d\n", ulOldHeight, 8);
956 ulNewHeight = SendMessageA(hTree, TVM_GETITEMHEIGHT, 0, 0);
957 ok(ulNewHeight == 2, "got %d, expected %d\n", ulNewHeight, 2);
958
959 ulOldHeight = SendMessageA(hTree, TVM_SETITEMHEIGHT, 4, 0);
960 ok(ulOldHeight == 2, "got %d, expected %d\n", ulOldHeight, 2);
961 ulNewHeight = SendMessageA(hTree, TVM_GETITEMHEIGHT, 0, 0);
962 ok(ulNewHeight == 4, "got %d, expected %d\n", ulNewHeight, 4);
963
964 /* with TVS_NONEVENHEIGHT */
965 SetWindowLongA(hTree, GWL_STYLE, GetWindowLongA(hTree, GWL_STYLE) | TVS_NONEVENHEIGHT);
966 /* odd value */
967 ulOldHeight = SendMessageA(hTree, TVM_SETITEMHEIGHT, 3, 0);
968 ok(ulOldHeight == 4, "got %d, expected %d\n", ulOldHeight, 4);
969 ulNewHeight = SendMessageA(hTree, TVM_GETITEMHEIGHT, 0, 0);
970 ok(ulNewHeight == 3, "got %d, expected %d\n", ulNewHeight, 3);
971 /* even value */
972 ulOldHeight = SendMessageA(hTree, TVM_SETITEMHEIGHT, 10, 0);
973 ok(ulOldHeight == 3, "got %d, expected %d\n", ulOldHeight, 3);
974 ulNewHeight = SendMessageA(hTree, TVM_GETITEMHEIGHT, 0, 0);
975 ok(ulNewHeight == 10, "got %d, expected %d\n", ulNewHeight, 10);
976
977 DestroyWindow(hTree);
978 }
979
980 static void test_get_set_scrolltime(void)
981 {
982 int ulExpectedTime = 20;
983 int ulTime = 0;
984 HWND hTree;
985
986 hTree = create_treeview_control(0);
987 fill_tree(hTree);
988
989 flush_sequences(sequences, NUM_MSG_SEQUENCES);
990
991 SendMessageA(hTree, TVM_SETSCROLLTIME, ulExpectedTime, 0);
992 ulTime = SendMessageA(hTree, TVM_GETSCROLLTIME, 0, 0);
993 ok(ulTime == ulExpectedTime, "Scroll time reported as %d, expected %d\n", ulTime, ulExpectedTime);
994
995 ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_scrolltime_seq,
996 "test get set scroll time", FALSE);
997
998 DestroyWindow(hTree);
999 }
1000
1001 static void test_get_set_textcolor(void)
1002 {
1003 /* If the value is -1, the control is using the system color for the text color. */
1004 COLORREF crColor;
1005 HWND hTree;
1006
1007 hTree = create_treeview_control(0);
1008 fill_tree(hTree);
1009
1010 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1011
1012 crColor = SendMessageA(hTree, TVM_GETTEXTCOLOR, 0, 0);
1013 ok(crColor == ~0u, "Default text color reported as 0x%.8x\n", crColor);
1014
1015 /* Test for black text */
1016 SendMessageA(hTree, TVM_SETTEXTCOLOR, 0, RGB(0,0,0));
1017 crColor = SendMessageA(hTree, TVM_GETTEXTCOLOR, 0, 0);
1018 ok(crColor == RGB(0,0,0), "Black text color reported as 0x%.8x\n", crColor);
1019
1020 /* Test for white text */
1021 SendMessageA(hTree, TVM_SETTEXTCOLOR, 0, RGB(255,255,255));
1022 crColor = SendMessageA(hTree, TVM_GETTEXTCOLOR, 0, 0);
1023 ok(crColor == RGB(255,255,255), "White text color reported as 0x%.8x\n", crColor);
1024
1025 /* Reset the default text color */
1026 SendMessageA(hTree, TVM_SETTEXTCOLOR, 0, CLR_NONE);
1027
1028 ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_textcolor_seq,
1029 "test get set text color", FALSE);
1030
1031 DestroyWindow(hTree);
1032 }
1033
1034 static void test_get_set_tooltips(void)
1035 {
1036 HWND hwndLastToolTip = NULL;
1037 HWND hPopupTreeView;
1038 HWND hTree;
1039
1040 hTree = create_treeview_control(0);
1041 fill_tree(hTree);
1042
1043 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1044
1045 /* show even WS_POPUP treeview don't send NM_TOOLTIPSCREATED */
1046 hPopupTreeView = CreateWindowA(WC_TREEVIEWA, NULL, WS_POPUP|WS_VISIBLE, 0, 0, 100, 100,
1047 hMainWnd, NULL, NULL, NULL);
1048 DestroyWindow(hPopupTreeView);
1049
1050 /* Testing setting a NULL ToolTip */
1051 SendMessageA(hTree, TVM_SETTOOLTIPS, 0, 0);
1052 hwndLastToolTip = (HWND)SendMessageA(hTree, TVM_GETTOOLTIPS, 0, 0);
1053 ok(hwndLastToolTip == NULL, "NULL tool tip, reported as 0x%p, expected 0.\n", hwndLastToolTip);
1054
1055 ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_tooltips_seq,
1056 "test get set tooltips", TRUE);
1057
1058 /* TODO: Add a test of an actual tooltip */
1059 DestroyWindow(hTree);
1060 }
1061
1062 static void test_get_set_unicodeformat(void)
1063 {
1064 BOOL bPreviousSetting;
1065 BOOL bNewSetting;
1066 HWND hTree;
1067
1068 hTree = create_treeview_control(0);
1069 fill_tree(hTree);
1070
1071 /* Check that an invalid format returned by NF_QUERY defaults to ANSI */
1072 bPreviousSetting = SendMessageA(hTree, TVM_GETUNICODEFORMAT, 0, 0);
1073 ok(bPreviousSetting == FALSE, "Format should be ANSI.\n");
1074
1075 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1076
1077 /* Set to Unicode */
1078 bPreviousSetting = SendMessageA(hTree, TVM_SETUNICODEFORMAT, 1, 0);
1079 bNewSetting = SendMessageA(hTree, TVM_GETUNICODEFORMAT, 0, 0);
1080 ok(bNewSetting == TRUE, "Unicode setting did not work.\n");
1081
1082 /* Set to ANSI */
1083 SendMessageA(hTree, TVM_SETUNICODEFORMAT, 0, 0);
1084 bNewSetting = SendMessageA(hTree, TVM_GETUNICODEFORMAT, 0, 0);
1085 ok(bNewSetting == FALSE, "ANSI setting did not work.\n");
1086
1087 /* Revert to original setting */
1088 SendMessageA(hTree, TVM_SETUNICODEFORMAT, bPreviousSetting, 0);
1089
1090 ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_unicodeformat_seq,
1091 "test get set unicode format", FALSE);
1092
1093 DestroyWindow(hTree);
1094 }
1095
1096 static LRESULT CALLBACK parent_wnd_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1097 {
1098 static LONG defwndproc_counter = 0;
1099 struct message msg;
1100 LRESULT ret;
1101 RECT rect;
1102 HTREEITEM visibleItem;
1103
1104 msg.message = message;
1105 msg.flags = sent|wparam|lparam;
1106 if (defwndproc_counter) msg.flags |= defwinproc;
1107 msg.wParam = wParam;
1108 msg.lParam = lParam;
1109 if (message == WM_NOTIFY && lParam)
1110 msg.id = ((NMHDR*)lParam)->code;
1111 else
1112 msg.id = 0;
1113
1114 /* log system messages, except for painting */
1115 if (message < WM_USER &&
1116 message != WM_PAINT &&
1117 message != WM_ERASEBKGND &&
1118 message != WM_NCPAINT &&
1119 message != WM_NCHITTEST &&
1120 message != WM_GETTEXT &&
1121 message != WM_GETICON &&
1122 message != WM_DEVICECHANGE)
1123 {
1124 add_message(sequences, PARENT_SEQ_INDEX, &msg);
1125 }
1126
1127 switch(message) {
1128 case WM_NOTIFYFORMAT:
1129 {
1130 /* Make NF_QUERY return an invalid format to show that it defaults to ANSI */
1131 if (lParam == NF_QUERY) return 0;
1132 break;
1133 }
1134
1135 case WM_NOTIFY:
1136 {
1137 NMHDR *pHdr = (NMHDR *)lParam;
1138
1139 ok(pHdr->code != NM_TOOLTIPSCREATED, "Treeview should not send NM_TOOLTIPSCREATED\n");
1140 if (pHdr->idFrom == 100)
1141 {
1142 NMTREEVIEWA *pTreeView = (LPNMTREEVIEWA) lParam;
1143 switch(pHdr->code)
1144 {
1145 case TVN_SELCHANGINGA:
1146 AddItem('(');
1147 IdentifyItem(pTreeView->itemOld.hItem);
1148 IdentifyItem(pTreeView->itemNew.hItem);
1149 break;
1150 case TVN_SELCHANGEDA:
1151 AddItem(')');
1152 IdentifyItem(pTreeView->itemOld.hItem);
1153 IdentifyItem(pTreeView->itemNew.hItem);
1154 break;
1155 case TVN_GETDISPINFOA: {
1156 NMTVDISPINFOA *disp = (NMTVDISPINFOA *)lParam;
1157 if (disp->item.mask & TVIF_TEXT) {
1158 lstrcpynA(disp->item.pszText, TEST_CALLBACK_TEXT, disp->item.cchTextMax);
1159 }
1160
1161 if (g_disp_A_to_W && (disp->item.mask & TVIF_TEXT)) {
1162 static const WCHAR testW[] = {'T','E','S','T','2',0};
1163
1164 disp->hdr.code = TVN_GETDISPINFOW;
1165 memcpy(disp->item.pszText, testW, sizeof(testW));
1166 }
1167
1168 if (g_disp_set_stateimage)
1169 {
1170 ok(disp->item.mask == TVIF_IMAGE, "got %x\n", disp->item.mask);
1171 /* both masks set here are necessary to change state bits */
1172 disp->item.mask |= TVIF_STATE;
1173 disp->item.state = TVIS_SELECTED | INDEXTOSTATEIMAGEMASK(2) | INDEXTOOVERLAYMASK(3);
1174 disp->item.stateMask = TVIS_SELECTED | TVIS_OVERLAYMASK | TVIS_STATEIMAGEMASK;
1175 }
1176
1177 break;
1178 }
1179 case TVN_BEGINLABELEDITA:
1180 {
1181 if (g_beginedit_alter_text)
1182 {
1183 static const char* textA = "<edittextaltered>";
1184 HWND edit;
1185
1186 edit = (HWND)SendMessageA(pHdr->hwndFrom, TVM_GETEDITCONTROL, 0, 0);
1187 ok(IsWindow(edit), "failed to get edit handle\n");
1188 SetWindowTextA(edit, textA);
1189 }
1190
1191 break;
1192 }
1193
1194 case TVN_ENDLABELEDITA: return TRUE;
1195 case TVN_ITEMEXPANDINGA:
1196 {
1197 UINT newmask = pTreeView->itemNew.mask & ~TVIF_CHILDREN;
1198 ok(newmask ==
1199 (TVIF_HANDLE | TVIF_SELECTEDIMAGE | TVIF_IMAGE | TVIF_PARAM | TVIF_STATE),
1200 "got wrong mask %x\n", pTreeView->itemNew.mask);
1201 ok(pTreeView->itemOld.mask == 0,
1202 "got wrong mask %x\n", pTreeView->itemOld.mask);
1203
1204 if (g_get_from_expand)
1205 {
1206 g_item_expanding.mask = TVIF_STATE;
1207 g_item_expanding.hItem = hRoot;
1208 ret = SendMessageA(pHdr->hwndFrom, TVM_GETITEMA, 0, (LPARAM)&g_item_expanding);
1209 ok(ret == TRUE, "got %lu\n", ret);
1210 }
1211 break;
1212 }
1213 case TVN_ITEMEXPANDEDA:
1214 ok(pTreeView->itemNew.mask & TVIF_STATE, "got wrong mask %x\n", pTreeView->itemNew.mask);
1215 ok(pTreeView->itemNew.state & (TVIS_EXPANDED|TVIS_EXPANDEDONCE),
1216 "got wrong mask %x\n", pTreeView->itemNew.mask);
1217 ok(pTreeView->itemOld.mask == 0,
1218 "got wrong mask %x\n", pTreeView->itemOld.mask);
1219
1220 if (g_get_from_expand)
1221 {
1222 g_item_expanded.mask = TVIF_STATE;
1223 g_item_expanded.hItem = hRoot;
1224 ret = SendMessageA(pHdr->hwndFrom, TVM_GETITEMA, 0, (LPARAM)&g_item_expanded);
1225 ok(ret == TRUE, "got %lu\n", ret);
1226 }
1227 if (g_get_rect_in_expand)
1228 {
1229 visibleItem = (HTREEITEM)SendMessageA(pHdr->hwndFrom, TVM_GETNEXTITEM,
1230 TVGN_FIRSTVISIBLE, 0);
1231 ok(pTreeView->itemNew.hItem == visibleItem, "expanded item == first visible item\n");
1232 *(HTREEITEM*)&rect = visibleItem;
1233 ok(SendMessageA(pHdr->hwndFrom, TVM_GETITEMRECT, TRUE, (LPARAM)&rect),
1234 "Failed to get rect for first visible item.\n");
1235 visibleItem = (HTREEITEM)SendMessageA(pHdr->hwndFrom, TVM_GETNEXTITEM,
1236 TVGN_NEXTVISIBLE, (LPARAM)visibleItem);
1237 *(HTREEITEM*)&rect = visibleItem;
1238 ok(visibleItem != NULL, "There must be a visible item after the first visisble item.\n");
1239 ok(SendMessageA(pHdr->hwndFrom, TVM_GETITEMRECT, TRUE, (LPARAM)&rect),
1240 "Failed to get rect for second visible item.\n");
1241 }
1242 break;
1243 case TVN_DELETEITEMA:
1244 {
1245 struct message item;
1246
1247 ok(pTreeView->itemNew.mask == 0, "got wrong mask 0x%x\n", pTreeView->itemNew.mask);
1248
1249 ok(pTreeView->itemOld.mask == (TVIF_HANDLE | TVIF_PARAM), "got wrong mask 0x%x\n", pTreeView->itemOld.mask);
1250 ok(pTreeView->itemOld.hItem != NULL, "got %p\n", pTreeView->itemOld.hItem);
1251
1252 memset(&item, 0, sizeof(item));
1253 item.lParam = (LPARAM)pTreeView->itemOld.hItem;
1254 add_message(item_sequence, 0, &item);
1255
1256 break;
1257 }
1258 case NM_CUSTOMDRAW:
1259 {
1260 NMTVCUSTOMDRAW *nmcd = (NMTVCUSTOMDRAW*)lParam;
1261 COLORREF c0ffee = RGB(0xc0,0xff,0xee), cafe = RGB(0xca,0xfe,0x00);
1262
1263 msg.flags |= custdraw;
1264 msg.stage = nmcd->nmcd.dwDrawStage;
1265 add_message(sequences, PARENT_CD_SEQ_INDEX, &msg);
1266
1267 switch (msg.stage)
1268 {
1269 case CDDS_PREPAINT:
1270 return CDRF_NOTIFYITEMDRAW|CDRF_NOTIFYITEMERASE|CDRF_NOTIFYPOSTPAINT;
1271 case CDDS_ITEMPREPAINT:
1272 nmcd->clrTextBk = c0ffee;
1273 nmcd->clrText = cafe;
1274 if (g_customdraw_font)
1275 SelectObject(nmcd->nmcd.hdc, g_customdraw_font);
1276 return CDRF_NOTIFYPOSTPAINT|CDRF_NEWFONT;
1277 case CDDS_ITEMPOSTPAINT:
1278 /* at the point of post paint notification colors are already restored */
1279 ok(GetTextColor(nmcd->nmcd.hdc) != cafe, "got 0%x\n", GetTextColor(nmcd->nmcd.hdc));
1280 ok(GetBkColor(nmcd->nmcd.hdc) != c0ffee, "got 0%x\n", GetBkColor(nmcd->nmcd.hdc));
1281 if (g_customdraw_font)
1282 ok(GetCurrentObject(nmcd->nmcd.hdc, OBJ_FONT) != g_customdraw_font, "got %p\n",
1283 GetCurrentObject(nmcd->nmcd.hdc, OBJ_FONT));
1284 break;
1285 default:
1286 ;
1287 }
1288 break;
1289 }
1290 }
1291 }
1292 break;
1293 }
1294
1295 case WM_DESTROY:
1296 PostQuitMessage(0);
1297 break;
1298 }
1299
1300 defwndproc_counter++;
1301 ret = DefWindowProcA(hWnd, message, wParam, lParam);
1302 defwndproc_counter--;
1303
1304 return ret;
1305 }
1306
1307 static void test_expandinvisible(void)
1308 {
1309 static CHAR nodeText[][5] = {"0", "1", "2", "3", "4"};
1310 TVINSERTSTRUCTA ins;
1311 HTREEITEM node[5];
1312 RECT dummyRect;
1313 BOOL nodeVisible;
1314 LRESULT ret;
1315 HWND hTree;
1316
1317 hTree = create_treeview_control(0);
1318
1319 /* The test builds the following tree and expands node 1, while node 0 is collapsed.
1320 *
1321 * 0
1322 * |- 1
1323 * | |- 2
1324 * | |- 3
1325 * |- 4
1326 *
1327 */
1328
1329 ret = SendMessageA(hTree, TVM_DELETEITEM, 0, (LPARAM)TVI_ROOT);
1330 ok(ret == TRUE, "ret\n");
1331 ins.hParent = TVI_ROOT;
1332 ins.hInsertAfter = TVI_ROOT;
1333 U(ins).item.mask = TVIF_TEXT;
1334 U(ins).item.pszText = nodeText[0];
1335 node[0] = TreeView_InsertItemA(hTree, &ins);
1336 ok(node[0] != NULL, "failed to set node[0]\n");
1337
1338 ins.hInsertAfter = TVI_LAST;
1339 U(ins).item.mask = TVIF_TEXT;
1340 ins.hParent = node[0];
1341
1342 U(ins).item.pszText = nodeText[1];
1343 node[1] = TreeView_InsertItemA(hTree, &ins);
1344 ok(node[1] != NULL, "failed to set node[1]\n");
1345 U(ins).item.pszText = nodeText[4];
1346 node[4] = TreeView_InsertItemA(hTree, &ins);
1347 ok(node[4] != NULL, "failed to set node[4]\n");
1348
1349 ins.hParent = node[1];
1350
1351 U(ins).item.pszText = nodeText[2];
1352 node[2] = TreeView_InsertItemA(hTree, &ins);
1353 ok(node[2] != NULL, "failed to set node[2]\n");
1354 U(ins).item.pszText = nodeText[3];
1355 node[3] = TreeView_InsertItemA(hTree, &ins);
1356 ok(node[3] != NULL, "failed to set node[3]\n");
1357
1358 *(HTREEITEM *)&dummyRect = node[1];
1359 nodeVisible = SendMessageA(hTree, TVM_GETITEMRECT, FALSE, (LPARAM)&dummyRect);
1360 ok(!nodeVisible, "Node 1 should not be visible.\n");
1361 *(HTREEITEM *)&dummyRect = node[2];
1362 nodeVisible = SendMessageA(hTree, TVM_GETITEMRECT, FALSE, (LPARAM)&dummyRect);
1363 ok(!nodeVisible, "Node 2 should not be visible.\n");
1364 *(HTREEITEM *)&dummyRect = node[3];
1365 nodeVisible = SendMessageA(hTree, TVM_GETITEMRECT, FALSE, (LPARAM)&dummyRect);
1366 ok(!nodeVisible, "Node 3 should not be visible.\n");
1367 *(HTREEITEM *)&dummyRect = node[4];
1368 nodeVisible = SendMessageA(hTree, TVM_GETITEMRECT, FALSE, (LPARAM)&dummyRect);
1369 ok(!nodeVisible, "Node 4 should not be visible.\n");
1370
1371 ok(SendMessageA(hTree, TVM_EXPAND, TVE_EXPAND, (LPARAM)node[1]), "Expand of node 1 failed.\n");
1372
1373 *(HTREEITEM *)&dummyRect = node[1];
1374 nodeVisible = SendMessageA(hTree, TVM_GETITEMRECT, FALSE, (LPARAM)&dummyRect);
1375 ok(!nodeVisible, "Node 1 should not be visible.\n");
1376 *(HTREEITEM *)&dummyRect = node[2];
1377 nodeVisible = SendMessageA(hTree, TVM_GETITEMRECT, FALSE, (LPARAM)&dummyRect);
1378 ok(!nodeVisible, "Node 2 should not be visible.\n");
1379 *(HTREEITEM *)&dummyRect = node[3];
1380 nodeVisible = SendMessageA(hTree, TVM_GETITEMRECT, FALSE, (LPARAM)&dummyRect);
1381 ok(!nodeVisible, "Node 3 should not be visible.\n");
1382 *(HTREEITEM *)&dummyRect = node[4];
1383 nodeVisible = SendMessageA(hTree, TVM_GETITEMRECT, FALSE, (LPARAM)&dummyRect);
1384 ok(!nodeVisible, "Node 4 should not be visible.\n");
1385
1386 DestroyWindow(hTree);
1387 }
1388
1389 static void test_itemedit(void)
1390 {
1391 DWORD r;
1392 HWND edit;
1393 TVITEMA item;
1394 CHAR buffA[20];
1395 HWND hTree;
1396
1397 hTree = create_treeview_control(0);
1398 fill_tree(hTree);
1399
1400 /* try with null item */
1401 edit = (HWND)SendMessageA(hTree, TVM_EDITLABELA, 0, 0);
1402 ok(!IsWindow(edit), "Expected valid handle\n");
1403
1404 /* trigger edit */
1405 edit = (HWND)SendMessageA(hTree, TVM_EDITLABELA, 0, (LPARAM)hRoot);
1406 ok(IsWindow(edit), "Expected valid handle\n");
1407 /* item shouldn't be selected automatically after TVM_EDITLABELA */
1408 r = SendMessageA(hTree, TVM_GETITEMSTATE, (WPARAM)hRoot, TVIS_SELECTED);
1409 expect(0, r);
1410 /* try to cancel with wrong edit handle */
1411 r = SendMessageA(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), 0);
1412 expect(0, r);
1413 ok(IsWindow(edit), "Expected edit control to be valid\n");
1414 r = SendMessageA(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)edit);
1415 expect(0, r);
1416 ok(!IsWindow(edit), "Expected edit control to be destroyed\n");
1417 /* try to cancel without creating edit */
1418 r = SendMessageA(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), 0);
1419 expect(0, r);
1420
1421 /* try to cancel with wrong (not null) handle */
1422 edit = (HWND)SendMessageA(hTree, TVM_EDITLABELA, 0, (LPARAM)hRoot);
1423 ok(IsWindow(edit), "Expected valid handle\n");
1424 r = SendMessageA(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)hTree);
1425 expect(0, r);
1426 ok(IsWindow(edit), "Expected edit control to be valid\n");
1427 r = SendMessageA(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)edit);
1428 expect(0, r);
1429
1430 /* remove selection after starting edit */
1431 r = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hRoot);
1432 expect(TRUE, r);
1433 edit = (HWND)SendMessageA(hTree, TVM_EDITLABELA, 0, (LPARAM)hRoot);
1434 ok(IsWindow(edit), "Expected valid handle\n");
1435 r = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, 0);
1436 expect(TRUE, r);
1437 /* alter text */
1438 strcpy(buffA, "x");
1439 r = SendMessageA(edit, WM_SETTEXT, 0, (LPARAM)buffA);
1440 expect(TRUE, r);
1441 r = SendMessageA(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)edit);
1442 expect(0, r);
1443 ok(!IsWindow(edit), "Expected edit control to be destroyed\n");
1444 /* check that text is saved */
1445 item.mask = TVIF_TEXT;
1446 item.hItem = hRoot;
1447 item.pszText = buffA;
1448 item.cchTextMax = sizeof(buffA)/sizeof(CHAR);
1449 r = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1450 expect(TRUE, r);
1451 ok(!strcmp("x", buffA), "Expected item text to change\n");
1452
1453 /* try A/W messages */
1454 edit = (HWND)SendMessageA(hTree, TVM_EDITLABELA, 0, (LPARAM)hRoot);
1455 ok(IsWindow(edit), "Expected valid handle\n");
1456 ok(IsWindowUnicode(edit), "got ansi window\n");
1457 r = SendMessageA(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)edit);
1458 expect(0, r);
1459 ok(!IsWindow(edit), "expected invalid handle\n");
1460
1461 edit = (HWND)SendMessageA(hTree, TVM_EDITLABELW, 0, (LPARAM)hRoot);
1462 ok(IsWindow(edit), "Expected valid handle\n");
1463 ok(IsWindowUnicode(edit), "got ansi window\n");
1464 r = SendMessageA(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)edit);
1465 expect(0, r);
1466
1467 /* alter text during TVM_BEGINLABELEDIT, check that it's preserved */
1468 strcpy(buffA, "<root>");
1469
1470 item.mask = TVIF_TEXT;
1471 item.hItem = hRoot;
1472 item.pszText = buffA;
1473 item.cchTextMax = 0;
1474 r = SendMessageA(hTree, TVM_SETITEMA, 0, (LPARAM)&item);
1475 expect(TRUE, r);
1476
1477 g_beginedit_alter_text = TRUE;
1478 edit = (HWND)SendMessageA(hTree, TVM_EDITLABELA, 0, (LPARAM)hRoot);
1479 ok(IsWindow(edit), "Expected valid handle\n");
1480 g_beginedit_alter_text = FALSE;
1481
1482 GetWindowTextA(edit, buffA, sizeof(buffA)/sizeof(CHAR));
1483 ok(!strcmp(buffA, "<edittextaltered>"), "got string %s\n", buffA);
1484
1485 DestroyWindow(hTree);
1486 }
1487
1488 static void test_treeview_classinfo(void)
1489 {
1490 WNDCLASSA cls;
1491
1492 memset(&cls, 0, sizeof(cls));
1493 GetClassInfoA(GetModuleHandleA("comctl32.dll"), WC_TREEVIEWA, &cls);
1494 ok(cls.hbrBackground == NULL, "Expected NULL background brush, got %p\n", cls.hbrBackground);
1495 ok(cls.style == (CS_GLOBALCLASS | CS_DBLCLKS), "Expected got %x\n", cls.style);
1496 expect(0, cls.cbClsExtra);
1497 }
1498
1499 static void test_get_linecolor(void)
1500 {
1501 COLORREF clr;
1502 HWND hTree;
1503
1504 hTree = create_treeview_control(0);
1505
1506 /* newly created control has default color */
1507 clr = SendMessageA(hTree, TVM_GETLINECOLOR, 0, 0);
1508 if (clr == 0)
1509 win_skip("TVM_GETLINECOLOR is not supported on comctl32 < 5.80\n");
1510 else
1511 expect(CLR_DEFAULT, clr);
1512
1513 DestroyWindow(hTree);
1514 }
1515
1516 static void test_get_insertmarkcolor(void)
1517 {
1518 COLORREF clr;
1519 HWND hTree;
1520
1521 hTree = create_treeview_control(0);
1522
1523 /* newly created control has default color */
1524 clr = SendMessageA(hTree, TVM_GETINSERTMARKCOLOR, 0, 0);
1525 if (clr == 0)
1526 win_skip("TVM_GETINSERTMARKCOLOR is not supported on comctl32 < 5.80\n");
1527 else
1528 expect(CLR_DEFAULT, clr);
1529
1530 DestroyWindow(hTree);
1531 }
1532
1533 static void test_expandnotify(void)
1534 {
1535 HTREEITEM hitem;
1536 HWND hTree;
1537 BOOL ret;
1538 TVITEMA item;
1539
1540 hTree = create_treeview_control(0);
1541 fill_tree(hTree);
1542
1543 item.hItem = hRoot;
1544 item.mask = TVIF_STATE;
1545
1546 item.state = TVIS_EXPANDED;
1547 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1548 expect(TRUE, ret);
1549 ok((item.state & TVIS_EXPANDED) == 0, "expected collapsed\n");
1550
1551 /* preselect root node here */
1552 ret = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hRoot);
1553 expect(TRUE, ret);
1554
1555 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1556 ret = SendMessageA(hTree, TVM_EXPAND, TVE_COLLAPSE, (LPARAM)hRoot);
1557 expect(FALSE, ret);
1558 ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq, "no collapse notifications", FALSE);
1559
1560 g_get_from_expand = TRUE;
1561 /* expand */
1562 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1563 g_item_expanding.state = 0xdeadbeef;
1564 g_item_expanded.state = 0xdeadbeef;
1565 ret = SendMessageA(hTree, TVM_EXPAND, TVE_EXPAND, (LPARAM)hRoot);
1566 expect(TRUE, ret);
1567 ok(g_item_expanding.state == TVIS_SELECTED, "got state on TVN_ITEMEXPANDING 0x%08x\n",
1568 g_item_expanding.state);
1569 ok(g_item_expanded.state == (TVIS_SELECTED|TVIS_EXPANDED), "got state on TVN_ITEMEXPANDED 0x%08x\n",
1570 g_item_expanded.state);
1571 ok_sequence(sequences, PARENT_SEQ_INDEX, parent_expand_seq, "expand notifications", FALSE);
1572 g_get_from_expand = FALSE;
1573
1574 /* check that it's expanded */
1575 item.state = TVIS_EXPANDED;
1576 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1577 expect(TRUE, ret);
1578 ok((item.state & TVIS_EXPANDED) == TVIS_EXPANDED, "expected expanded\n");
1579
1580 /* collapse */
1581 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1582 ret = SendMessageA(hTree, TVM_EXPAND, TVE_COLLAPSE, (LPARAM)hRoot);
1583 expect(TRUE, ret);
1584 item.state = TVIS_EXPANDED;
1585 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1586 expect(TRUE, ret);
1587 ok((item.state & TVIS_EXPANDED) == 0, "expected collapsed\n");
1588 /* all further collapse/expand attempts won't produce any notifications,
1589 the only way is to reset with all children removed */
1590 ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq, "collapse after expand notifications", FALSE);
1591
1592 /* try to toggle child that doesn't have children itself */
1593 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1594 ret = SendMessageA(hTree, TVM_EXPAND, TVE_TOGGLE, (LPARAM)hChild);
1595 expect(FALSE, ret);
1596 ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq, "toggle node without children", FALSE);
1597
1598 DestroyWindow(hTree);
1599
1600 /* test TVM_GETITEMRECT inside TVN_ITEMEXPANDED notification */
1601 hTree = create_treeview_control(0);
1602 fill_tree(hTree);
1603 g_get_rect_in_expand = TRUE;
1604 ret = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hChild);
1605 expect(TRUE, ret);
1606 g_get_rect_in_expand = FALSE;
1607
1608 DestroyWindow(hTree);
1609
1610 /* TVE_TOGGLE acts as any other TVM_EXPAND */
1611 hTree = create_treeview_control(0);
1612 fill_tree(hTree);
1613
1614 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1615 ret = SendMessageA(hTree, TVM_EXPAND, TVE_TOGGLE, (LPARAM)hRoot);
1616 expect(TRUE, ret);
1617 ok_sequence(sequences, PARENT_SEQ_INDEX, parent_expand_seq, "toggle node (expand)", FALSE);
1618
1619 /* toggle again - no notifications */
1620 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1621 ret = SendMessageA(hTree, TVM_EXPAND, TVE_TOGGLE, (LPARAM)hRoot);
1622 expect(TRUE, ret);
1623 ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq, "toggle node (collapse)", FALSE);
1624
1625 DestroyWindow(hTree);
1626
1627 /* some keyboard events are also translated to expand */
1628 hTree = create_treeview_control(0);
1629 fill_tree(hTree);
1630
1631 /* preselect root node here */
1632 ret = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hRoot);
1633 expect(TRUE, ret);
1634
1635 g_get_from_expand = TRUE;
1636 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1637 ret = SendMessageA(hTree, WM_KEYDOWN, VK_ADD, 0);
1638 expect(FALSE, ret);
1639 ok_sequence(sequences, PARENT_SEQ_INDEX, parent_expand_kb_seq, "expand node", FALSE);
1640 ok(g_item_expanding.state == TVIS_SELECTED, "got state on TVN_ITEMEXPANDING 0x%08x\n",
1641 g_item_expanding.state);
1642 ok(g_item_expanded.state == (TVIS_SELECTED|TVIS_EXPANDED), "got state on TVN_ITEMEXPANDED 0x%08x\n",
1643 g_item_expanded.state);
1644
1645 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1646 ret = SendMessageA(hTree, WM_KEYDOWN, VK_ADD, 0);
1647 expect(FALSE, ret);
1648 ok_sequence(sequences, PARENT_SEQ_INDEX, parent_expand_kb_seq, "expand node again", FALSE);
1649 ok(g_item_expanding.state == (TVIS_SELECTED|TVIS_EXPANDED|TVIS_EXPANDEDONCE), "got state on TVN_ITEMEXPANDING 0x%08x\n",
1650 g_item_expanding.state);
1651 ok(g_item_expanded.state == (TVIS_SELECTED|TVIS_EXPANDED|TVIS_EXPANDEDONCE), "got state on TVN_ITEMEXPANDED 0x%08x\n",
1652 g_item_expanded.state);
1653
1654 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1655 ret = SendMessageA(hTree, WM_KEYDOWN, VK_SUBTRACT, 0);
1656 expect(FALSE, ret);
1657 ok_sequence(sequences, PARENT_SEQ_INDEX, parent_expand_kb_seq, "collapse node", FALSE);
1658 ok(g_item_expanding.state == (TVIS_SELECTED|TVIS_EXPANDED|TVIS_EXPANDEDONCE), "got state on TVN_ITEMEXPANDING 0x%08x\n",
1659 g_item_expanding.state);
1660 ok(g_item_expanded.state == (TVIS_SELECTED|TVIS_EXPANDEDONCE), "got state on TVN_ITEMEXPANDED 0x%08x\n",
1661 g_item_expanded.state);
1662
1663 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1664 ret = SendMessageA(hTree, WM_KEYDOWN, VK_SUBTRACT, 0);
1665 expect(FALSE, ret);
1666 ok_sequence(sequences, PARENT_SEQ_INDEX, parent_collapse_2nd_kb_seq, "collapse node again", FALSE);
1667 ok(g_item_expanding.state == (TVIS_SELECTED|TVIS_EXPANDEDONCE), "got state on TVN_ITEMEXPANDING 0x%08x\n",
1668 g_item_expanding.state);
1669 g_get_from_expand = FALSE;
1670
1671 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1672 ret = SendMessageA(hTree, WM_KEYDOWN, VK_ADD, 0);
1673 expect(FALSE, ret);
1674 ok_sequence(sequences, PARENT_SEQ_INDEX, parent_expand_kb_seq, "expand node", FALSE);
1675
1676 /* go to child */
1677 ret = SendMessageA(hTree, WM_KEYDOWN, VK_RIGHT, 0);
1678 expect(FALSE, ret);
1679
1680 /* try to expand child that doesn't have children itself */
1681 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1682 ret = SendMessageA(hTree, WM_KEYDOWN, VK_ADD, 0);
1683 expect(FALSE, ret);
1684 ok_sequence(sequences, PARENT_SEQ_INDEX, parent_expand_empty_kb_seq, "expand node with no children", FALSE);
1685
1686 /* stay on current selection and set non-zero children count */
1687 hitem = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_CARET, 0);
1688 ok(hitem != NULL, "got %p\n", hitem);
1689
1690 item.hItem = hitem;
1691 item.mask = TVIF_CHILDREN;
1692 item.cChildren = 0x80000000;
1693
1694 ret = SendMessageA(hTree, TVM_SETITEMA, 0, (LPARAM)&item);
1695 expect(TRUE, ret);
1696
1697 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1698 ret = SendMessageA(hTree, WM_KEYDOWN, VK_ADD, 0);
1699 expect(FALSE, ret);
1700 ok_sequence(sequences, PARENT_SEQ_INDEX, parent_collapse_2nd_kb_seq, "expand node with children", FALSE);
1701
1702 DestroyWindow(hTree);
1703 }
1704
1705 static void test_expandedimage(void)
1706 {
1707 TVITEMEXA item;
1708 HWND hTree;
1709 BOOL ret;
1710
1711 hTree = create_treeview_control(0);
1712 fill_tree(hTree);
1713
1714 item.mask = TVIF_EXPANDEDIMAGE;
1715 item.iExpandedImage = 1;
1716 item.hItem = hRoot;
1717 ret = SendMessageA(hTree, TVM_SETITEMA, 0, (LPARAM)&item);
1718 ok(ret, "got %d\n", ret);
1719
1720 item.mask = TVIF_EXPANDEDIMAGE;
1721 item.iExpandedImage = -1;
1722 item.hItem = hRoot;
1723 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1724 ok(ret, "got %d\n", ret);
1725
1726 if (item.iExpandedImage != 1)
1727 {
1728 win_skip("TVIF_EXPANDEDIMAGE not supported\n");
1729 DestroyWindow(hTree);
1730 return;
1731 }
1732
1733 /* test for default iExpandedImage value */
1734 item.mask = TVIF_EXPANDEDIMAGE;
1735 item.iExpandedImage = -1;
1736 item.hItem = hChild;
1737 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1738 ok(ret, "got %d\n", ret);
1739 ok(item.iExpandedImage == (WORD)I_IMAGENONE, "got %d\n", item.iExpandedImage);
1740
1741 DestroyWindow(hTree);
1742 }
1743
1744 static void test_TVS_SINGLEEXPAND(void)
1745 {
1746 HWND hTree;
1747 HTREEITEM alpha, bravo, charlie, delta, echo, foxtrot, golf, hotel, india, juliet;
1748 TVINSERTSTRUCTA ins;
1749 char foo[] = "foo";
1750 char context[32];
1751 int i;
1752 BOOL ret;
1753
1754 /* build a fairly complex tree
1755 * - TVI_ROOT
1756 * - alpha
1757 * - bravo
1758 * - charlie
1759 * - delta
1760 * - echo
1761 * - foxtrot
1762 * - golf
1763 * - hotel
1764 * - india
1765 * - juliet
1766 */
1767 struct
1768 {
1769 HTREEITEM *handle;
1770 HTREEITEM *parent;
1771 UINT final_state;
1772 }
1773 items[] =
1774 {
1775 { &alpha, NULL, TVIS_EXPANDEDONCE },
1776 { &bravo, &alpha, TVIS_EXPANDEDONCE },
1777 { &charlie, &bravo, 0 },
1778 { &delta, &alpha, TVIS_EXPANDEDONCE },
1779 { &echo, &delta, 0 },
1780 { &foxtrot, NULL, TVIS_EXPANDEDONCE|TVIS_EXPANDED },
1781 { &golf, &foxtrot, TVIS_EXPANDEDONCE|TVIS_EXPANDED },
1782 { &hotel, &golf, 0 },
1783 { &india, &golf, TVIS_SELECTED },
1784 { &juliet, &foxtrot, 0 }
1785 };
1786
1787 struct
1788 {
1789 HTREEITEM *select;
1790 const struct message *sequence;
1791 }
1792 sequence_tests[] =
1793 {
1794 { &alpha, parent_singleexpand_seq0 },
1795 { &bravo, parent_singleexpand_seq1 },
1796 { &delta, parent_singleexpand_seq2 },
1797 { &foxtrot, parent_singleexpand_seq3 },
1798 { &alpha, parent_singleexpand_seq4 },
1799 { &golf, parent_singleexpand_seq5 },
1800 { &hotel, parent_singleexpand_seq6 },
1801 { &india, parent_singleexpand_seq7 },
1802 { &india, empty_seq }
1803 };
1804
1805 hTree = create_treeview_control(0);
1806 SetWindowLongA(hTree, GWL_STYLE, GetWindowLongA(hTree, GWL_STYLE) | TVS_SINGLEEXPAND);
1807 /* to avoid painting related notifications */
1808 ShowWindow(hTree, SW_HIDE);
1809 for (i = 0; i < sizeof(items)/sizeof(items[0]); i++)
1810 {
1811 ins.hParent = items[i].parent ? *items[i].parent : TVI_ROOT;
1812 ins.hInsertAfter = TVI_FIRST;
1813 U(ins).item.mask = TVIF_TEXT;
1814 U(ins).item.pszText = foo;
1815 *items[i].handle = TreeView_InsertItemA(hTree, &ins);
1816 }
1817
1818 for (i = 0; i < sizeof(sequence_tests)/sizeof(sequence_tests[0]); i++)
1819 {
1820 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1821 ret = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)(*sequence_tests[i].select));
1822 ok(ret, "got %d\n", ret);
1823 sprintf(context, "singleexpand notifications %d", i);
1824 ok_sequence(sequences, PARENT_SEQ_INDEX, sequence_tests[i].sequence, context, FALSE);
1825 }
1826
1827 for (i = 0; i < sizeof(items)/sizeof(items[0]); i++)
1828 {
1829 ret = SendMessageA(hTree, TVM_GETITEMSTATE, (WPARAM)(*items[i].handle), 0xFFFF);
1830 ok(ret == items[i].final_state, "singleexpand items[%d]: expected state 0x%x got 0x%x\n",
1831 i, items[i].final_state, ret);
1832 }
1833
1834 /* a workaround for NT4 that sends expand notifications when nothing is about to expand */
1835 ret = SendMessageA(hTree, TVM_DELETEITEM, 0, (LPARAM)TVI_ROOT);
1836 ok(ret, "got %d\n", ret);
1837 fill_tree(hTree);
1838 ret = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, 0);
1839 ok(ret, "got %d\n", ret);
1840
1841 DestroyWindow(hTree);
1842 }
1843
1844 static void test_WM_PAINT(void)
1845 {
1846 HWND hTree;
1847 COLORREF clr;
1848 LONG ret;
1849 RECT rc;
1850 HDC hdc;
1851
1852 hTree = create_treeview_control(0);
1853
1854 clr = SendMessageA(hTree, TVM_SETBKCOLOR, 0, RGB(255, 0, 0));
1855 ok(clr == ~0u, "got %d, expected -1\n", clr);
1856
1857 hdc = GetDC(hMainWnd);
1858
1859 GetClientRect(hMainWnd, &rc);
1860 FillRect(hdc, &rc, GetStockObject(BLACK_BRUSH));
1861
1862 clr = GetPixel(hdc, 1, 1);
1863 ok(clr == RGB(0, 0, 0), "got 0x%x\n", clr);
1864
1865 ret = SendMessageA(hTree, WM_PAINT, (WPARAM)hdc, 0);
1866 ok(ret == 0, "got %d\n", ret);
1867
1868 clr = GetPixel(hdc, 1, 1);
1869 ok(clr == RGB(255, 0, 0) || broken(clr == RGB(0, 0, 0)) /* win98 */,
1870 "got 0x%x\n", clr);
1871
1872 ReleaseDC(hMainWnd, hdc);
1873
1874 DestroyWindow(hTree);
1875 }
1876
1877 static void test_delete_items(void)
1878 {
1879 const struct message *msg;
1880 HWND hTree;
1881 HTREEITEM hItem1, hItem2;
1882 TVINSERTSTRUCTA ins;
1883 INT ret;
1884
1885 static CHAR item1[] = "Item 1";
1886 static CHAR item2[] = "Item 2";
1887
1888 hTree = create_treeview_control(0);
1889 fill_tree(hTree);
1890
1891 /* check delete order */
1892 flush_sequences(item_sequence, 1);
1893 ret = SendMessageA(hTree, TVM_DELETEITEM, 0, 0);
1894 ok(ret == TRUE, "got %d\n", ret);
1895
1896 msg = item_sequence[0]->sequence;
1897 ok(item_sequence[0]->count == 2, "expected 2 items, got %d\n", item_sequence[0]->count);
1898
1899 if (item_sequence[0]->count == 2)
1900 {
1901 ok(msg[0].lParam == (LPARAM)hChild, "expected %p, got 0x%lx\n", hChild, msg[0].lParam);
1902 ok(msg[1].lParam == (LPARAM)hRoot, "expected %p, got 0x%lx\n", hRoot, msg[1].lParam);
1903 }
1904
1905 ret = SendMessageA(hTree, TVM_GETCOUNT, 0, 0);
1906 ok(ret == 0, "got %d\n", ret);
1907
1908 DestroyWindow(hTree);
1909
1910 /* Regression test for a crash when deleting the first visible item while bRedraw == false. */
1911 hTree = create_treeview_control(0);
1912
1913 ret = SendMessageA(hTree, WM_SETREDRAW, FALSE, 0);
1914 ok(ret == 0, "got %d\n", ret);
1915
1916 ins.hParent = TVI_ROOT;
1917 ins.hInsertAfter = TVI_ROOT;
1918 U(ins).item.mask = TVIF_TEXT;
1919 U(ins).item.pszText = item1;
1920 hItem1 = TreeView_InsertItemA(hTree, &ins);
1921 ok(hItem1 != NULL, "InsertItem failed\n");
1922
1923 ins.hParent = TVI_ROOT;
1924 ins.hInsertAfter = hItem1;
1925 U(ins).item.mask = TVIF_TEXT;
1926 U(ins).item.pszText = item2;
1927 hItem2 = TreeView_InsertItemA(hTree, &ins);
1928 ok(hItem2 != NULL, "InsertItem failed\n");
1929
1930 ret = SendMessageA(hTree, TVM_DELETEITEM, 0, (LPARAM)hItem1);
1931 ok(ret == TRUE, "got %d\n", ret);
1932
1933 ret = SendMessageA(hTree, WM_SETREDRAW, TRUE, 0);
1934 ok(ret == 0, "got %d\n", ret);
1935
1936 DestroyWindow(hTree);
1937 }
1938
1939 static void test_cchildren(void)
1940 {
1941 HWND hTree;
1942 INT ret;
1943 TVITEMA item;
1944
1945 hTree = create_treeview_control(0);
1946 fill_tree(hTree);
1947
1948 ret = SendMessageA(hTree, TVM_DELETEITEM, 0, (LPARAM)hChild);
1949 expect(TRUE, ret);
1950
1951 /* check cChildren - automatic mode */
1952 item.hItem = hRoot;
1953 item.mask = TVIF_CHILDREN;
1954
1955 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1956 expect(TRUE, ret);
1957 expect(0, item.cChildren);
1958
1959 DestroyWindow(hTree);
1960
1961 /* start over */
1962 hTree = create_treeview_control(0);
1963 fill_tree(hTree);
1964
1965 /* turn off automatic mode by setting cChildren explicitly */
1966 item.hItem = hRoot;
1967 item.mask = TVIF_CHILDREN;
1968
1969 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1970 expect(TRUE, ret);
1971 expect(1, item.cChildren);
1972
1973 ret = SendMessageA(hTree, TVM_SETITEMA, 0, (LPARAM)&item);
1974 expect(TRUE, ret);
1975
1976 ret = SendMessageA(hTree, TVM_DELETEITEM, 0, (LPARAM)hChild);
1977 expect(TRUE, ret);
1978
1979 /* check cChildren */
1980 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1981 expect(TRUE, ret);
1982 todo_wine
1983 expect(1, item.cChildren);
1984
1985 DestroyWindow(hTree);
1986 }
1987
1988 struct _ITEM_DATA
1989 {
1990 HTREEITEM parent; /* for root value of parent field is unidetified */
1991 HTREEITEM nextsibling;
1992 HTREEITEM firstchild;
1993 };
1994
1995 static void _check_item(HTREEITEM item, HTREEITEM parent, HTREEITEM nextsibling, HTREEITEM firstchild, int line)
1996 {
1997 struct _ITEM_DATA *data = (struct _ITEM_DATA*)item;
1998
1999 ok_(__FILE__, line)(data->parent == parent, "parent %p, got %p\n", parent, data->parent);
2000 ok_(__FILE__, line)(data->nextsibling == nextsibling, "sibling %p, got %p\n", nextsibling, data->nextsibling);
2001 ok_(__FILE__, line)(data->firstchild == firstchild, "firstchild %p, got %p\n", firstchild, data->firstchild);
2002 }
2003
2004 #define check_item(a, b, c, d) _check_item(a, b, c, d, __LINE__)
2005
2006 static void test_htreeitem_layout(void)
2007 {
2008 TVINSERTSTRUCTA ins;
2009 HTREEITEM item1, item2;
2010 HWND hTree;
2011
2012 hTree = create_treeview_control(0);
2013 fill_tree(hTree);
2014
2015 /* root has some special pointer in parent field */
2016 check_item(hRoot, ((struct _ITEM_DATA*)hRoot)->parent, 0, hChild);
2017 check_item(hChild, hRoot, 0, 0);
2018
2019 ins.hParent = hChild;
2020 ins.hInsertAfter = TVI_FIRST;
2021 U(ins).item.mask = 0;
2022 item1 = TreeView_InsertItemA(hTree, &ins);
2023
2024 check_item(item1, hChild, 0, 0);
2025
2026 ins.hParent = hRoot;
2027 ins.hInsertAfter = TVI_FIRST;
2028 U(ins).item.mask = 0;
2029 item2 = TreeView_InsertItemA(hTree, &ins);
2030
2031 check_item(item2, hRoot, hChild, 0);
2032
2033 SendMessageA(hTree, TVM_DELETEITEM, 0, (LPARAM)hChild);
2034
2035 /* without children now */
2036 check_item(hRoot, ((struct _ITEM_DATA*)hRoot)->parent, 0, item2);
2037
2038 DestroyWindow(hTree);
2039 }
2040
2041 static void test_TVS_CHECKBOXES(void)
2042 {
2043 HIMAGELIST himl, himl2;
2044 HWND hTree, hTree2;
2045 TVITEMA item;
2046 DWORD ret;
2047 MSG msg;
2048
2049 hTree = create_treeview_control(0);
2050 fill_tree(hTree);
2051
2052 himl = (HIMAGELIST)SendMessageA(hTree, TVM_GETIMAGELIST, TVSIL_STATE, 0);
2053 ok(himl == NULL, "got %p\n", himl);
2054
2055 item.hItem = hRoot;
2056 item.mask = TVIF_STATE;
2057 item.state = INDEXTOSTATEIMAGEMASK(1);
2058 item.stateMask = TVIS_STATEIMAGEMASK;
2059 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
2060 expect(TRUE, ret);
2061 ok(item.state == 0, "got 0x%x\n", item.state);
2062
2063 /* set some index for a child */
2064 item.hItem = hChild;
2065 item.mask = TVIF_STATE;
2066 item.state = INDEXTOSTATEIMAGEMASK(4);
2067 item.stateMask = TVIS_STATEIMAGEMASK;
2068 ret = SendMessageA(hTree, TVM_SETITEMA, 0, (LPARAM)&item);
2069 expect(TRUE, ret);
2070
2071 /* enabling check boxes set all items to 1 state image index */
2072 SetWindowLongA(hTree, GWL_STYLE, GetWindowLongA(hTree, GWL_STYLE) | TVS_CHECKBOXES);
2073 himl = (HIMAGELIST)SendMessageA(hTree, TVM_GETIMAGELIST, TVSIL_STATE, 0);
2074 ok(himl != NULL, "got %p\n", himl);
2075
2076 himl2 = (HIMAGELIST)SendMessageA(hTree, TVM_GETIMAGELIST, TVSIL_STATE, 0);
2077 ok(himl2 != NULL, "got %p\n", himl2);
2078 ok(himl2 == himl, "got %p, expected %p\n", himl2, himl);
2079
2080 item.hItem = hRoot;
2081 item.mask = TVIF_STATE;
2082 item.state = 0;
2083 item.stateMask = TVIS_STATEIMAGEMASK;
2084 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
2085 expect(TRUE, ret);
2086 ok(item.state == INDEXTOSTATEIMAGEMASK(1), "got 0x%x\n", item.state);
2087
2088 item.hItem = hChild;
2089 item.mask = TVIF_STATE;
2090 item.state = 0;
2091 item.stateMask = TVIS_STATEIMAGEMASK;
2092 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
2093 expect(TRUE, ret);
2094 ok(item.state == INDEXTOSTATEIMAGEMASK(1), "got 0x%x\n", item.state);
2095
2096 /* create another control and check its checkbox list */
2097 hTree2 = create_treeview_control(0);
2098 fill_tree(hTree2);
2099
2100 /* set some index for a child */
2101 item.hItem = hChild;
2102 item.mask = TVIF_STATE;
2103 item.state = INDEXTOSTATEIMAGEMASK(4);
2104 item.stateMask = TVIS_STATEIMAGEMASK;
2105 ret = SendMessageA(hTree2, TVM_SETITEMA, 0, (LPARAM)&item);
2106 expect(TRUE, ret);
2107
2108 /* enabling check boxes set all items to 1 state image index */
2109 SetWindowLongA(hTree2, GWL_STYLE, GetWindowLongA(hTree, GWL_STYLE) | TVS_CHECKBOXES);
2110 himl2 = (HIMAGELIST)SendMessageA(hTree2, TVM_GETIMAGELIST, TVSIL_STATE, 0);
2111 ok(himl2 != NULL, "got %p\n", himl2);
2112 ok(himl != himl2, "got %p, expected %p\n", himl2, himl);
2113
2114 DestroyWindow(hTree2);
2115 DestroyWindow(hTree);
2116
2117 /* the same, but initially created with TVS_CHECKBOXES */
2118 hTree = create_treeview_control(TVS_CHECKBOXES);
2119 fill_tree(hTree);
2120 himl = (HIMAGELIST)SendMessageA(hTree, TVM_GETIMAGELIST, TVSIL_STATE, 0);
2121 ok(himl == NULL, "got %p\n", himl);
2122
2123 item.hItem = hRoot;
2124 item.mask = TVIF_STATE;
2125 item.state = 0;
2126 item.stateMask = TVIS_STATEIMAGEMASK;
2127 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
2128 expect(TRUE, ret);
2129 ok(item.state == INDEXTOSTATEIMAGEMASK(1), "got 0x%x\n", item.state);
2130
2131 item.hItem = hChild;
2132 item.mask = TVIF_STATE;
2133 item.state = 0;
2134 item.stateMask = TVIS_STATEIMAGEMASK;
2135 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
2136 expect(TRUE, ret);
2137 ok(item.state == INDEXTOSTATEIMAGEMASK(1), "got 0x%x\n", item.state);
2138
2139 item.hItem = hChild;
2140 item.mask = TVIF_STATE;
2141 item.state = INDEXTOSTATEIMAGEMASK(2);
2142 item.stateMask = TVIS_STATEIMAGEMASK;
2143 ret = SendMessageA(hTree, TVM_SETITEMA, 0, (LPARAM)&item);
2144 expect(TRUE, ret);
2145
2146 item.hItem = hChild;
2147 item.mask = TVIF_STATE;
2148 item.state = 0;
2149 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
2150 expect(TRUE, ret);
2151 ok(item.state == INDEXTOSTATEIMAGEMASK(2), "got 0x%x\n", item.state);
2152
2153 while(GetMessageA(&msg, 0, 0, 0))
2154 {
2155 TranslateMessage(&msg);
2156 DispatchMessageA(&msg);
2157
2158 if((msg.hwnd == hTree) && (msg.message == WM_PAINT))
2159 break;
2160 }
2161
2162 item.hItem = hChild;
2163 item.mask = TVIF_STATE;
2164 item.state = 0;
2165 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
2166 expect(TRUE, ret);
2167 ok(item.state == INDEXTOSTATEIMAGEMASK(1), "got 0x%x\n", item.state);
2168
2169 himl = (HIMAGELIST)SendMessageA(hTree, TVM_GETIMAGELIST, TVSIL_STATE, 0);
2170 ok(himl != NULL, "got %p\n", himl);
2171
2172 DestroyWindow(hTree);
2173
2174 /* check what happens if TVSIL_STATE image list is removed */
2175 hTree = create_treeview_control(0);
2176 fill_tree(hTree);
2177 himl = (HIMAGELIST)SendMessageA(hTree, TVM_GETIMAGELIST, TVSIL_STATE, 0);
2178 ok(himl == NULL, "got %p\n", himl);
2179
2180 SetWindowLongA(hTree, GWL_STYLE, GetWindowLongA(hTree, GWL_STYLE) | TVS_CHECKBOXES);
2181 himl = (HIMAGELIST)SendMessageA(hTree, TVM_GETIMAGELIST, TVSIL_STATE, 0);
2182 ok(himl != NULL, "got %p\n", himl);
2183
2184 himl2 = (HIMAGELIST)SendMessageA(hTree, TVM_SETIMAGELIST, TVSIL_STATE, 0);
2185 ok(himl2 == himl, "got %p\n", himl2);
2186
2187 himl2 = (HIMAGELIST)SendMessageA(hTree, TVM_GETIMAGELIST, TVSIL_STATE, 0);
2188 ok(himl2 == NULL, "got %p\n", himl2);
2189
2190 item.hItem = hChild;
2191 item.mask = TVIF_STATE;
2192 item.state = INDEXTOSTATEIMAGEMASK(2);
2193 item.stateMask = TVIS_STATEIMAGEMASK;
2194 ret = SendMessageA(hTree, TVM_SETITEMA, 0, (LPARAM)&item);
2195 expect(TRUE, ret);
2196
2197 item.hItem = hChild;
2198 item.mask = TVIF_STATE;
2199 item.state = 0;
2200 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
2201 expect(TRUE, ret);
2202 ok(item.state == INDEXTOSTATEIMAGEMASK(2), "got 0x%x\n", item.state);
2203
2204 while(GetMessageA(&msg, 0, 0, 0))
2205 {
2206 TranslateMessage(&msg);
2207 DispatchMessageA(&msg);
2208
2209 if((msg.hwnd == hTree) && (msg.message == WM_PAINT))
2210 break;
2211 }
2212
2213 item.hItem = hChild;
2214 item.mask = TVIF_STATE;
2215 item.state = 0;
2216 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
2217 expect(TRUE, ret);
2218 ok(item.state == INDEXTOSTATEIMAGEMASK(1), "got 0x%x\n", item.state);
2219
2220 himl = (HIMAGELIST)SendMessageA(hTree, TVM_GETIMAGELIST, TVSIL_STATE, 0);
2221 ok(himl != NULL, "got %p\n", himl);
2222
2223 DestroyWindow(hTree);
2224 }
2225
2226 static void test_TVM_GETNEXTITEM(void)
2227 {
2228 HTREEITEM item;
2229 HWND hTree;
2230
2231 hTree = create_treeview_control(0);
2232 fill_tree(hTree);
2233
2234 item = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_ROOT, 0);
2235 ok(item == hRoot, "got %p, expected %p\n", item, hRoot);
2236
2237 item = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_ROOT, (LPARAM)TVI_ROOT);
2238 ok(item == hRoot, "got %p, expected %p\n", item, hRoot);
2239
2240 item = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_ROOT, (LPARAM)hRoot);
2241 ok(item == hRoot, "got %p, expected %p\n", item, hRoot);
2242
2243 item = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_ROOT, (LPARAM)hChild);
2244 ok(item == hRoot, "got %p, expected %p\n", item, hRoot);
2245
2246 item = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_CHILD, 0);
2247 ok(item == hRoot, "got %p, expected %p\n", item, hRoot);
2248
2249 item = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hRoot);
2250 ok(item == hChild, "got %p, expected %p\n", item, hChild);
2251
2252 item = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)TVI_ROOT);
2253 ok(item == hRoot, "got %p, expected %p\n", item, hRoot);
2254
2255 item = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_PARENT, 0);
2256 ok(item == NULL, "got %p\n", item);
2257
2258 item = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_PARENT, (LPARAM)hChild);
2259 ok(item == hRoot, "got %p, expected %p\n", item, hRoot);
2260
2261 DestroyWindow(hTree);
2262 }
2263
2264 static void test_TVM_HITTEST(void)
2265 {
2266 HWND hTree;
2267 LRESULT ret;
2268 RECT rc;
2269 TVHITTESTINFO ht;
2270
2271 hTree = create_treeview_control(0);
2272 fill_tree(hTree);
2273
2274 *(HTREEITEM*)&rc = hRoot;
2275 ret = SendMessageA(hTree, TVM_GETITEMRECT, TRUE, (LPARAM)&rc);
2276 expect(TRUE, (BOOL)ret);
2277
2278 ht.pt.x = rc.left-1;
2279 ht.pt.y = rc.top;
2280
2281 ret = SendMessageA(hTree, TVM_HITTEST, 0, (LPARAM)&ht);
2282 ok((HTREEITEM)ret == hRoot, "got %p, expected %p\n", (HTREEITEM)ret, hRoot);
2283 ok(ht.hItem == hRoot, "got %p, expected %p\n", ht.hItem, hRoot);
2284 ok(ht.flags == TVHT_ONITEMBUTTON, "got %d, expected %d\n", ht.flags, TVHT_ONITEMBUTTON);
2285
2286 ret = SendMessageA(hTree, TVM_EXPAND, TVE_EXPAND, (LPARAM)hRoot);
2287 expect(TRUE, (BOOL)ret);
2288
2289 *(HTREEITEM*)&rc = hChild;
2290 ret = SendMessageA(hTree, TVM_GETITEMRECT, TRUE, (LPARAM)&rc);
2291 expect(TRUE, (BOOL)ret);
2292
2293 ht.pt.x = rc.left-1;
2294 ht.pt.y = rc.top;
2295
2296 ret = SendMessageA(hTree, TVM_HITTEST, 0, (LPARAM)&ht);
2297 ok((HTREEITEM)ret == hChild, "got %p, expected %p\n", (HTREEITEM)ret, hChild);
2298 ok(ht.hItem == hChild, "got %p, expected %p\n", ht.hItem, hChild);
2299 /* Wine returns item button here, but this item has no button */
2300 todo_wine ok(ht.flags == TVHT_ONITEMINDENT, "got %d, expected %d\n", ht.flags, TVHT_ONITEMINDENT);
2301
2302 DestroyWindow(hTree);
2303 }
2304
2305 static void test_WM_GETDLGCODE(void)
2306 {
2307 DWORD code;
2308 HWND hTree;
2309
2310 hTree = create_treeview_control(0);
2311
2312 code = SendMessageA(hTree, WM_GETDLGCODE, VK_TAB, 0);
2313 ok(code == (DLGC_WANTCHARS | DLGC_WANTARROWS), "0x%08x\n", code);
2314
2315 DestroyWindow(hTree);
2316 }
2317
2318 static void test_customdraw(void)
2319 {
2320 static const char *rootA = "root";
2321 TVINSERTSTRUCTA ins;
2322 HTREEITEM hRoot;
2323 LOGFONTA lf;
2324 HWND hwnd;
2325
2326 hwnd = create_treeview_control(0);
2327
2328 ins.hParent = TVI_ROOT;
2329 ins.hInsertAfter = TVI_ROOT;
2330 U(ins).item.mask = TVIF_TEXT;
2331 U(ins).item.pszText = (char*)rootA;
2332 hRoot = TreeView_InsertItemA(hwnd, &ins);
2333 ok(hRoot != NULL, "got %p\n", hRoot);
2334
2335 /* create additional font, custom draw handler will select it */
2336 SystemParametersInfoA(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
2337 lf.lfHeight *= 2;
2338 g_customdraw_font = CreateFontIndirectA(&lf);
2339 flush_sequences(sequences, NUM_MSG_SEQUENCES);
2340 InvalidateRect(hwnd, NULL, TRUE);
2341 UpdateWindow(hwnd);
2342 ok_sequence(sequences, PARENT_CD_SEQ_INDEX, parent_cd_seq, "custom draw notifications", FALSE);
2343 DeleteObject(g_customdraw_font);
2344 g_customdraw_font = NULL;
2345
2346 DestroyWindow(hwnd);
2347 }
2348
2349 static void test_WM_KEYDOWN(void)
2350 {
2351 static const char *rootA = "root";
2352 TVINSERTSTRUCTA ins;
2353 HTREEITEM hRoot;
2354 HWND hwnd;
2355
2356 hwnd = create_treeview_control(0);
2357
2358 ins.hParent = TVI_ROOT;
2359 ins.hInsertAfter = TVI_ROOT;
2360 U(ins).item.mask = TVIF_TEXT;
2361 U(ins).item.pszText = (char*)rootA;
2362 hRoot = TreeView_InsertItemA(hwnd, &ins);
2363 ok(hRoot != NULL, "got %p\n", hRoot);
2364
2365 flush_sequences(sequences, NUM_MSG_SEQUENCES);
2366 SendMessageA(hwnd, WM_KEYDOWN, VK_RETURN, 0);
2367 ok_sequence(sequences, PARENT_SEQ_INDEX, parent_vk_return_seq, "WM_KEYDOWN/VK_RETURN parent notification", TRUE);
2368
2369 DestroyWindow(hwnd);
2370 }
2371
2372 static void test_TVS_FULLROWSELECT(void)
2373 {
2374 DWORD style;
2375 HWND hwnd;
2376
2377 /* try to create both with TVS_HASLINES and TVS_FULLROWSELECT */
2378 hwnd = create_treeview_control(TVS_FULLROWSELECT);
2379
2380 style = GetWindowLongA(hwnd, GWL_STYLE);
2381 ok((style & (TVS_FULLROWSELECT | TVS_HASLINES)) == (TVS_FULLROWSELECT | TVS_HASLINES), "got style 0x%08x\n", style);
2382
2383 DestroyWindow(hwnd);
2384
2385 /* create just with TVS_HASLINES, try to enable TVS_FULLROWSELECT later */
2386 hwnd = create_treeview_control(0);
2387
2388 style = GetWindowLongA(hwnd, GWL_STYLE);
2389 SetWindowLongA(hwnd, GWL_STYLE, style | TVS_FULLROWSELECT);
2390 style = GetWindowLongA(hwnd, GWL_STYLE);
2391 ok(style & TVS_FULLROWSELECT, "got style 0x%08x\n", style);
2392
2393 DestroyWindow(hwnd);
2394 }
2395
2396 START_TEST(treeview)
2397 {
2398 HMODULE hComctl32;
2399 BOOL (WINAPI *pInitCommonControlsEx)(const INITCOMMONCONTROLSEX*);
2400 WNDCLASSA wc;
2401 MSG msg;
2402
2403 ULONG_PTR ctx_cookie;
2404 HANDLE hCtx;
2405
2406 hComctl32 = GetModuleHandleA("comctl32.dll");
2407 pInitCommonControlsEx = (void*)GetProcAddress(hComctl32, "InitCommonControlsEx");
2408 if (pInitCommonControlsEx)
2409 {
2410 INITCOMMONCONTROLSEX iccex;
2411 iccex.dwSize = sizeof(iccex);
2412 iccex.dwICC = ICC_TREEVIEW_CLASSES;
2413 pInitCommonControlsEx(&iccex);
2414 }
2415 else
2416 InitCommonControls();
2417
2418 init_msg_sequences(sequences, NUM_MSG_SEQUENCES);
2419 init_msg_sequences(item_sequence, 1);
2420
2421 wc.style = CS_HREDRAW | CS_VREDRAW;
2422 wc.cbClsExtra = 0;
2423 wc.cbWndExtra = 0;
2424 wc.hInstance = GetModuleHandleA(NULL);
2425 wc.hIcon = NULL;
2426 wc.hCursor = LoadCursorA(NULL, (LPCSTR)IDC_IBEAM);
2427 wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
2428 wc.lpszMenuName = NULL;
2429 wc.lpszClassName = "MyTestWnd";
2430 wc.lpfnWndProc = parent_wnd_proc;
2431 RegisterClassA(&wc);
2432
2433 hMainWnd = CreateWindowExA(0, "MyTestWnd", "Blah", WS_OVERLAPPEDWINDOW,
2434 CW_USEDEFAULT, CW_USEDEFAULT, 130, 105, NULL, NULL, GetModuleHandleA(NULL), 0);
2435
2436 ok(hMainWnd != NULL, "Failed to create parent window. Tests aborted.\n");
2437 if (!hMainWnd) return;
2438
2439 test_fillroot();
2440 test_select();
2441 test_getitemtext();
2442 test_focus();
2443 test_get_set_bkcolor();
2444 test_get_set_imagelist();
2445 test_get_set_indent();
2446 test_get_set_insertmark();
2447 test_get_set_item();
2448 test_get_set_itemheight();
2449 test_get_set_scrolltime();
2450 test_get_set_textcolor();
2451 test_get_linecolor();
2452 test_get_insertmarkcolor();
2453 test_get_set_tooltips();
2454 test_get_set_unicodeformat();
2455 test_callback();
2456 test_expandinvisible();
2457 test_itemedit();
2458 test_treeview_classinfo();
2459 test_expandnotify();
2460 test_TVS_SINGLEEXPAND();
2461 test_WM_PAINT();
2462 test_delete_items();
2463 test_cchildren();
2464 test_htreeitem_layout();
2465 test_TVS_CHECKBOXES();
2466 test_TVM_GETNEXTITEM();
2467 test_TVM_HITTEST();
2468 test_WM_GETDLGCODE();
2469 test_customdraw();
2470 test_WM_KEYDOWN();
2471 test_TVS_FULLROWSELECT();
2472
2473 if (!load_v6_module(&ctx_cookie, &hCtx))
2474 {
2475 DestroyWindow(hMainWnd);
2476 return;
2477 }
2478
2479 /* comctl32 version 6 tests start here */
2480 test_expandedimage();
2481 test_htreeitem_layout();
2482 test_WM_GETDLGCODE();
2483
2484 unload_v6_module(ctx_cookie, hCtx);
2485
2486 PostMessageA(hMainWnd, WM_CLOSE, 0, 0);
2487 while(GetMessageA(&msg, 0, 0, 0))
2488 {
2489 TranslateMessage(&msg);
2490 DispatchMessageA(&msg);
2491 }
2492 }