3d46aa8359636d6868da712b0e56b80bb356782e
[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 ok(pTreeView->itemNew.mask ==
1197 (TVIF_HANDLE | TVIF_SELECTEDIMAGE | TVIF_IMAGE | TVIF_PARAM | TVIF_STATE),
1198 "got wrong mask %x\n", pTreeView->itemNew.mask);
1199 ok(pTreeView->itemOld.mask == 0,
1200 "got wrong mask %x\n", pTreeView->itemOld.mask);
1201
1202 if (g_get_from_expand)
1203 {
1204 g_item_expanding.mask = TVIF_STATE;
1205 g_item_expanding.hItem = hRoot;
1206 ret = SendMessageA(pHdr->hwndFrom, TVM_GETITEMA, 0, (LPARAM)&g_item_expanding);
1207 ok(ret == TRUE, "got %lu\n", ret);
1208 }
1209 break;
1210 case TVN_ITEMEXPANDEDA:
1211 ok(pTreeView->itemNew.mask & TVIF_STATE, "got wrong mask %x\n", pTreeView->itemNew.mask);
1212 ok(pTreeView->itemNew.state & (TVIS_EXPANDED|TVIS_EXPANDEDONCE),
1213 "got wrong mask %x\n", pTreeView->itemNew.mask);
1214 ok(pTreeView->itemOld.mask == 0,
1215 "got wrong mask %x\n", pTreeView->itemOld.mask);
1216
1217 if (g_get_from_expand)
1218 {
1219 g_item_expanded.mask = TVIF_STATE;
1220 g_item_expanded.hItem = hRoot;
1221 ret = SendMessageA(pHdr->hwndFrom, TVM_GETITEMA, 0, (LPARAM)&g_item_expanded);
1222 ok(ret == TRUE, "got %lu\n", ret);
1223 }
1224 if (g_get_rect_in_expand)
1225 {
1226 visibleItem = (HTREEITEM)SendMessageA(pHdr->hwndFrom, TVM_GETNEXTITEM,
1227 TVGN_FIRSTVISIBLE, 0);
1228 ok(pTreeView->itemNew.hItem == visibleItem, "expanded item == first visible item\n");
1229 *(HTREEITEM*)&rect = visibleItem;
1230 ok(SendMessageA(pHdr->hwndFrom, TVM_GETITEMRECT, TRUE, (LPARAM)&rect),
1231 "Failed to get rect for first visible item.\n");
1232 visibleItem = (HTREEITEM)SendMessageA(pHdr->hwndFrom, TVM_GETNEXTITEM,
1233 TVGN_NEXTVISIBLE, (LPARAM)visibleItem);
1234 *(HTREEITEM*)&rect = visibleItem;
1235 ok(visibleItem != NULL, "There must be a visible item after the first visisble item.\n");
1236 ok(SendMessageA(pHdr->hwndFrom, TVM_GETITEMRECT, TRUE, (LPARAM)&rect),
1237 "Failed to get rect for second visible item.\n");
1238 }
1239 break;
1240 case TVN_DELETEITEMA:
1241 {
1242 struct message item;
1243
1244 ok(pTreeView->itemNew.mask == 0, "got wrong mask 0x%x\n", pTreeView->itemNew.mask);
1245
1246 ok(pTreeView->itemOld.mask == (TVIF_HANDLE | TVIF_PARAM), "got wrong mask 0x%x\n", pTreeView->itemOld.mask);
1247 ok(pTreeView->itemOld.hItem != NULL, "got %p\n", pTreeView->itemOld.hItem);
1248
1249 memset(&item, 0, sizeof(item));
1250 item.lParam = (LPARAM)pTreeView->itemOld.hItem;
1251 add_message(item_sequence, 0, &item);
1252
1253 break;
1254 }
1255 case NM_CUSTOMDRAW:
1256 {
1257 NMTVCUSTOMDRAW *nmcd = (NMTVCUSTOMDRAW*)lParam;
1258 COLORREF c0ffee = RGB(0xc0,0xff,0xee), cafe = RGB(0xca,0xfe,0x00);
1259
1260 msg.flags |= custdraw;
1261 msg.stage = nmcd->nmcd.dwDrawStage;
1262 add_message(sequences, PARENT_CD_SEQ_INDEX, &msg);
1263
1264 switch (msg.stage)
1265 {
1266 case CDDS_PREPAINT:
1267 return CDRF_NOTIFYITEMDRAW|CDRF_NOTIFYITEMERASE|CDRF_NOTIFYPOSTPAINT;
1268 case CDDS_ITEMPREPAINT:
1269 nmcd->clrTextBk = c0ffee;
1270 nmcd->clrText = cafe;
1271 if (g_customdraw_font)
1272 SelectObject(nmcd->nmcd.hdc, g_customdraw_font);
1273 return CDRF_NOTIFYPOSTPAINT|CDRF_NEWFONT;
1274 case CDDS_ITEMPOSTPAINT:
1275 /* at the point of post paint notification colors are already restored */
1276 ok(GetTextColor(nmcd->nmcd.hdc) != cafe, "got 0%x\n", GetTextColor(nmcd->nmcd.hdc));
1277 ok(GetBkColor(nmcd->nmcd.hdc) != c0ffee, "got 0%x\n", GetBkColor(nmcd->nmcd.hdc));
1278 if (g_customdraw_font)
1279 ok(GetCurrentObject(nmcd->nmcd.hdc, OBJ_FONT) != g_customdraw_font, "got %p\n",
1280 GetCurrentObject(nmcd->nmcd.hdc, OBJ_FONT));
1281 break;
1282 default:
1283 ;
1284 }
1285 break;
1286 }
1287 }
1288 }
1289 break;
1290 }
1291
1292 case WM_DESTROY:
1293 PostQuitMessage(0);
1294 break;
1295 }
1296
1297 defwndproc_counter++;
1298 ret = DefWindowProcA(hWnd, message, wParam, lParam);
1299 defwndproc_counter--;
1300
1301 return ret;
1302 }
1303
1304 static void test_expandinvisible(void)
1305 {
1306 static CHAR nodeText[][5] = {"0", "1", "2", "3", "4"};
1307 TVINSERTSTRUCTA ins;
1308 HTREEITEM node[5];
1309 RECT dummyRect;
1310 BOOL nodeVisible;
1311 LRESULT ret;
1312 HWND hTree;
1313
1314 hTree = create_treeview_control(0);
1315
1316 /* The test builds the following tree and expands node 1, while node 0 is collapsed.
1317 *
1318 * 0
1319 * |- 1
1320 * | |- 2
1321 * | |- 3
1322 * |- 4
1323 *
1324 */
1325
1326 ret = SendMessageA(hTree, TVM_DELETEITEM, 0, (LPARAM)TVI_ROOT);
1327 ok(ret == TRUE, "ret\n");
1328 ins.hParent = TVI_ROOT;
1329 ins.hInsertAfter = TVI_ROOT;
1330 U(ins).item.mask = TVIF_TEXT;
1331 U(ins).item.pszText = nodeText[0];
1332 node[0] = TreeView_InsertItemA(hTree, &ins);
1333 ok(node[0] != NULL, "failed to set node[0]\n");
1334
1335 ins.hInsertAfter = TVI_LAST;
1336 U(ins).item.mask = TVIF_TEXT;
1337 ins.hParent = node[0];
1338
1339 U(ins).item.pszText = nodeText[1];
1340 node[1] = TreeView_InsertItemA(hTree, &ins);
1341 ok(node[1] != NULL, "failed to set node[1]\n");
1342 U(ins).item.pszText = nodeText[4];
1343 node[4] = TreeView_InsertItemA(hTree, &ins);
1344 ok(node[4] != NULL, "failed to set node[4]\n");
1345
1346 ins.hParent = node[1];
1347
1348 U(ins).item.pszText = nodeText[2];
1349 node[2] = TreeView_InsertItemA(hTree, &ins);
1350 ok(node[2] != NULL, "failed to set node[2]\n");
1351 U(ins).item.pszText = nodeText[3];
1352 node[3] = TreeView_InsertItemA(hTree, &ins);
1353 ok(node[3] != NULL, "failed to set node[3]\n");
1354
1355 *(HTREEITEM *)&dummyRect = node[1];
1356 nodeVisible = SendMessageA(hTree, TVM_GETITEMRECT, FALSE, (LPARAM)&dummyRect);
1357 ok(!nodeVisible, "Node 1 should not be visible.\n");
1358 *(HTREEITEM *)&dummyRect = node[2];
1359 nodeVisible = SendMessageA(hTree, TVM_GETITEMRECT, FALSE, (LPARAM)&dummyRect);
1360 ok(!nodeVisible, "Node 2 should not be visible.\n");
1361 *(HTREEITEM *)&dummyRect = node[3];
1362 nodeVisible = SendMessageA(hTree, TVM_GETITEMRECT, FALSE, (LPARAM)&dummyRect);
1363 ok(!nodeVisible, "Node 3 should not be visible.\n");
1364 *(HTREEITEM *)&dummyRect = node[4];
1365 nodeVisible = SendMessageA(hTree, TVM_GETITEMRECT, FALSE, (LPARAM)&dummyRect);
1366 ok(!nodeVisible, "Node 4 should not be visible.\n");
1367
1368 ok(SendMessageA(hTree, TVM_EXPAND, TVE_EXPAND, (LPARAM)node[1]), "Expand of node 1 failed.\n");
1369
1370 *(HTREEITEM *)&dummyRect = node[1];
1371 nodeVisible = SendMessageA(hTree, TVM_GETITEMRECT, FALSE, (LPARAM)&dummyRect);
1372 ok(!nodeVisible, "Node 1 should not be visible.\n");
1373 *(HTREEITEM *)&dummyRect = node[2];
1374 nodeVisible = SendMessageA(hTree, TVM_GETITEMRECT, FALSE, (LPARAM)&dummyRect);
1375 ok(!nodeVisible, "Node 2 should not be visible.\n");
1376 *(HTREEITEM *)&dummyRect = node[3];
1377 nodeVisible = SendMessageA(hTree, TVM_GETITEMRECT, FALSE, (LPARAM)&dummyRect);
1378 ok(!nodeVisible, "Node 3 should not be visible.\n");
1379 *(HTREEITEM *)&dummyRect = node[4];
1380 nodeVisible = SendMessageA(hTree, TVM_GETITEMRECT, FALSE, (LPARAM)&dummyRect);
1381 ok(!nodeVisible, "Node 4 should not be visible.\n");
1382
1383 DestroyWindow(hTree);
1384 }
1385
1386 static void test_itemedit(void)
1387 {
1388 DWORD r;
1389 HWND edit;
1390 TVITEMA item;
1391 CHAR buffA[20];
1392 HWND hTree;
1393
1394 hTree = create_treeview_control(0);
1395 fill_tree(hTree);
1396
1397 /* try with null item */
1398 edit = (HWND)SendMessageA(hTree, TVM_EDITLABELA, 0, 0);
1399 ok(!IsWindow(edit), "Expected valid handle\n");
1400
1401 /* trigger edit */
1402 edit = (HWND)SendMessageA(hTree, TVM_EDITLABELA, 0, (LPARAM)hRoot);
1403 ok(IsWindow(edit), "Expected valid handle\n");
1404 /* item shouldn't be selected automatically after TVM_EDITLABELA */
1405 r = SendMessageA(hTree, TVM_GETITEMSTATE, (WPARAM)hRoot, TVIS_SELECTED);
1406 expect(0, r);
1407 /* try to cancel with wrong edit handle */
1408 r = SendMessageA(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), 0);
1409 expect(0, r);
1410 ok(IsWindow(edit), "Expected edit control to be valid\n");
1411 r = SendMessageA(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)edit);
1412 expect(0, r);
1413 ok(!IsWindow(edit), "Expected edit control to be destroyed\n");
1414 /* try to cancel without creating edit */
1415 r = SendMessageA(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), 0);
1416 expect(0, r);
1417
1418 /* try to cancel with wrong (not null) handle */
1419 edit = (HWND)SendMessageA(hTree, TVM_EDITLABELA, 0, (LPARAM)hRoot);
1420 ok(IsWindow(edit), "Expected valid handle\n");
1421 r = SendMessageA(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)hTree);
1422 expect(0, r);
1423 ok(IsWindow(edit), "Expected edit control to be valid\n");
1424 r = SendMessageA(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)edit);
1425 expect(0, r);
1426
1427 /* remove selection after starting edit */
1428 r = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hRoot);
1429 expect(TRUE, r);
1430 edit = (HWND)SendMessageA(hTree, TVM_EDITLABELA, 0, (LPARAM)hRoot);
1431 ok(IsWindow(edit), "Expected valid handle\n");
1432 r = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, 0);
1433 expect(TRUE, r);
1434 /* alter text */
1435 strcpy(buffA, "x");
1436 r = SendMessageA(edit, WM_SETTEXT, 0, (LPARAM)buffA);
1437 expect(TRUE, r);
1438 r = SendMessageA(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)edit);
1439 expect(0, r);
1440 ok(!IsWindow(edit), "Expected edit control to be destroyed\n");
1441 /* check that text is saved */
1442 item.mask = TVIF_TEXT;
1443 item.hItem = hRoot;
1444 item.pszText = buffA;
1445 item.cchTextMax = sizeof(buffA)/sizeof(CHAR);
1446 r = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1447 expect(TRUE, r);
1448 ok(!strcmp("x", buffA), "Expected item text to change\n");
1449
1450 /* try A/W messages */
1451 edit = (HWND)SendMessageA(hTree, TVM_EDITLABELA, 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 ok(!IsWindow(edit), "expected invalid handle\n");
1457
1458 edit = (HWND)SendMessageA(hTree, TVM_EDITLABELW, 0, (LPARAM)hRoot);
1459 ok(IsWindow(edit), "Expected valid handle\n");
1460 ok(IsWindowUnicode(edit), "got ansi window\n");
1461 r = SendMessageA(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)edit);
1462 expect(0, r);
1463
1464 /* alter text during TVM_BEGINLABELEDIT, check that it's preserved */
1465 strcpy(buffA, "<root>");
1466
1467 item.mask = TVIF_TEXT;
1468 item.hItem = hRoot;
1469 item.pszText = buffA;
1470 item.cchTextMax = 0;
1471 r = SendMessageA(hTree, TVM_SETITEMA, 0, (LPARAM)&item);
1472 expect(TRUE, r);
1473
1474 g_beginedit_alter_text = TRUE;
1475 edit = (HWND)SendMessageA(hTree, TVM_EDITLABELA, 0, (LPARAM)hRoot);
1476 ok(IsWindow(edit), "Expected valid handle\n");
1477 g_beginedit_alter_text = FALSE;
1478
1479 GetWindowTextA(edit, buffA, sizeof(buffA)/sizeof(CHAR));
1480 ok(!strcmp(buffA, "<edittextaltered>"), "got string %s\n", buffA);
1481
1482 DestroyWindow(hTree);
1483 }
1484
1485 static void test_treeview_classinfo(void)
1486 {
1487 WNDCLASSA cls;
1488
1489 memset(&cls, 0, sizeof(cls));
1490 GetClassInfoA(GetModuleHandleA("comctl32.dll"), WC_TREEVIEWA, &cls);
1491 ok(cls.hbrBackground == NULL, "Expected NULL background brush, got %p\n", cls.hbrBackground);
1492 ok(cls.style == (CS_GLOBALCLASS | CS_DBLCLKS), "Expected got %x\n", cls.style);
1493 expect(0, cls.cbClsExtra);
1494 }
1495
1496 static void test_get_linecolor(void)
1497 {
1498 COLORREF clr;
1499 HWND hTree;
1500
1501 hTree = create_treeview_control(0);
1502
1503 /* newly created control has default color */
1504 clr = SendMessageA(hTree, TVM_GETLINECOLOR, 0, 0);
1505 if (clr == 0)
1506 win_skip("TVM_GETLINECOLOR is not supported on comctl32 < 5.80\n");
1507 else
1508 expect(CLR_DEFAULT, clr);
1509
1510 DestroyWindow(hTree);
1511 }
1512
1513 static void test_get_insertmarkcolor(void)
1514 {
1515 COLORREF clr;
1516 HWND hTree;
1517
1518 hTree = create_treeview_control(0);
1519
1520 /* newly created control has default color */
1521 clr = SendMessageA(hTree, TVM_GETINSERTMARKCOLOR, 0, 0);
1522 if (clr == 0)
1523 win_skip("TVM_GETINSERTMARKCOLOR is not supported on comctl32 < 5.80\n");
1524 else
1525 expect(CLR_DEFAULT, clr);
1526
1527 DestroyWindow(hTree);
1528 }
1529
1530 static void test_expandnotify(void)
1531 {
1532 HWND hTree;
1533 BOOL ret;
1534 TVITEMA item;
1535
1536 hTree = create_treeview_control(0);
1537 fill_tree(hTree);
1538
1539 item.hItem = hRoot;
1540 item.mask = TVIF_STATE;
1541
1542 item.state = TVIS_EXPANDED;
1543 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1544 expect(TRUE, ret);
1545 ok((item.state & TVIS_EXPANDED) == 0, "expected collapsed\n");
1546
1547 /* preselect root node here */
1548 ret = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hRoot);
1549 expect(TRUE, ret);
1550
1551 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1552 ret = SendMessageA(hTree, TVM_EXPAND, TVE_COLLAPSE, (LPARAM)hRoot);
1553 expect(FALSE, ret);
1554 ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq, "no collapse notifications", FALSE);
1555
1556 g_get_from_expand = TRUE;
1557 /* expand */
1558 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1559 g_item_expanding.state = 0xdeadbeef;
1560 g_item_expanded.state = 0xdeadbeef;
1561 ret = SendMessageA(hTree, TVM_EXPAND, TVE_EXPAND, (LPARAM)hRoot);
1562 expect(TRUE, ret);
1563 ok(g_item_expanding.state == TVIS_SELECTED, "got state on TVN_ITEMEXPANDING 0x%08x\n",
1564 g_item_expanding.state);
1565 ok(g_item_expanded.state == (TVIS_SELECTED|TVIS_EXPANDED), "got state on TVN_ITEMEXPANDED 0x%08x\n",
1566 g_item_expanded.state);
1567 ok_sequence(sequences, PARENT_SEQ_INDEX, parent_expand_seq, "expand notifications", FALSE);
1568 g_get_from_expand = FALSE;
1569
1570 /* check that it's expanded */
1571 item.state = TVIS_EXPANDED;
1572 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1573 expect(TRUE, ret);
1574 ok((item.state & TVIS_EXPANDED) == TVIS_EXPANDED, "expected expanded\n");
1575
1576 /* collapse */
1577 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1578 ret = SendMessageA(hTree, TVM_EXPAND, TVE_COLLAPSE, (LPARAM)hRoot);
1579 expect(TRUE, ret);
1580 item.state = TVIS_EXPANDED;
1581 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1582 expect(TRUE, ret);
1583 ok((item.state & TVIS_EXPANDED) == 0, "expected collapsed\n");
1584 /* all further collapse/expand attempts won't produce any notifications,
1585 the only way is to reset with all children removed */
1586 ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq, "collapse after expand notifications", FALSE);
1587
1588 /* try to toggle child that doesn't have children itself */
1589 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1590 ret = SendMessageA(hTree, TVM_EXPAND, TVE_TOGGLE, (LPARAM)hChild);
1591 expect(FALSE, ret);
1592 ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq, "toggle node without children", FALSE);
1593
1594 DestroyWindow(hTree);
1595
1596 /* test TVM_GETITEMRECT inside TVN_ITEMEXPANDED notification */
1597 hTree = create_treeview_control(0);
1598 fill_tree(hTree);
1599 g_get_rect_in_expand = TRUE;
1600 ret = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hChild);
1601 expect(TRUE, ret);
1602 g_get_rect_in_expand = FALSE;
1603
1604 DestroyWindow(hTree);
1605
1606 /* TVE_TOGGLE acts as any other TVM_EXPAND */
1607 hTree = create_treeview_control(0);
1608 fill_tree(hTree);
1609
1610 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1611 ret = SendMessageA(hTree, TVM_EXPAND, TVE_TOGGLE, (LPARAM)hRoot);
1612 expect(TRUE, ret);
1613 ok_sequence(sequences, PARENT_SEQ_INDEX, parent_expand_seq, "toggle node (expand)", FALSE);
1614
1615 /* toggle again - no notifications */
1616 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1617 ret = SendMessageA(hTree, TVM_EXPAND, TVE_TOGGLE, (LPARAM)hRoot);
1618 expect(TRUE, ret);
1619 ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq, "toggle node (collapse)", FALSE);
1620
1621 DestroyWindow(hTree);
1622
1623 /* some keyboard events are also translated to expand */
1624 hTree = create_treeview_control(0);
1625 fill_tree(hTree);
1626
1627 /* preselect root node here */
1628 ret = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hRoot);
1629 expect(TRUE, ret);
1630
1631 g_get_from_expand = TRUE;
1632 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1633 ret = SendMessageA(hTree, WM_KEYDOWN, VK_ADD, 0);
1634 expect(FALSE, ret);
1635 ok_sequence(sequences, PARENT_SEQ_INDEX, parent_expand_kb_seq, "expand node", FALSE);
1636 ok(g_item_expanding.state == TVIS_SELECTED, "got state on TVN_ITEMEXPANDING 0x%08x\n",
1637 g_item_expanding.state);
1638 ok(g_item_expanded.state == (TVIS_SELECTED|TVIS_EXPANDED), "got state on TVN_ITEMEXPANDED 0x%08x\n",
1639 g_item_expanded.state);
1640
1641 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1642 ret = SendMessageA(hTree, WM_KEYDOWN, VK_ADD, 0);
1643 expect(FALSE, ret);
1644 ok_sequence(sequences, PARENT_SEQ_INDEX, parent_expand_kb_seq, "expand node again", FALSE);
1645 ok(g_item_expanding.state == (TVIS_SELECTED|TVIS_EXPANDED|TVIS_EXPANDEDONCE), "got state on TVN_ITEMEXPANDING 0x%08x\n",
1646 g_item_expanding.state);
1647 ok(g_item_expanded.state == (TVIS_SELECTED|TVIS_EXPANDED|TVIS_EXPANDEDONCE), "got state on TVN_ITEMEXPANDED 0x%08x\n",
1648 g_item_expanded.state);
1649
1650 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1651 ret = SendMessageA(hTree, WM_KEYDOWN, VK_SUBTRACT, 0);
1652 expect(FALSE, ret);
1653 ok_sequence(sequences, PARENT_SEQ_INDEX, parent_expand_kb_seq, "collapse node", FALSE);
1654 ok(g_item_expanding.state == (TVIS_SELECTED|TVIS_EXPANDED|TVIS_EXPANDEDONCE), "got state on TVN_ITEMEXPANDING 0x%08x\n",
1655 g_item_expanding.state);
1656 ok(g_item_expanded.state == (TVIS_SELECTED|TVIS_EXPANDEDONCE), "got state on TVN_ITEMEXPANDED 0x%08x\n",
1657 g_item_expanded.state);
1658
1659 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1660 ret = SendMessageA(hTree, WM_KEYDOWN, VK_SUBTRACT, 0);
1661 expect(FALSE, ret);
1662 ok_sequence(sequences, PARENT_SEQ_INDEX, parent_collapse_2nd_kb_seq, "collapse node again", FALSE);
1663 ok(g_item_expanding.state == (TVIS_SELECTED|TVIS_EXPANDEDONCE), "got state on TVN_ITEMEXPANDING 0x%08x\n",
1664 g_item_expanding.state);
1665 g_get_from_expand = FALSE;
1666
1667 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1668 ret = SendMessageA(hTree, WM_KEYDOWN, VK_ADD, 0);
1669 expect(FALSE, ret);
1670 ok_sequence(sequences, PARENT_SEQ_INDEX, parent_expand_kb_seq, "expand node", FALSE);
1671
1672 /* go to child */
1673 ret = SendMessageA(hTree, WM_KEYDOWN, VK_RIGHT, 0);
1674 expect(FALSE, ret);
1675
1676 /* try to expand child that doesn't have children itself */
1677 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1678 ret = SendMessageA(hTree, WM_KEYDOWN, VK_ADD, 0);
1679 expect(FALSE, ret);
1680 ok_sequence(sequences, PARENT_SEQ_INDEX, parent_expand_empty_kb_seq, "expand node with no children", FALSE);
1681
1682 DestroyWindow(hTree);
1683 }
1684
1685 static void test_expandedimage(void)
1686 {
1687 TVITEMEXA item;
1688 HWND hTree;
1689 BOOL ret;
1690
1691 hTree = create_treeview_control(0);
1692 fill_tree(hTree);
1693
1694 item.mask = TVIF_EXPANDEDIMAGE;
1695 item.iExpandedImage = 1;
1696 item.hItem = hRoot;
1697 ret = SendMessageA(hTree, TVM_SETITEMA, 0, (LPARAM)&item);
1698 ok(ret, "got %d\n", ret);
1699
1700 item.mask = TVIF_EXPANDEDIMAGE;
1701 item.iExpandedImage = -1;
1702 item.hItem = hRoot;
1703 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1704 ok(ret, "got %d\n", ret);
1705
1706 if (item.iExpandedImage != 1)
1707 {
1708 win_skip("TVIF_EXPANDEDIMAGE not supported\n");
1709 DestroyWindow(hTree);
1710 return;
1711 }
1712
1713 /* test for default iExpandedImage value */
1714 item.mask = TVIF_EXPANDEDIMAGE;
1715 item.iExpandedImage = -1;
1716 item.hItem = hChild;
1717 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1718 ok(ret, "got %d\n", ret);
1719 ok(item.iExpandedImage == (WORD)I_IMAGENONE, "got %d\n", item.iExpandedImage);
1720
1721 DestroyWindow(hTree);
1722 }
1723
1724 static void test_TVS_SINGLEEXPAND(void)
1725 {
1726 HWND hTree;
1727 HTREEITEM alpha, bravo, charlie, delta, echo, foxtrot, golf, hotel, india, juliet;
1728 TVINSERTSTRUCTA ins;
1729 char foo[] = "foo";
1730 char context[32];
1731 int i;
1732 BOOL ret;
1733
1734 /* build a fairly complex tree
1735 * - TVI_ROOT
1736 * - alpha
1737 * - bravo
1738 * - charlie
1739 * - delta
1740 * - echo
1741 * - foxtrot
1742 * - golf
1743 * - hotel
1744 * - india
1745 * - juliet
1746 */
1747 struct
1748 {
1749 HTREEITEM *handle;
1750 HTREEITEM *parent;
1751 UINT final_state;
1752 }
1753 items[] =
1754 {
1755 { &alpha, NULL, TVIS_EXPANDEDONCE },
1756 { &bravo, &alpha, TVIS_EXPANDEDONCE },
1757 { &charlie, &bravo, 0 },
1758 { &delta, &alpha, TVIS_EXPANDEDONCE },
1759 { &echo, &delta, 0 },
1760 { &foxtrot, NULL, TVIS_EXPANDEDONCE|TVIS_EXPANDED },
1761 { &golf, &foxtrot, TVIS_EXPANDEDONCE|TVIS_EXPANDED },
1762 { &hotel, &golf, 0 },
1763 { &india, &golf, TVIS_SELECTED },
1764 { &juliet, &foxtrot, 0 }
1765 };
1766
1767 struct
1768 {
1769 HTREEITEM *select;
1770 const struct message *sequence;
1771 }
1772 sequence_tests[] =
1773 {
1774 { &alpha, parent_singleexpand_seq0 },
1775 { &bravo, parent_singleexpand_seq1 },
1776 { &delta, parent_singleexpand_seq2 },
1777 { &foxtrot, parent_singleexpand_seq3 },
1778 { &alpha, parent_singleexpand_seq4 },
1779 { &golf, parent_singleexpand_seq5 },
1780 { &hotel, parent_singleexpand_seq6 },
1781 { &india, parent_singleexpand_seq7 },
1782 { &india, empty_seq }
1783 };
1784
1785 hTree = create_treeview_control(0);
1786 SetWindowLongA(hTree, GWL_STYLE, GetWindowLongA(hTree, GWL_STYLE) | TVS_SINGLEEXPAND);
1787 /* to avoid painting related notifications */
1788 ShowWindow(hTree, SW_HIDE);
1789 for (i = 0; i < sizeof(items)/sizeof(items[0]); i++)
1790 {
1791 ins.hParent = items[i].parent ? *items[i].parent : TVI_ROOT;
1792 ins.hInsertAfter = TVI_FIRST;
1793 U(ins).item.mask = TVIF_TEXT;
1794 U(ins).item.pszText = foo;
1795 *items[i].handle = TreeView_InsertItemA(hTree, &ins);
1796 }
1797
1798 for (i = 0; i < sizeof(sequence_tests)/sizeof(sequence_tests[0]); i++)
1799 {
1800 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1801 ret = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)(*sequence_tests[i].select));
1802 ok(ret, "got %d\n", ret);
1803 sprintf(context, "singleexpand notifications %d", i);
1804 ok_sequence(sequences, PARENT_SEQ_INDEX, sequence_tests[i].sequence, context, FALSE);
1805 }
1806
1807 for (i = 0; i < sizeof(items)/sizeof(items[0]); i++)
1808 {
1809 ret = SendMessageA(hTree, TVM_GETITEMSTATE, (WPARAM)(*items[i].handle), 0xFFFF);
1810 ok(ret == items[i].final_state, "singleexpand items[%d]: expected state 0x%x got 0x%x\n",
1811 i, items[i].final_state, ret);
1812 }
1813
1814 /* a workaround for NT4 that sends expand notifications when nothing is about to expand */
1815 ret = SendMessageA(hTree, TVM_DELETEITEM, 0, (LPARAM)TVI_ROOT);
1816 ok(ret, "got %d\n", ret);
1817 fill_tree(hTree);
1818 ret = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, 0);
1819 ok(ret, "got %d\n", ret);
1820
1821 DestroyWindow(hTree);
1822 }
1823
1824 static void test_WM_PAINT(void)
1825 {
1826 HWND hTree;
1827 COLORREF clr;
1828 LONG ret;
1829 RECT rc;
1830 HDC hdc;
1831
1832 hTree = create_treeview_control(0);
1833
1834 clr = SendMessageA(hTree, TVM_SETBKCOLOR, 0, RGB(255, 0, 0));
1835 ok(clr == ~0u, "got %d, expected -1\n", clr);
1836
1837 hdc = GetDC(hMainWnd);
1838
1839 GetClientRect(hMainWnd, &rc);
1840 FillRect(hdc, &rc, GetStockObject(BLACK_BRUSH));
1841
1842 clr = GetPixel(hdc, 1, 1);
1843 ok(clr == RGB(0, 0, 0), "got 0x%x\n", clr);
1844
1845 ret = SendMessageA(hTree, WM_PAINT, (WPARAM)hdc, 0);
1846 ok(ret == 0, "got %d\n", ret);
1847
1848 clr = GetPixel(hdc, 1, 1);
1849 ok(clr == RGB(255, 0, 0) || broken(clr == RGB(0, 0, 0)) /* win98 */,
1850 "got 0x%x\n", clr);
1851
1852 ReleaseDC(hMainWnd, hdc);
1853
1854 DestroyWindow(hTree);
1855 }
1856
1857 static void test_delete_items(void)
1858 {
1859 const struct message *msg;
1860 HWND hTree;
1861 INT ret;
1862
1863 hTree = create_treeview_control(0);
1864 fill_tree(hTree);
1865
1866 /* check delete order */
1867 flush_sequences(item_sequence, 1);
1868 ret = SendMessageA(hTree, TVM_DELETEITEM, 0, 0);
1869 ok(ret == TRUE, "got %d\n", ret);
1870
1871 msg = item_sequence[0]->sequence;
1872 ok(item_sequence[0]->count == 2, "expected 2 items, got %d\n", item_sequence[0]->count);
1873
1874 if (item_sequence[0]->count == 2)
1875 {
1876 ok(msg[0].lParam == (LPARAM)hChild, "expected %p, got 0x%lx\n", hChild, msg[0].lParam);
1877 ok(msg[1].lParam == (LPARAM)hRoot, "expected %p, got 0x%lx\n", hRoot, msg[1].lParam);
1878 }
1879
1880 ret = SendMessageA(hTree, TVM_GETCOUNT, 0, 0);
1881 ok(ret == 0, "got %d\n", ret);
1882
1883 DestroyWindow(hTree);
1884 }
1885
1886 static void test_cchildren(void)
1887 {
1888 HWND hTree;
1889 INT ret;
1890 TVITEMA item;
1891
1892 hTree = create_treeview_control(0);
1893 fill_tree(hTree);
1894
1895 ret = SendMessageA(hTree, TVM_DELETEITEM, 0, (LPARAM)hChild);
1896 expect(TRUE, ret);
1897
1898 /* check cChildren - automatic mode */
1899 item.hItem = hRoot;
1900 item.mask = TVIF_CHILDREN;
1901
1902 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1903 expect(TRUE, ret);
1904 expect(0, item.cChildren);
1905
1906 DestroyWindow(hTree);
1907
1908 /* start over */
1909 hTree = create_treeview_control(0);
1910 fill_tree(hTree);
1911
1912 /* turn off automatic mode by setting cChildren explicitly */
1913 item.hItem = hRoot;
1914 item.mask = TVIF_CHILDREN;
1915
1916 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1917 expect(TRUE, ret);
1918 expect(1, item.cChildren);
1919
1920 ret = SendMessageA(hTree, TVM_SETITEMA, 0, (LPARAM)&item);
1921 expect(TRUE, ret);
1922
1923 ret = SendMessageA(hTree, TVM_DELETEITEM, 0, (LPARAM)hChild);
1924 expect(TRUE, ret);
1925
1926 /* check cChildren */
1927 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1928 expect(TRUE, ret);
1929 todo_wine
1930 expect(1, item.cChildren);
1931
1932 DestroyWindow(hTree);
1933 }
1934
1935 struct _ITEM_DATA
1936 {
1937 HTREEITEM parent; /* for root value of parent field is unidetified */
1938 HTREEITEM nextsibling;
1939 HTREEITEM firstchild;
1940 };
1941
1942 static void _check_item(HTREEITEM item, HTREEITEM parent, HTREEITEM nextsibling, HTREEITEM firstchild, int line)
1943 {
1944 struct _ITEM_DATA *data = (struct _ITEM_DATA*)item;
1945
1946 ok_(__FILE__, line)(data->parent == parent, "parent %p, got %p\n", parent, data->parent);
1947 ok_(__FILE__, line)(data->nextsibling == nextsibling, "sibling %p, got %p\n", nextsibling, data->nextsibling);
1948 ok_(__FILE__, line)(data->firstchild == firstchild, "firstchild %p, got %p\n", firstchild, data->firstchild);
1949 }
1950
1951 #define check_item(a, b, c, d) _check_item(a, b, c, d, __LINE__)
1952
1953 static void test_htreeitem_layout(void)
1954 {
1955 TVINSERTSTRUCTA ins;
1956 HTREEITEM item1, item2;
1957 HWND hTree;
1958
1959 hTree = create_treeview_control(0);
1960 fill_tree(hTree);
1961
1962 /* root has some special pointer in parent field */
1963 check_item(hRoot, ((struct _ITEM_DATA*)hRoot)->parent, 0, hChild);
1964 check_item(hChild, hRoot, 0, 0);
1965
1966 ins.hParent = hChild;
1967 ins.hInsertAfter = TVI_FIRST;
1968 U(ins).item.mask = 0;
1969 item1 = TreeView_InsertItemA(hTree, &ins);
1970
1971 check_item(item1, hChild, 0, 0);
1972
1973 ins.hParent = hRoot;
1974 ins.hInsertAfter = TVI_FIRST;
1975 U(ins).item.mask = 0;
1976 item2 = TreeView_InsertItemA(hTree, &ins);
1977
1978 check_item(item2, hRoot, hChild, 0);
1979
1980 SendMessageA(hTree, TVM_DELETEITEM, 0, (LPARAM)hChild);
1981
1982 /* without children now */
1983 check_item(hRoot, ((struct _ITEM_DATA*)hRoot)->parent, 0, item2);
1984
1985 DestroyWindow(hTree);
1986 }
1987
1988 static void test_TVS_CHECKBOXES(void)
1989 {
1990 HIMAGELIST himl, himl2;
1991 HWND hTree, hTree2;
1992 TVITEMA item;
1993 DWORD ret;
1994 MSG msg;
1995
1996 hTree = create_treeview_control(0);
1997 fill_tree(hTree);
1998
1999 himl = (HIMAGELIST)SendMessageA(hTree, TVM_GETIMAGELIST, TVSIL_STATE, 0);
2000 ok(himl == NULL, "got %p\n", himl);
2001
2002 item.hItem = hRoot;
2003 item.mask = TVIF_STATE;
2004 item.state = INDEXTOSTATEIMAGEMASK(1);
2005 item.stateMask = TVIS_STATEIMAGEMASK;
2006 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
2007 expect(TRUE, ret);
2008 ok(item.state == 0, "got 0x%x\n", item.state);
2009
2010 /* set some index for a child */
2011 item.hItem = hChild;
2012 item.mask = TVIF_STATE;
2013 item.state = INDEXTOSTATEIMAGEMASK(4);
2014 item.stateMask = TVIS_STATEIMAGEMASK;
2015 ret = SendMessageA(hTree, TVM_SETITEMA, 0, (LPARAM)&item);
2016 expect(TRUE, ret);
2017
2018 /* enabling check boxes set all items to 1 state image index */
2019 SetWindowLongA(hTree, GWL_STYLE, GetWindowLongA(hTree, GWL_STYLE) | TVS_CHECKBOXES);
2020 himl = (HIMAGELIST)SendMessageA(hTree, TVM_GETIMAGELIST, TVSIL_STATE, 0);
2021 ok(himl != NULL, "got %p\n", himl);
2022
2023 himl2 = (HIMAGELIST)SendMessageA(hTree, TVM_GETIMAGELIST, TVSIL_STATE, 0);
2024 ok(himl2 != NULL, "got %p\n", himl2);
2025 ok(himl2 == himl, "got %p, expected %p\n", himl2, himl);
2026
2027 item.hItem = hRoot;
2028 item.mask = TVIF_STATE;
2029 item.state = 0;
2030 item.stateMask = TVIS_STATEIMAGEMASK;
2031 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
2032 expect(TRUE, ret);
2033 ok(item.state == INDEXTOSTATEIMAGEMASK(1), "got 0x%x\n", item.state);
2034
2035 item.hItem = hChild;
2036 item.mask = TVIF_STATE;
2037 item.state = 0;
2038 item.stateMask = TVIS_STATEIMAGEMASK;
2039 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
2040 expect(TRUE, ret);
2041 ok(item.state == INDEXTOSTATEIMAGEMASK(1), "got 0x%x\n", item.state);
2042
2043 /* create another control and check its checkbox list */
2044 hTree2 = create_treeview_control(0);
2045 fill_tree(hTree2);
2046
2047 /* set some index for a child */
2048 item.hItem = hChild;
2049 item.mask = TVIF_STATE;
2050 item.state = INDEXTOSTATEIMAGEMASK(4);
2051 item.stateMask = TVIS_STATEIMAGEMASK;
2052 ret = SendMessageA(hTree2, TVM_SETITEMA, 0, (LPARAM)&item);
2053 expect(TRUE, ret);
2054
2055 /* enabling check boxes set all items to 1 state image index */
2056 SetWindowLongA(hTree2, GWL_STYLE, GetWindowLongA(hTree, GWL_STYLE) | TVS_CHECKBOXES);
2057 himl2 = (HIMAGELIST)SendMessageA(hTree2, TVM_GETIMAGELIST, TVSIL_STATE, 0);
2058 ok(himl2 != NULL, "got %p\n", himl2);
2059 ok(himl != himl2, "got %p, expected %p\n", himl2, himl);
2060
2061 DestroyWindow(hTree2);
2062 DestroyWindow(hTree);
2063
2064 /* the same, but initially created with TVS_CHECKBOXES */
2065 hTree = create_treeview_control(TVS_CHECKBOXES);
2066 fill_tree(hTree);
2067 himl = (HIMAGELIST)SendMessageA(hTree, TVM_GETIMAGELIST, TVSIL_STATE, 0);
2068 ok(himl == NULL, "got %p\n", himl);
2069
2070 item.hItem = hRoot;
2071 item.mask = TVIF_STATE;
2072 item.state = 0;
2073 item.stateMask = TVIS_STATEIMAGEMASK;
2074 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
2075 expect(TRUE, ret);
2076 ok(item.state == INDEXTOSTATEIMAGEMASK(1), "got 0x%x\n", item.state);
2077
2078 item.hItem = hChild;
2079 item.mask = TVIF_STATE;
2080 item.state = 0;
2081 item.stateMask = TVIS_STATEIMAGEMASK;
2082 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
2083 expect(TRUE, ret);
2084 ok(item.state == INDEXTOSTATEIMAGEMASK(1), "got 0x%x\n", item.state);
2085
2086 item.hItem = hChild;
2087 item.mask = TVIF_STATE;
2088 item.state = INDEXTOSTATEIMAGEMASK(2);
2089 item.stateMask = TVIS_STATEIMAGEMASK;
2090 ret = SendMessageA(hTree, TVM_SETITEMA, 0, (LPARAM)&item);
2091 expect(TRUE, ret);
2092
2093 item.hItem = hChild;
2094 item.mask = TVIF_STATE;
2095 item.state = 0;
2096 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
2097 expect(TRUE, ret);
2098 ok(item.state == INDEXTOSTATEIMAGEMASK(2), "got 0x%x\n", item.state);
2099
2100 while(GetMessageA(&msg, 0, 0, 0))
2101 {
2102 TranslateMessage(&msg);
2103 DispatchMessageA(&msg);
2104
2105 if((msg.hwnd == hTree) && (msg.message == WM_PAINT))
2106 break;
2107 }
2108
2109 item.hItem = hChild;
2110 item.mask = TVIF_STATE;
2111 item.state = 0;
2112 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
2113 expect(TRUE, ret);
2114 ok(item.state == INDEXTOSTATEIMAGEMASK(1), "got 0x%x\n", item.state);
2115
2116 himl = (HIMAGELIST)SendMessageA(hTree, TVM_GETIMAGELIST, TVSIL_STATE, 0);
2117 ok(himl != NULL, "got %p\n", himl);
2118
2119 DestroyWindow(hTree);
2120
2121 /* check what happens if TVSIL_STATE image list is removed */
2122 hTree = create_treeview_control(0);
2123 fill_tree(hTree);
2124 himl = (HIMAGELIST)SendMessageA(hTree, TVM_GETIMAGELIST, TVSIL_STATE, 0);
2125 ok(himl == NULL, "got %p\n", himl);
2126
2127 SetWindowLongA(hTree, GWL_STYLE, GetWindowLongA(hTree, GWL_STYLE) | TVS_CHECKBOXES);
2128 himl = (HIMAGELIST)SendMessageA(hTree, TVM_GETIMAGELIST, TVSIL_STATE, 0);
2129 ok(himl != NULL, "got %p\n", himl);
2130
2131 himl2 = (HIMAGELIST)SendMessageA(hTree, TVM_SETIMAGELIST, TVSIL_STATE, 0);
2132 ok(himl2 == himl, "got %p\n", himl2);
2133
2134 himl2 = (HIMAGELIST)SendMessageA(hTree, TVM_GETIMAGELIST, TVSIL_STATE, 0);
2135 ok(himl2 == NULL, "got %p\n", himl2);
2136
2137 item.hItem = hChild;
2138 item.mask = TVIF_STATE;
2139 item.state = INDEXTOSTATEIMAGEMASK(2);
2140 item.stateMask = TVIS_STATEIMAGEMASK;
2141 ret = SendMessageA(hTree, TVM_SETITEMA, 0, (LPARAM)&item);
2142 expect(TRUE, ret);
2143
2144 item.hItem = hChild;
2145 item.mask = TVIF_STATE;
2146 item.state = 0;
2147 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
2148 expect(TRUE, ret);
2149 ok(item.state == INDEXTOSTATEIMAGEMASK(2), "got 0x%x\n", item.state);
2150
2151 while(GetMessageA(&msg, 0, 0, 0))
2152 {
2153 TranslateMessage(&msg);
2154 DispatchMessageA(&msg);
2155
2156 if((msg.hwnd == hTree) && (msg.message == WM_PAINT))
2157 break;
2158 }
2159
2160 item.hItem = hChild;
2161 item.mask = TVIF_STATE;
2162 item.state = 0;
2163 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
2164 expect(TRUE, ret);
2165 ok(item.state == INDEXTOSTATEIMAGEMASK(1), "got 0x%x\n", item.state);
2166
2167 himl = (HIMAGELIST)SendMessageA(hTree, TVM_GETIMAGELIST, TVSIL_STATE, 0);
2168 ok(himl != NULL, "got %p\n", himl);
2169
2170 DestroyWindow(hTree);
2171 }
2172
2173 static void test_TVM_GETNEXTITEM(void)
2174 {
2175 HTREEITEM item;
2176 HWND hTree;
2177
2178 hTree = create_treeview_control(0);
2179 fill_tree(hTree);
2180
2181 item = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_ROOT, 0);
2182 ok(item == hRoot, "got %p, expected %p\n", item, hRoot);
2183
2184 item = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_ROOT, (LPARAM)TVI_ROOT);
2185 ok(item == hRoot, "got %p, expected %p\n", item, hRoot);
2186
2187 item = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_ROOT, (LPARAM)hRoot);
2188 ok(item == hRoot, "got %p, expected %p\n", item, hRoot);
2189
2190 item = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_ROOT, (LPARAM)hChild);
2191 ok(item == hRoot, "got %p, expected %p\n", item, hRoot);
2192
2193 item = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_CHILD, 0);
2194 ok(item == hRoot, "got %p, expected %p\n", item, hRoot);
2195
2196 item = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hRoot);
2197 ok(item == hChild, "got %p, expected %p\n", item, hChild);
2198
2199 item = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)TVI_ROOT);
2200 ok(item == hRoot, "got %p, expected %p\n", item, hRoot);
2201
2202 item = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_PARENT, 0);
2203 ok(item == NULL, "got %p\n", item);
2204
2205 item = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_PARENT, (LPARAM)hChild);
2206 ok(item == hRoot, "got %p, expected %p\n", item, hRoot);
2207
2208 DestroyWindow(hTree);
2209 }
2210
2211 static void test_TVM_HITTEST(void)
2212 {
2213 HWND hTree;
2214 LRESULT ret;
2215 RECT rc;
2216 TVHITTESTINFO ht;
2217
2218 hTree = create_treeview_control(0);
2219 fill_tree(hTree);
2220
2221 *(HTREEITEM*)&rc = hRoot;
2222 ret = SendMessageA(hTree, TVM_GETITEMRECT, TRUE, (LPARAM)&rc);
2223 expect(TRUE, (BOOL)ret);
2224
2225 ht.pt.x = rc.left-1;
2226 ht.pt.y = rc.top;
2227
2228 ret = SendMessageA(hTree, TVM_HITTEST, 0, (LPARAM)&ht);
2229 ok((HTREEITEM)ret == hRoot, "got %p, expected %p\n", (HTREEITEM)ret, hRoot);
2230 ok(ht.hItem == hRoot, "got %p, expected %p\n", ht.hItem, hRoot);
2231 ok(ht.flags == TVHT_ONITEMBUTTON, "got %d, expected %d\n", ht.flags, TVHT_ONITEMBUTTON);
2232
2233 ret = SendMessageA(hTree, TVM_EXPAND, TVE_EXPAND, (LPARAM)hRoot);
2234 expect(TRUE, (BOOL)ret);
2235
2236 *(HTREEITEM*)&rc = hChild;
2237 ret = SendMessageA(hTree, TVM_GETITEMRECT, TRUE, (LPARAM)&rc);
2238 expect(TRUE, (BOOL)ret);
2239
2240 ht.pt.x = rc.left-1;
2241 ht.pt.y = rc.top;
2242
2243 ret = SendMessageA(hTree, TVM_HITTEST, 0, (LPARAM)&ht);
2244 ok((HTREEITEM)ret == hChild, "got %p, expected %p\n", (HTREEITEM)ret, hChild);
2245 ok(ht.hItem == hChild, "got %p, expected %p\n", ht.hItem, hChild);
2246 /* Wine returns item button here, but this item has no button */
2247 todo_wine ok(ht.flags == TVHT_ONITEMINDENT, "got %d, expected %d\n", ht.flags, TVHT_ONITEMINDENT);
2248
2249 DestroyWindow(hTree);
2250 }
2251
2252 static void test_WM_GETDLGCODE(void)
2253 {
2254 DWORD code;
2255 HWND hTree;
2256
2257 hTree = create_treeview_control(0);
2258
2259 code = SendMessageA(hTree, WM_GETDLGCODE, VK_TAB, 0);
2260 ok(code == (DLGC_WANTCHARS | DLGC_WANTARROWS), "0x%08x\n", code);
2261
2262 DestroyWindow(hTree);
2263 }
2264
2265 static void test_customdraw(void)
2266 {
2267 static const char *rootA = "root";
2268 TVINSERTSTRUCTA ins;
2269 HTREEITEM hRoot;
2270 LOGFONTA lf;
2271 HWND hwnd;
2272
2273 hwnd = create_treeview_control(0);
2274
2275 ins.hParent = TVI_ROOT;
2276 ins.hInsertAfter = TVI_ROOT;
2277 U(ins).item.mask = TVIF_TEXT;
2278 U(ins).item.pszText = (char*)rootA;
2279 hRoot = TreeView_InsertItemA(hwnd, &ins);
2280 ok(hRoot != NULL, "got %p\n", hRoot);
2281
2282 /* create additional font, custom draw handler will select it */
2283 SystemParametersInfoA(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
2284 lf.lfHeight *= 2;
2285 g_customdraw_font = CreateFontIndirectA(&lf);
2286 flush_sequences(sequences, NUM_MSG_SEQUENCES);
2287 InvalidateRect(hwnd, NULL, TRUE);
2288 UpdateWindow(hwnd);
2289 ok_sequence(sequences, PARENT_CD_SEQ_INDEX, parent_cd_seq, "custom draw notifications", FALSE);
2290 DeleteObject(g_customdraw_font);
2291 g_customdraw_font = NULL;
2292
2293 DestroyWindow(hwnd);
2294 }
2295
2296 static void test_WM_KEYDOWN(void)
2297 {
2298 static const char *rootA = "root";
2299 TVINSERTSTRUCTA ins;
2300 HTREEITEM hRoot;
2301 HWND hwnd;
2302
2303 hwnd = create_treeview_control(0);
2304
2305 ins.hParent = TVI_ROOT;
2306 ins.hInsertAfter = TVI_ROOT;
2307 U(ins).item.mask = TVIF_TEXT;
2308 U(ins).item.pszText = (char*)rootA;
2309 hRoot = TreeView_InsertItemA(hwnd, &ins);
2310 ok(hRoot != NULL, "got %p\n", hRoot);
2311
2312 flush_sequences(sequences, NUM_MSG_SEQUENCES);
2313 SendMessageA(hwnd, WM_KEYDOWN, VK_RETURN, 0);
2314 ok_sequence(sequences, PARENT_SEQ_INDEX, parent_vk_return_seq, "WM_KEYDOWN/VK_RETURN parent notification", TRUE);
2315
2316 DestroyWindow(hwnd);
2317 }
2318
2319 START_TEST(treeview)
2320 {
2321 HMODULE hComctl32;
2322 BOOL (WINAPI *pInitCommonControlsEx)(const INITCOMMONCONTROLSEX*);
2323 WNDCLASSA wc;
2324 MSG msg;
2325
2326 ULONG_PTR ctx_cookie;
2327 HANDLE hCtx;
2328
2329 hComctl32 = GetModuleHandleA("comctl32.dll");
2330 pInitCommonControlsEx = (void*)GetProcAddress(hComctl32, "InitCommonControlsEx");
2331 if (pInitCommonControlsEx)
2332 {
2333 INITCOMMONCONTROLSEX iccex;
2334 iccex.dwSize = sizeof(iccex);
2335 iccex.dwICC = ICC_TREEVIEW_CLASSES;
2336 pInitCommonControlsEx(&iccex);
2337 }
2338 else
2339 InitCommonControls();
2340
2341 init_msg_sequences(sequences, NUM_MSG_SEQUENCES);
2342 init_msg_sequences(item_sequence, 1);
2343
2344 wc.style = CS_HREDRAW | CS_VREDRAW;
2345 wc.cbClsExtra = 0;
2346 wc.cbWndExtra = 0;
2347 wc.hInstance = GetModuleHandleA(NULL);
2348 wc.hIcon = NULL;
2349 wc.hCursor = LoadCursorA(NULL, (LPCSTR)IDC_IBEAM);
2350 wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
2351 wc.lpszMenuName = NULL;
2352 wc.lpszClassName = "MyTestWnd";
2353 wc.lpfnWndProc = parent_wnd_proc;
2354 RegisterClassA(&wc);
2355
2356 hMainWnd = CreateWindowExA(0, "MyTestWnd", "Blah", WS_OVERLAPPEDWINDOW,
2357 CW_USEDEFAULT, CW_USEDEFAULT, 130, 105, NULL, NULL, GetModuleHandleA(NULL), 0);
2358
2359 ok(hMainWnd != NULL, "Failed to create parent window. Tests aborted.\n");
2360 if (!hMainWnd) return;
2361
2362 test_fillroot();
2363 test_select();
2364 test_getitemtext();
2365 test_focus();
2366 test_get_set_bkcolor();
2367 test_get_set_imagelist();
2368 test_get_set_indent();
2369 test_get_set_insertmark();
2370 test_get_set_item();
2371 test_get_set_itemheight();
2372 test_get_set_scrolltime();
2373 test_get_set_textcolor();
2374 test_get_linecolor();
2375 test_get_insertmarkcolor();
2376 test_get_set_tooltips();
2377 test_get_set_unicodeformat();
2378 test_callback();
2379 test_expandinvisible();
2380 test_itemedit();
2381 test_treeview_classinfo();
2382 test_expandnotify();
2383 test_TVS_SINGLEEXPAND();
2384 test_WM_PAINT();
2385 test_delete_items();
2386 test_cchildren();
2387 test_htreeitem_layout();
2388 test_TVS_CHECKBOXES();
2389 test_TVM_GETNEXTITEM();
2390 test_TVM_HITTEST();
2391 test_WM_GETDLGCODE();
2392 test_customdraw();
2393 test_WM_KEYDOWN();
2394
2395 if (!load_v6_module(&ctx_cookie, &hCtx))
2396 {
2397 DestroyWindow(hMainWnd);
2398 return;
2399 }
2400
2401 /* comctl32 version 6 tests start here */
2402 test_expandedimage();
2403 test_htreeitem_layout();
2404 test_WM_GETDLGCODE();
2405
2406 unload_v6_module(ctx_cookie, hCtx);
2407
2408 PostMessageA(hMainWnd, WM_CLOSE, 0, 0);
2409 while(GetMessageA(&msg, 0, 0, 0))
2410 {
2411 TranslateMessage(&msg);
2412 DispatchMessageA(&msg);
2413 }
2414 }