mfplat: Read queue subscriber within the critical section.
[wine/zf.git] / dlls / comctl32 / tests / treeview.c
blob02d914c249c863a0ec7ad9744ed23f148e66ffc3
1 /* Unit tests for treeview.
3 * Copyright 2005 Krzysztof Foltman
4 * Copyright 2007 Christopher James Peterson
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.
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.
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
21 #include <stdarg.h>
22 #include <stdio.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "winuser.h"
28 #include "winnls.h"
29 #include "winreg.h"
30 #include "commctrl.h"
32 #include "wine/test.h"
33 #include "v6util.h"
34 #include "msg.h"
36 static BOOL (WINAPI *pInitCommonControlsEx)(const INITCOMMONCONTROLSEX*);
37 static const char *TEST_CALLBACK_TEXT = "callback_text";
39 static TVITEMA g_item_expanding, g_item_expanded;
40 static BOOL g_get_from_expand;
41 static BOOL g_get_rect_in_expand;
42 static BOOL g_disp_A_to_W;
43 static BOOL g_disp_set_stateimage;
44 static BOOL g_beginedit_alter_text;
45 static const char *g_endedit_overwrite_contents;
46 static char *g_endedit_overwrite_ptr;
47 static HFONT g_customdraw_font;
48 static BOOL g_v6;
50 #define NUM_MSG_SEQUENCES 3
51 #define TREEVIEW_SEQ_INDEX 0
52 #define PARENT_SEQ_INDEX 1
53 #define PARENT_CD_SEQ_INDEX 2
55 #define expect(expected, got) ok(got == expected, "Expected %d, got %d\n", expected, got)
57 static struct msg_sequence *sequences[NUM_MSG_SEQUENCES];
58 static struct msg_sequence *item_sequence[1];
60 static void flush_events(void)
62 MSG msg;
63 int diff = 200;
64 int min_timeout = 100;
65 DWORD time = GetTickCount() + diff;
67 while (diff > 0)
69 if (MsgWaitForMultipleObjects(0, NULL, FALSE, min_timeout, QS_ALLINPUT) == WAIT_TIMEOUT) break;
70 while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
71 diff = time - GetTickCount();
75 static const struct message FillRootSeq[] = {
76 { TVM_INSERTITEMA, sent },
77 { TVM_INSERTITEMA, sent },
78 { 0 }
81 static const struct message rootnone_select_seq[] = {
82 { TVM_SELECTITEM, sent|wparam, 9 },
83 { TVM_SELECTITEM, sent|wparam, 9 },
84 { TVM_SELECTITEM, sent|wparam, 9 },
85 { TVM_SELECTITEM, sent|wparam, 9 },
86 { TVM_SELECTITEM, sent|wparam, 9 },
87 { TVM_SELECTITEM, sent|wparam, 9 },
88 { 0 }
91 static const struct message rootchild_select_seq[] = {
92 { TVM_SELECTITEM, sent|wparam, 9 },
93 { TVM_SELECTITEM, sent|wparam, 9 },
94 { TVM_SELECTITEM, sent|wparam, 9 },
95 { TVM_SELECTITEM, sent|wparam, 9 },
96 { TVM_SELECTITEM, sent|wparam, 9 },
97 { TVM_SELECTITEM, sent|wparam, 9 },
98 { 0 }
101 static const struct message getitemtext_seq[] = {
102 { TVM_INSERTITEMA, sent },
103 { TVM_GETITEMA, sent },
104 { TVM_DELETEITEM, sent },
105 { 0 }
108 static const struct message focus_seq[] = {
109 { TVM_INSERTITEMA, sent },
110 { TVM_INSERTITEMA, sent },
111 { TVM_SELECTITEM, sent|wparam, 9 },
112 /* The following end up out of order in wine */
113 { WM_WINDOWPOSCHANGING, sent|defwinproc },
114 { WM_NCCALCSIZE, sent|wparam|defwinproc, TRUE },
115 { WM_WINDOWPOSCHANGED, sent|defwinproc },
116 { WM_SIZE, sent|defwinproc },
117 { WM_WINDOWPOSCHANGING, sent|defwinproc|optional },
118 { WM_NCCALCSIZE, sent|wparam|defwinproc|optional, TRUE },
119 { WM_WINDOWPOSCHANGED, sent|defwinproc|optional },
120 { WM_SIZE, sent|defwinproc|optional },
121 { WM_PAINT, sent|defwinproc },
122 { WM_NCPAINT, sent|wparam|defwinproc, 1 },
123 { WM_ERASEBKGND, sent|defwinproc },
124 { TVM_EDITLABELA, sent },
125 { WM_COMMAND, sent|wparam|defwinproc, MAKEWPARAM(0, EN_UPDATE) },
126 { WM_COMMAND, sent|wparam|defwinproc, MAKEWPARAM(0, EN_CHANGE) },
127 { WM_PARENTNOTIFY, sent|wparam|defwinproc, MAKEWPARAM(WM_CREATE, 0) },
128 { WM_KILLFOCUS, sent|defwinproc },
129 { WM_PAINT, sent|defwinproc },
130 { WM_IME_SETCONTEXT, sent|defwinproc|optional },
131 { WM_COMMAND, sent|wparam|defwinproc, MAKEWPARAM(0, EN_SETFOCUS) },
132 { WM_ERASEBKGND, sent|defwinproc|optional },
133 { WM_CTLCOLOREDIT, sent|defwinproc|optional },
134 { WM_CTLCOLOREDIT, sent|defwinproc|optional },
135 { 0 }
138 static const struct message test_get_set_bkcolor_seq[] = {
139 { TVM_GETBKCOLOR, sent|wparam|lparam, 0, 0 },
140 { TVM_SETBKCOLOR, sent|wparam|lparam, 0, 0 },
141 { TVM_GETBKCOLOR, sent|wparam|lparam, 0, 0 },
142 { TVM_SETBKCOLOR, sent|wparam|lparam, 0, 0x00ffffff },
143 { TVM_GETBKCOLOR, sent|wparam|lparam, 0, 0 },
144 { TVM_SETBKCOLOR, sent|wparam|lparam, 0, -1 },
145 { 0 }
148 static const struct message test_get_set_imagelist_seq[] = {
149 { TVM_SETIMAGELIST, sent|wparam|lparam, 0, 0 },
150 { TVM_GETIMAGELIST, sent|wparam|lparam, 0, 0 },
151 { 0 }
154 static const struct message test_get_set_indent_seq[] = {
155 { TVM_SETINDENT, sent|wparam|lparam, 0, 0 },
156 { TVM_GETINDENT, sent|wparam|lparam, 0, 0 },
157 /* The actual amount to indent is dependent on the system for this message */
158 { TVM_SETINDENT, sent },
159 { TVM_GETINDENT, sent|wparam|lparam, 0, 0 },
160 { 0 }
163 static const struct message test_get_set_insertmarkcolor_seq[] = {
164 { TVM_SETINSERTMARKCOLOR, sent|wparam|lparam, 0, 0 },
165 { TVM_GETINSERTMARKCOLOR, sent|wparam|lparam, 0, 0 },
166 { 0 }
169 static const struct message test_get_set_item_seq[] = {
170 { TVM_GETITEMA, sent },
171 { TVM_SETITEMA, sent },
172 { TVM_GETITEMA, sent },
173 { TVM_SETITEMA, sent },
174 { 0 }
177 static const struct message test_get_set_itemheight_seq[] = {
178 { TVM_GETITEMHEIGHT, sent|wparam|lparam, 0, 0 },
179 { TVM_SETITEMHEIGHT, sent|wparam|lparam, -1, 0 },
180 { TVM_GETITEMHEIGHT, sent|wparam|lparam, 0, 0 },
181 { TVM_SETITEMHEIGHT, sent|lparam, 0xcccccccc, 0 },
182 { TVM_GETITEMHEIGHT, sent|wparam|lparam|optional, 0, 0 },
183 { TVM_SETITEMHEIGHT, sent|wparam|lparam|optional, 9, 0 },
184 { TVM_GETITEMHEIGHT, sent|wparam|lparam, 0, 0 },
185 { 0 }
188 static const struct message test_get_set_scrolltime_seq[] = {
189 { TVM_SETSCROLLTIME, sent|wparam|lparam, 20, 0 },
190 { TVM_GETSCROLLTIME, sent|wparam|lparam, 0, 0 },
191 { 0 }
194 static const struct message test_get_set_textcolor_seq[] = {
195 { TVM_GETTEXTCOLOR, sent|wparam|lparam, 0, 0 },
196 { TVM_SETTEXTCOLOR, sent|wparam|lparam, 0, 0 },
197 { TVM_GETTEXTCOLOR, sent|wparam|lparam, 0, 0 },
198 { TVM_SETTEXTCOLOR, sent|wparam|lparam, 0, RGB(255, 255, 255) },
199 { TVM_GETTEXTCOLOR, sent|wparam|lparam, 0, 0 },
200 { TVM_SETTEXTCOLOR, sent|wparam|lparam, 0, CLR_NONE },
201 { 0 }
204 static const struct message test_get_set_tooltips_seq[] = {
205 { WM_KILLFOCUS, sent },
206 { WM_IME_SETCONTEXT, sent|optional },
207 { WM_IME_NOTIFY, sent|optional },
208 { TVM_SETTOOLTIPS, sent|wparam|lparam, 0, 0 },
209 { TVM_GETTOOLTIPS, sent|wparam|lparam, 0, 0 },
210 { 0 }
213 static const struct message test_get_set_unicodeformat_seq[] = {
214 { TVM_SETUNICODEFORMAT, sent|wparam|lparam, TRUE, 0 },
215 { TVM_GETUNICODEFORMAT, sent|wparam|lparam, 0, 0 },
216 { TVM_SETUNICODEFORMAT, sent|wparam|lparam, 0, 0 },
217 { TVM_GETUNICODEFORMAT, sent|wparam|lparam, 0, 0 },
218 { TVM_SETUNICODEFORMAT, sent|wparam|lparam, 0, 0 },
219 { 0 }
222 static const struct message test_right_click_seq[] = {
223 { WM_RBUTTONDOWN, sent|wparam, MK_RBUTTON },
224 { WM_CAPTURECHANGED, sent|defwinproc },
225 { TVM_GETNEXTITEM, sent|wparam|lparam|defwinproc, TVGN_CARET, 0 },
226 { WM_NCHITTEST, sent|optional },
227 { WM_SETCURSOR, sent|optional },
228 { WM_MOUSEMOVE, sent|optional },
229 { 0 }
232 static const struct message parent_expand_seq[] = {
233 { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDINGA },
234 { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDEDA },
235 { 0 }
238 static const struct message parent_expand_kb_seq[] = {
239 { WM_NOTIFY, sent|id, 0, 0, TVN_KEYDOWN },
240 { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDINGA },
241 { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDEDA },
242 { WM_CHANGEUISTATE, sent|optional },
243 { 0 }
246 static const struct message parent_collapse_2nd_kb_seq[] = {
247 { WM_NOTIFY, sent|id|optional, 0, 0, TVN_KEYDOWN },
248 { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDINGA },
249 { WM_CHANGEUISTATE, sent|optional },
250 { 0 }
253 static const struct message parent_expand_empty_kb_seq[] = {
254 { WM_NOTIFY, sent|id, 0, 0, TVN_KEYDOWN },
255 { WM_CHANGEUISTATE, sent|optional },
256 { 0 }
259 static const struct message parent_singleexpand_seq0[] = {
260 /* alpha expands */
261 { WM_NOTIFY, sent|id, 0, 0, TVN_SELCHANGINGA },
262 { WM_NOTIFY, sent|id, 0, 0, TVN_SELCHANGEDA },
263 { WM_NOTIFY, sent|id, 0, 0, TVN_SINGLEEXPAND },
264 { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDINGA },
265 { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDEDA },
266 { 0 }
269 static const struct message parent_singleexpand_seq1[] = {
270 /* bravo expands */
271 { WM_NOTIFY, sent|id, 0, 0, TVN_SELCHANGINGA },
272 { WM_NOTIFY, sent|id, 0, 0, TVN_SELCHANGEDA },
273 { WM_NOTIFY, sent|id, 0, 0, TVN_SINGLEEXPAND },
274 { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDINGA },
275 { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDEDA },
276 { 0 }
279 static const struct message parent_singleexpand_seq2[] = {
280 /* delta expands, bravo collapses */
281 { WM_NOTIFY, sent|id, 0, 0, TVN_SELCHANGINGA },
282 { WM_NOTIFY, sent|id, 0, 0, TVN_SELCHANGEDA },
283 { WM_NOTIFY, sent|id, 0, 0, TVN_SINGLEEXPAND },
284 { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDINGA },
285 { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDEDA },
286 { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDINGA },
287 { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDEDA },
288 { 0 }
291 static const struct message parent_singleexpand_seq3[] = {
292 /* foxtrot expands, alpha and delta collapse */
293 { WM_NOTIFY, sent|id, 0, 0, TVN_SELCHANGINGA },
294 { WM_NOTIFY, sent|id, 0, 0, TVN_SELCHANGEDA },
295 { WM_NOTIFY, sent|id, 0, 0, TVN_SINGLEEXPAND },
296 { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDINGA },
297 { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDEDA },
298 { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDINGA },
299 { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDEDA },
300 { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDINGA },
301 { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDEDA },
302 { 0 }
305 static const struct message parent_singleexpand_seq4[] = {
306 /* alpha expands, foxtrot collapses */
307 { WM_NOTIFY, sent|id, 0, 0, TVN_SELCHANGINGA },
308 { WM_NOTIFY, sent|id, 0, 0, TVN_SELCHANGEDA },
309 { WM_NOTIFY, sent|id, 0, 0, TVN_SINGLEEXPAND },
310 { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDINGA },
311 { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDEDA },
312 { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDINGA },
313 { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDEDA },
314 { 0 }
317 static const struct message parent_singleexpand_seq5[] = {
318 /* foxtrot expands while golf is selected, then golf expands and alpha collapses */
319 { WM_NOTIFY, sent|id, 0, 0, TVN_SELCHANGINGA },
320 { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDINGA },
321 { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDEDA },
322 { WM_NOTIFY, sent|id, 0, 0, TVN_SELCHANGEDA },
323 { WM_NOTIFY, sent|id, 0, 0, TVN_SINGLEEXPAND },
324 { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDINGA },
325 { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDEDA },
326 { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDINGA },
327 { WM_NOTIFY, sent|id, 0, 0, TVN_ITEMEXPANDEDA },
328 { 0 }
331 static const struct message parent_singleexpand_seq6[] = {
332 /* hotel does not expand and india does not collapse because they have no children */
333 { WM_NOTIFY, sent|id, 0, 0, TVN_SELCHANGINGA },
334 { WM_NOTIFY, sent|id, 0, 0, TVN_SELCHANGEDA },
335 { WM_NOTIFY, sent|id, 0, 0, TVN_SINGLEEXPAND },
336 { 0 }
339 static const struct message parent_singleexpand_seq7[] = {
340 /* india does not expand and hotel does not collapse because they have no children */
341 { WM_NOTIFY, sent|id, 0, 0, TVN_SELCHANGINGA },
342 { WM_NOTIFY, sent|id, 0, 0, TVN_SELCHANGEDA },
343 { WM_NOTIFY, sent|id, 0, 0, TVN_SINGLEEXPAND },
344 { 0 }
347 static const struct message parent_get_dispinfo_seq[] = {
348 { WM_NOTIFY, sent|id, 0, 0, TVN_GETDISPINFOA },
349 { 0 }
352 static const struct message empty_seq[] = {
353 { 0 }
356 static const struct message parent_cd_seq[] = {
357 { WM_NOTIFY, sent|id|custdraw, 0, 0, NM_CUSTOMDRAW, CDDS_PREPAINT },
358 { WM_NOTIFY, sent|id|custdraw, 0, 0, NM_CUSTOMDRAW, CDDS_ITEMPREPAINT },
359 { WM_NOTIFY, sent|id|custdraw, 0, 0, NM_CUSTOMDRAW, CDDS_ITEMPOSTPAINT },
360 { WM_NOTIFY, sent|id|custdraw, 0, 0, NM_CUSTOMDRAW, CDDS_ITEMPREPAINT },
361 { WM_NOTIFY, sent|id|custdraw, 0, 0, NM_CUSTOMDRAW, CDDS_ITEMPOSTPAINT },
362 { WM_NOTIFY, sent|id|custdraw, 0, 0, NM_CUSTOMDRAW, CDDS_POSTPAINT },
363 { 0 }
366 static const struct message parent_vk_return_seq[] = {
367 { WM_NOTIFY, sent|id, 0, 0, TVN_KEYDOWN },
368 { WM_NOTIFY, sent|id, 0, 0, NM_RETURN },
369 { WM_CHANGEUISTATE, sent|optional },
370 { 0 }
373 static const struct message parent_right_click_seq[] = {
374 { WM_NOTIFY, sent|id, 0, 0, NM_RCLICK },
375 { WM_CONTEXTMENU, sent },
376 { WM_NOTIFY, sent|optional },
377 { WM_SETCURSOR, sent|optional },
378 { 0 }
381 static HWND hMainWnd;
383 static HTREEITEM hRoot, hChild;
385 static int pos = 0;
386 static char sequence[256];
388 static void Clear(void)
390 pos = 0;
391 sequence[0] = '\0';
394 static void AddItem(char ch)
396 sequence[pos++] = ch;
397 sequence[pos] = '\0';
400 static void IdentifyItem(HTREEITEM hItem)
402 if (hItem == hRoot) {
403 AddItem('R');
404 return;
406 if (hItem == hChild) {
407 AddItem('C');
408 return;
410 if (hItem == NULL) {
411 AddItem('n');
412 return;
414 AddItem('?');
417 /* This function hooks in and records all messages to the treeview control */
418 static LRESULT WINAPI TreeviewWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
420 static LONG defwndproc_counter = 0;
421 LRESULT ret;
422 WNDPROC lpOldProc = (WNDPROC)GetWindowLongPtrA(hwnd, GWLP_USERDATA);
423 struct message msg = { 0 };
425 msg.message = message;
426 msg.flags = sent|wparam|lparam;
427 if (defwndproc_counter) msg.flags |= defwinproc;
428 msg.wParam = wParam;
429 msg.lParam = lParam;
430 add_message(sequences, TREEVIEW_SEQ_INDEX, &msg);
432 defwndproc_counter++;
433 ret = CallWindowProcA(lpOldProc, hwnd, message, wParam, lParam);
434 defwndproc_counter--;
436 return ret;
439 static HWND create_treeview_control(DWORD style)
441 WNDPROC pOldWndProc;
442 HWND hTree;
444 hTree = CreateWindowExA(WS_EX_CLIENTEDGE, WC_TREEVIEWA, NULL, WS_CHILD|WS_VISIBLE|
445 TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS|TVS_EDITLABELS|style,
446 0, 0, 120, 100, hMainWnd, (HMENU)100, GetModuleHandleA(0), 0);
448 SetFocus(hTree);
450 /* Record the old WNDPROC so we can call it after recording the messages */
451 pOldWndProc = (WNDPROC)SetWindowLongPtrA(hTree, GWLP_WNDPROC, (LONG_PTR)TreeviewWndProc);
452 SetWindowLongPtrA(hTree, GWLP_USERDATA, (LONG_PTR)pOldWndProc);
454 return hTree;
457 static void fill_tree(HWND hTree)
459 TVINSERTSTRUCTA ins;
460 static CHAR root[] = "Root",
461 child[] = "Child";
463 ins.hParent = TVI_ROOT;
464 ins.hInsertAfter = TVI_ROOT;
465 U(ins).item.mask = TVIF_TEXT;
466 U(ins).item.pszText = root;
467 hRoot = TreeView_InsertItemA(hTree, &ins);
469 ins.hParent = hRoot;
470 ins.hInsertAfter = TVI_FIRST;
471 U(ins).item.mask = TVIF_TEXT;
472 U(ins).item.pszText = child;
473 hChild = TreeView_InsertItemA(hTree, &ins);
476 static void test_fillroot(void)
478 TVITEMA tvi;
479 HWND hTree;
481 hTree = create_treeview_control(0);
483 flush_sequences(sequences, NUM_MSG_SEQUENCES);
485 fill_tree(hTree);
487 Clear();
488 AddItem('A');
489 ok(hRoot != NULL, "failed to set root\n");
490 AddItem('B');
491 ok(hChild != NULL, "failed to set child\n");
492 AddItem('.');
493 ok_sequence(sequences, TREEVIEW_SEQ_INDEX, FillRootSeq, "FillRoot", FALSE);
494 ok(!strcmp(sequence, "AB."), "Item creation\n");
496 /* UMLPad 1.15 depends on this being not -1 (I_IMAGECALLBACK) */
497 tvi.hItem = hRoot;
498 tvi.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE;
499 SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&tvi);
500 ok(tvi.iImage == 0, "tvi.iImage=%d\n", tvi.iImage);
501 ok(tvi.iSelectedImage == 0, "tvi.iSelectedImage=%d\n", tvi.iSelectedImage);
503 DestroyWindow(hTree);
506 static void test_callback(void)
508 HTREEITEM hRoot;
509 HTREEITEM hItem1, hItem2;
510 TVINSERTSTRUCTA ins;
511 TVITEMA tvi;
512 CHAR test_string[] = "Test_string";
513 static const CHAR test2A[] = "TEST2";
514 CHAR buf[128];
515 HWND hTree;
516 DWORD ret;
518 hTree = create_treeview_control(0);
520 ret = SendMessageA(hTree, TVM_DELETEITEM, 0, (LPARAM)TVI_ROOT);
521 expect(TRUE, ret);
522 ins.hParent = TVI_ROOT;
523 ins.hInsertAfter = TVI_ROOT;
524 U(ins).item.mask = TVIF_TEXT;
525 U(ins).item.pszText = LPSTR_TEXTCALLBACKA;
526 hRoot = TreeView_InsertItemA(hTree, &ins);
527 ok(hRoot != NULL, "failed to set root\n");
529 tvi.hItem = hRoot;
530 tvi.mask = TVIF_TEXT;
531 tvi.pszText = buf;
532 tvi.cchTextMax = ARRAY_SIZE(buf);
533 ret = TreeView_GetItemA(hTree, &tvi);
534 expect(TRUE, ret);
535 ok(strcmp(tvi.pszText, TEST_CALLBACK_TEXT) == 0, "Callback item text mismatch %s vs %s\n",
536 tvi.pszText, TEST_CALLBACK_TEXT);
538 ins.hParent = hRoot;
539 ins.hInsertAfter = TVI_FIRST;
540 U(ins).item.mask = TVIF_TEXT;
541 U(ins).item.pszText = test_string;
542 hItem1 = TreeView_InsertItemA(hTree, &ins);
543 ok(hItem1 != NULL, "failed to set Item1\n");
545 tvi.hItem = hItem1;
546 ret = TreeView_GetItemA(hTree, &tvi);
547 expect(TRUE, ret);
548 ok(strcmp(tvi.pszText, test_string) == 0, "Item text mismatch %s vs %s\n",
549 tvi.pszText, test_string);
551 /* undocumented: pszText of NULL also means LPSTR_CALLBACK: */
552 tvi.pszText = NULL;
553 ret = TreeView_SetItemA(hTree, &tvi);
554 expect(TRUE, ret);
555 tvi.pszText = buf;
556 ret = TreeView_GetItemA(hTree, &tvi);
557 expect(TRUE, ret);
558 ok(strcmp(tvi.pszText, TEST_CALLBACK_TEXT) == 0, "Item text mismatch %s vs %s\n",
559 tvi.pszText, TEST_CALLBACK_TEXT);
561 U(ins).item.pszText = NULL;
562 hItem2 = TreeView_InsertItemA(hTree, &ins);
563 ok(hItem2 != NULL, "failed to set Item2\n");
564 tvi.hItem = hItem2;
565 memset(buf, 0, sizeof(buf));
566 ret = TreeView_GetItemA(hTree, &tvi);
567 expect(TRUE, ret);
568 ok(strcmp(tvi.pszText, TEST_CALLBACK_TEXT) == 0, "Item text mismatch %s vs %s\n",
569 tvi.pszText, TEST_CALLBACK_TEXT);
571 /* notification handler changed A->W */
572 g_disp_A_to_W = TRUE;
573 tvi.hItem = hItem2;
574 memset(buf, 0, sizeof(buf));
575 ret = TreeView_GetItemA(hTree, &tvi);
576 expect(TRUE, ret);
577 ok(strcmp(tvi.pszText, test2A) == 0, "got %s, expected %s\n",
578 tvi.pszText, test2A);
579 g_disp_A_to_W = FALSE;
581 /* handler changes state image index */
582 SetWindowLongA(hTree, GWL_STYLE, GetWindowLongA(hTree, GWL_STYLE) | TVS_CHECKBOXES);
584 /* clear selection, handler will set selected state */
585 ret = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, 0);
586 expect(TRUE, ret);
588 flush_sequences(sequences, NUM_MSG_SEQUENCES);
590 tvi.hItem = hRoot;
591 tvi.mask = TVIF_STATE;
592 tvi.state = TVIS_SELECTED;
593 ret = TreeView_GetItemA(hTree, &tvi);
594 expect(TRUE, ret);
595 ok(tvi.state == INDEXTOSTATEIMAGEMASK(1), "got 0x%x\n", tvi.state);
597 ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq,
598 "no TVN_GETDISPINFO for a state seq", FALSE);
600 tvi.hItem = hRoot;
601 tvi.mask = TVIF_IMAGE | TVIF_STATE;
602 tvi.state = TVIS_FOCUSED;
603 tvi.stateMask = TVIS_FOCUSED;
604 tvi.iImage = I_IMAGECALLBACK;
605 ret = TreeView_SetItemA(hTree, &tvi);
606 expect(TRUE, ret);
608 /* ask for item image index through callback - state is also set with state image index */
609 flush_sequences(sequences, NUM_MSG_SEQUENCES);
611 tvi.hItem = hRoot;
612 tvi.mask = TVIF_IMAGE;
613 tvi.state = 0;
614 ret = TreeView_GetItemA(hTree, &tvi);
615 expect(TRUE, ret);
616 ok(tvi.state == (INDEXTOSTATEIMAGEMASK(1) | TVIS_FOCUSED), "got 0x%x\n", tvi.state);
618 ok_sequence(sequences, PARENT_SEQ_INDEX, parent_get_dispinfo_seq,
619 "callback for state/overlay image index, noop seq", FALSE);
621 /* ask for image again and overwrite state to some value in handler */
622 flush_sequences(sequences, NUM_MSG_SEQUENCES);
624 g_disp_set_stateimage = TRUE;
625 tvi.hItem = hRoot;
626 tvi.mask = TVIF_IMAGE;
627 tvi.state = INDEXTOSTATEIMAGEMASK(1);
628 tvi.stateMask = 0;
629 ret = TreeView_GetItemA(hTree, &tvi);
630 expect(TRUE, ret);
631 /* handler sets TVIS_SELECTED as well */
632 ok(tvi.state == (TVIS_FOCUSED | TVIS_SELECTED | INDEXTOSTATEIMAGEMASK(2) | INDEXTOOVERLAYMASK(3)), "got 0x%x\n", tvi.state);
633 g_disp_set_stateimage = FALSE;
635 ok_sequence(sequences, PARENT_SEQ_INDEX, parent_get_dispinfo_seq,
636 "callback for state/overlay image index seq", FALSE);
638 DestroyWindow(hTree);
641 static void test_select(void)
643 BOOL r;
644 HWND hTree;
646 hTree = create_treeview_control(0);
647 fill_tree(hTree);
649 /* root-none select tests */
650 flush_sequences(sequences, NUM_MSG_SEQUENCES);
651 r = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, 0);
652 expect(TRUE, r);
653 Clear();
654 AddItem('1');
655 r = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hRoot);
656 expect(TRUE, r);
657 AddItem('2');
658 r = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hRoot);
659 expect(TRUE, r);
660 AddItem('3');
661 r = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, 0);
662 expect(TRUE, r);
663 AddItem('4');
664 r = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, 0);
665 expect(TRUE, r);
666 AddItem('5');
667 r = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hRoot);
668 expect(TRUE, r);
669 AddItem('.');
670 ok(!strcmp(sequence, "1(nR)nR23(Rn)Rn45(nR)nR."), "root-none select test\n");
671 ok_sequence(sequences, TREEVIEW_SEQ_INDEX, rootnone_select_seq,
672 "root-none select seq", FALSE);
674 /* root-child select tests */
675 flush_sequences(sequences, NUM_MSG_SEQUENCES);
676 r = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, 0);
677 expect(TRUE, r);
679 Clear();
680 AddItem('1');
681 r = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hRoot);
682 expect(TRUE, r);
683 AddItem('2');
684 r = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hRoot);
685 expect(TRUE, r);
686 AddItem('3');
687 r = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hChild);
688 expect(TRUE, r);
689 AddItem('4');
690 r = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hChild);
691 expect(TRUE, r);
692 AddItem('5');
693 r = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hRoot);
694 expect(TRUE, r);
695 AddItem('.');
696 ok(!strcmp(sequence, "1(nR)nR23(RC)RC45(CR)CR."), "root-child select test\n");
697 ok_sequence(sequences, TREEVIEW_SEQ_INDEX, rootchild_select_seq,
698 "root-child select seq", FALSE);
700 DestroyWindow(hTree);
703 static void test_getitemtext(void)
705 TVINSERTSTRUCTA ins;
706 HTREEITEM hChild;
707 TVITEMA tvi;
708 HWND hTree;
710 CHAR szBuffer[80] = "Blah";
711 int nBufferSize = ARRAY_SIZE(szBuffer);
713 hTree = create_treeview_control(0);
714 fill_tree(hTree);
716 flush_sequences(sequences, NUM_MSG_SEQUENCES);
718 /* add an item without TVIF_TEXT mask and pszText == NULL */
719 ins.hParent = hRoot;
720 ins.hInsertAfter = TVI_ROOT;
721 U(ins).item.mask = 0;
722 U(ins).item.pszText = NULL;
723 U(ins).item.cchTextMax = 0;
724 hChild = TreeView_InsertItemA(hTree, &ins);
725 ok(hChild != NULL, "failed to set hChild\n");
727 /* retrieve it with TVIF_TEXT mask */
728 tvi.hItem = hChild;
729 tvi.mask = TVIF_TEXT;
730 tvi.cchTextMax = nBufferSize;
731 tvi.pszText = szBuffer;
733 SendMessageA( hTree, TVM_GETITEMA, 0, (LPARAM)&tvi );
734 ok(!strcmp(szBuffer, ""), "szBuffer=\"%s\", expected \"\"\n", szBuffer);
735 ok(SendMessageA(hTree, TVM_DELETEITEM, 0, (LPARAM)hChild), "DeleteItem failed\n");
736 ok_sequence(sequences, TREEVIEW_SEQ_INDEX, getitemtext_seq, "get item text seq", FALSE);
738 DestroyWindow(hTree);
741 static void test_focus(void)
743 TVINSERTSTRUCTA ins;
744 static CHAR child1[] = "Edit",
745 child2[] = "A really long string";
746 HTREEITEM hChild1, hChild2;
747 HWND hTree;
748 HWND hEdit;
750 hTree = create_treeview_control(0);
751 fill_tree(hTree);
753 flush_sequences(sequences, NUM_MSG_SEQUENCES);
755 /* This test verifies that when a label is being edited, scrolling
756 * the treeview does not cause the label to lose focus. To test
757 * this, first some additional entries are added to generate
758 * scrollbars.
760 ins.hParent = hRoot;
761 ins.hInsertAfter = hChild;
762 U(ins).item.mask = TVIF_TEXT;
763 U(ins).item.pszText = child1;
764 hChild1 = TreeView_InsertItemA(hTree, &ins);
765 ok(hChild1 != NULL, "failed to set hChild1\n");
766 ins.hInsertAfter = hChild1;
767 U(ins).item.mask = TVIF_TEXT;
768 U(ins).item.pszText = child2;
769 hChild2 = TreeView_InsertItemA(hTree, &ins);
770 ok(hChild2 != NULL, "failed to set hChild2\n");
772 ShowWindow(hMainWnd,SW_SHOW);
773 SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hChild);
774 hEdit = (HWND)SendMessageA(hTree, TVM_EDITLABELA, 0, (LPARAM)hChild);
775 ScrollWindowEx(hTree, -10, 0, NULL, NULL, NULL, NULL, SW_SCROLLCHILDREN);
776 ok(GetFocus() == hEdit, "Edit control should have focus\n");
777 ok_sequence(sequences, TREEVIEW_SEQ_INDEX, focus_seq, "focus test", TRUE);
779 DestroyWindow(hTree);
782 static void test_get_set_bkcolor(void)
784 COLORREF crColor;
785 HWND hTree;
787 hTree = create_treeview_control(0);
788 fill_tree(hTree);
790 flush_sequences(sequences, NUM_MSG_SEQUENCES);
792 /* If the value is -1, the control is using the system color for the background color. */
793 crColor = SendMessageA(hTree, TVM_GETBKCOLOR, 0, 0);
794 ok(crColor == ~0u, "Default background color reported as 0x%.8x\n", crColor);
796 /* Test for black background */
797 SendMessageA(hTree, TVM_SETBKCOLOR, 0, RGB(0,0,0));
798 crColor = SendMessageA(hTree, TVM_GETBKCOLOR, 0, 0);
799 ok(crColor == RGB(0,0,0), "Black background color reported as 0x%.8x\n", crColor);
801 /* Test for white background */
802 SendMessageA(hTree, TVM_SETBKCOLOR, 0, RGB(255,255,255));
803 crColor = SendMessageA(hTree, TVM_GETBKCOLOR, 0, 0);
804 ok(crColor == RGB(255,255,255), "White background color reported as 0x%.8x\n", crColor);
806 /* Reset the default background */
807 SendMessageA(hTree, TVM_SETBKCOLOR, 0, -1);
809 ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_bkcolor_seq,
810 "test get set bkcolor", FALSE);
812 DestroyWindow(hTree);
815 static void test_get_set_imagelist(void)
817 HIMAGELIST himl;
818 HWND hTree;
820 hTree = create_treeview_control(0);
821 fill_tree(hTree);
823 flush_sequences(sequences, NUM_MSG_SEQUENCES);
825 /* Test a NULL HIMAGELIST */
826 SendMessageA(hTree, TVM_SETIMAGELIST, TVSIL_NORMAL, 0);
827 himl = (HIMAGELIST)SendMessageA(hTree, TVM_GETIMAGELIST, TVSIL_NORMAL, 0);
828 ok(himl == NULL, "NULL image list, reported as %p, expected 0.\n", himl);
830 /* TODO: Test an actual image list */
832 ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_imagelist_seq,
833 "test get imagelist", FALSE);
835 DestroyWindow(hTree);
838 static void test_get_set_indent(void)
840 int ulIndent;
841 int ulMinIndent;
842 int ulMoreThanTwiceMin;
843 HWND hTree;
845 hTree = create_treeview_control(0);
846 fill_tree(hTree);
848 flush_sequences(sequences, NUM_MSG_SEQUENCES);
850 /* Finding the minimum indent */
851 SendMessageA(hTree, TVM_SETINDENT, 0, 0);
852 ulMinIndent = SendMessageA(hTree, TVM_GETINDENT, 0, 0);
854 /* Checking an indent that is more than twice the default indent */
855 ulMoreThanTwiceMin = 2*ulMinIndent+1;
856 SendMessageA(hTree, TVM_SETINDENT, ulMoreThanTwiceMin, 0);
857 ulIndent = SendMessageA(hTree, TVM_GETINDENT, 0, 0);
858 ok(ulIndent == ulMoreThanTwiceMin, "Indent reported as %d, expected %d\n", ulIndent, ulMoreThanTwiceMin);
860 ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_indent_seq,
861 "test get set indent", FALSE);
863 DestroyWindow(hTree);
866 static void test_get_set_insertmark(void)
868 COLORREF crColor = RGB(0,0,0);
869 HWND hTree;
871 hTree = create_treeview_control(0);
872 fill_tree(hTree);
874 flush_sequences(sequences, NUM_MSG_SEQUENCES);
876 SendMessageA(hTree, TVM_SETINSERTMARKCOLOR, 0, crColor);
877 crColor = SendMessageA(hTree, TVM_GETINSERTMARKCOLOR, 0, 0);
878 ok(crColor == RGB(0,0,0), "Insert mark color reported as 0x%.8x, expected 0x00000000\n", crColor);
880 ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_insertmarkcolor_seq,
881 "test get set insertmark color", FALSE);
883 DestroyWindow(hTree);
886 static void test_get_set_item(void)
888 TVITEMA tviRoot = {0};
889 int nBufferSize = 80;
890 char szBuffer[80] = {0};
891 HWND hTree, hTree2;
892 DWORD ret;
894 hTree = create_treeview_control(0);
895 fill_tree(hTree);
897 tviRoot.hItem = hRoot;
898 tviRoot.mask = TVIF_STATE;
899 tviRoot.state = TVIS_FOCUSED;
900 tviRoot.stateMask = TVIS_FOCUSED;
901 ret = SendMessageA(hTree, TVM_SETITEMA, 0, (LPARAM)&tviRoot);
902 expect(TRUE, ret);
904 flush_sequences(sequences, NUM_MSG_SEQUENCES);
906 /* Test the root item, state is set even when not requested */
907 tviRoot.hItem = hRoot;
908 tviRoot.mask = TVIF_TEXT;
909 tviRoot.state = 0;
910 tviRoot.stateMask = 0;
911 tviRoot.cchTextMax = nBufferSize;
912 tviRoot.pszText = szBuffer;
913 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&tviRoot);
914 expect(TRUE, ret);
915 ok(!strcmp("Root", szBuffer), "GetItem: szBuffer=\"%s\", expected \"Root\"\n", szBuffer);
916 ok(tviRoot.state == TVIS_FOCUSED, "got 0x%0x\n", tviRoot.state);
918 /* Change the root text */
919 lstrcpynA(szBuffer, "Testing123", nBufferSize);
920 ret = SendMessageA(hTree, TVM_SETITEMA, 0, (LPARAM)&tviRoot);
921 expect(TRUE, ret);
922 memset(szBuffer, 0, nBufferSize);
923 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&tviRoot);
924 expect(TRUE, ret);
925 ok(!strcmp("Testing123", szBuffer), "GetItem: szBuffer=\"%s\", expected \"Testing123\"\n", szBuffer);
927 /* Reset the root text */
928 memset(szBuffer, 0, nBufferSize);
929 lstrcpynA(szBuffer, "Root", nBufferSize);
930 ret = SendMessageA(hTree, TVM_SETITEMA, 0, (LPARAM)&tviRoot);
931 expect(TRUE, ret);
933 ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_item_seq,
934 "test get set item", FALSE);
936 /* get item from a different tree */
937 hTree2 = create_treeview_control(0);
939 tviRoot.hItem = hRoot;
940 tviRoot.mask = TVIF_STATE;
941 tviRoot.state = 0;
942 ret = SendMessageA(hTree2, TVM_GETITEMA, 0, (LPARAM)&tviRoot);
943 expect(TRUE, ret);
944 ok(tviRoot.state == TVIS_FOCUSED, "got state 0x%0x\n", tviRoot.state);
946 /* invalid item pointer, nt4 crashes here but later versions just return 0 */
947 tviRoot.hItem = (HTREEITEM)0xdeadbeef;
948 tviRoot.mask = TVIF_STATE;
949 tviRoot.state = 0;
950 ret = SendMessageA(hTree2, TVM_GETITEMA, 0, (LPARAM)&tviRoot);
951 expect(FALSE, ret);
953 DestroyWindow(hTree);
954 DestroyWindow(hTree2);
957 static void test_get_set_itemheight(void)
959 int ulOldHeight = 0;
960 int ulNewHeight = 0;
961 HWND hTree;
963 hTree = create_treeview_control(0);
964 fill_tree(hTree);
966 flush_sequences(sequences, NUM_MSG_SEQUENCES);
968 /* Assuming default height to begin with */
969 ulOldHeight = SendMessageA(hTree, TVM_GETITEMHEIGHT, 0, 0);
971 /* Explicitly setting and getting the default height */
972 SendMessageA(hTree, TVM_SETITEMHEIGHT, -1, 0);
973 ulNewHeight = SendMessageA(hTree, TVM_GETITEMHEIGHT, 0, 0);
974 ok(ulNewHeight == ulOldHeight, "Default height not set properly, reported %d, expected %d\n", ulNewHeight, ulOldHeight);
976 /* Explicitly setting and getting the height of twice the normal */
977 SendMessageA(hTree, TVM_SETITEMHEIGHT, 2*ulOldHeight, 0);
978 ulNewHeight = SendMessageA(hTree, TVM_GETITEMHEIGHT, 0, 0);
979 ok(ulNewHeight == 2*ulOldHeight, "New height not set properly, reported %d, expected %d\n", ulNewHeight, 2*ulOldHeight);
981 /* Assuming tree doesn't have TVS_NONEVENHEIGHT set, so a set of 9 will round down to 8 */
982 SendMessageA(hTree, TVM_SETITEMHEIGHT, 9, 0);
983 ulNewHeight = SendMessageA(hTree, TVM_GETITEMHEIGHT, 0, 0);
984 ok(ulNewHeight == 8, "Uneven height not set properly, reported %d, expected %d\n", ulNewHeight, 8);
986 ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_itemheight_seq,
987 "test get set item height", FALSE);
989 /* without TVS_NONEVENHEIGHT */
990 SetWindowLongA(hTree, GWL_STYLE, GetWindowLongA(hTree, GWL_STYLE) & ~TVS_NONEVENHEIGHT);
991 /* odd value */
992 ulOldHeight = SendMessageA(hTree, TVM_SETITEMHEIGHT, 3, 0);
993 ok(ulOldHeight == 8, "got %d, expected %d\n", ulOldHeight, 8);
994 ulNewHeight = SendMessageA(hTree, TVM_GETITEMHEIGHT, 0, 0);
995 ok(ulNewHeight == 2, "got %d, expected %d\n", ulNewHeight, 2);
997 ulOldHeight = SendMessageA(hTree, TVM_SETITEMHEIGHT, 4, 0);
998 ok(ulOldHeight == 2, "got %d, expected %d\n", ulOldHeight, 2);
999 ulNewHeight = SendMessageA(hTree, TVM_GETITEMHEIGHT, 0, 0);
1000 ok(ulNewHeight == 4, "got %d, expected %d\n", ulNewHeight, 4);
1002 /* with TVS_NONEVENHEIGHT */
1003 SetWindowLongA(hTree, GWL_STYLE, GetWindowLongA(hTree, GWL_STYLE) | TVS_NONEVENHEIGHT);
1004 /* odd value */
1005 ulOldHeight = SendMessageA(hTree, TVM_SETITEMHEIGHT, 3, 0);
1006 ok(ulOldHeight == 4, "got %d, expected %d\n", ulOldHeight, 4);
1007 ulNewHeight = SendMessageA(hTree, TVM_GETITEMHEIGHT, 0, 0);
1008 ok(ulNewHeight == 3, "got %d, expected %d\n", ulNewHeight, 3);
1009 /* even value */
1010 ulOldHeight = SendMessageA(hTree, TVM_SETITEMHEIGHT, 10, 0);
1011 ok(ulOldHeight == 3, "got %d, expected %d\n", ulOldHeight, 3);
1012 ulNewHeight = SendMessageA(hTree, TVM_GETITEMHEIGHT, 0, 0);
1013 ok(ulNewHeight == 10, "got %d, expected %d\n", ulNewHeight, 10);
1015 DestroyWindow(hTree);
1018 static void test_get_set_scrolltime(void)
1020 int ulExpectedTime = 20;
1021 int ulTime = 0;
1022 HWND hTree;
1024 hTree = create_treeview_control(0);
1025 fill_tree(hTree);
1027 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1029 SendMessageA(hTree, TVM_SETSCROLLTIME, ulExpectedTime, 0);
1030 ulTime = SendMessageA(hTree, TVM_GETSCROLLTIME, 0, 0);
1031 ok(ulTime == ulExpectedTime, "Scroll time reported as %d, expected %d\n", ulTime, ulExpectedTime);
1033 ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_scrolltime_seq,
1034 "test get set scroll time", FALSE);
1036 DestroyWindow(hTree);
1039 static void test_get_set_textcolor(void)
1041 /* If the value is -1, the control is using the system color for the text color. */
1042 COLORREF crColor;
1043 HWND hTree;
1045 hTree = create_treeview_control(0);
1046 fill_tree(hTree);
1048 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1050 crColor = SendMessageA(hTree, TVM_GETTEXTCOLOR, 0, 0);
1051 ok(crColor == ~0u, "Default text color reported as 0x%.8x\n", crColor);
1053 /* Test for black text */
1054 SendMessageA(hTree, TVM_SETTEXTCOLOR, 0, RGB(0,0,0));
1055 crColor = SendMessageA(hTree, TVM_GETTEXTCOLOR, 0, 0);
1056 ok(crColor == RGB(0,0,0), "Black text color reported as 0x%.8x\n", crColor);
1058 /* Test for white text */
1059 SendMessageA(hTree, TVM_SETTEXTCOLOR, 0, RGB(255,255,255));
1060 crColor = SendMessageA(hTree, TVM_GETTEXTCOLOR, 0, 0);
1061 ok(crColor == RGB(255,255,255), "White text color reported as 0x%.8x\n", crColor);
1063 /* Reset the default text color */
1064 SendMessageA(hTree, TVM_SETTEXTCOLOR, 0, CLR_NONE);
1066 ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_textcolor_seq,
1067 "test get set text color", FALSE);
1069 DestroyWindow(hTree);
1072 static void test_get_set_tooltips(void)
1074 HWND hTree, tooltips, hwnd;
1075 DWORD style;
1076 int i;
1078 /* TVS_NOTOOLTIPS */
1079 hTree = create_treeview_control(TVS_NOTOOLTIPS);
1081 tooltips = (HWND)SendMessageA(hTree, TVM_GETTOOLTIPS, 0, 0);
1082 ok(tooltips == NULL, "Unexpected tooltip window %p.\n", tooltips);
1084 tooltips = (HWND)SendMessageA(hTree, TVM_SETTOOLTIPS, 0, 0);
1085 ok(tooltips == NULL, "Unexpected ret value %p.\n", tooltips);
1087 /* Toggle style */
1088 style = GetWindowLongA(hTree, GWL_STYLE);
1089 SetWindowLongA(hTree, GWL_STYLE, style & ~TVS_NOTOOLTIPS);
1091 tooltips = (HWND)SendMessageA(hTree, TVM_GETTOOLTIPS, 0, 0);
1092 ok(IsWindow(tooltips), "Unexpected tooltip window %p.\n", tooltips);
1094 style = GetWindowLongA(hTree, GWL_STYLE);
1095 SetWindowLongA(hTree, GWL_STYLE, style | TVS_NOTOOLTIPS);
1097 tooltips = (HWND)SendMessageA(hTree, TVM_GETTOOLTIPS, 0, 0);
1098 ok(tooltips == NULL, "Unexpected tooltip window %p.\n", tooltips);
1100 DestroyWindow(hTree);
1102 /* Set some valid window, does not have to be tooltips class. */
1103 hTree = create_treeview_control(TVS_NOTOOLTIPS);
1105 hwnd = CreateWindowA(WC_STATICA, "Test", WS_VISIBLE|WS_CHILD, 5, 5, 100, 100, hMainWnd, NULL, NULL, 0);
1106 ok(hwnd != NULL, "Failed to create child window.\n");
1108 tooltips = (HWND)SendMessageA(hTree, TVM_SETTOOLTIPS, (WPARAM)hwnd, 0);
1109 ok(tooltips == NULL, "Unexpected ret value %p.\n", tooltips);
1111 tooltips = (HWND)SendMessageA(hTree, TVM_GETTOOLTIPS, 0, 0);
1112 ok(tooltips == hwnd, "Unexpected tooltip window %p.\n", tooltips);
1114 /* Externally set tooltips window, disable style. */
1115 style = GetWindowLongA(hTree, GWL_STYLE);
1116 SetWindowLongA(hTree, GWL_STYLE, style & ~TVS_NOTOOLTIPS);
1118 tooltips = (HWND)SendMessageA(hTree, TVM_GETTOOLTIPS, 0, 0);
1119 ok(IsWindow(tooltips) && tooltips != hwnd, "Unexpected tooltip window %p.\n", tooltips);
1120 ok(IsWindow(hwnd), "Expected valid window.\n");
1122 style = GetWindowLongA(hTree, GWL_STYLE);
1123 SetWindowLongA(hTree, GWL_STYLE, style | TVS_NOTOOLTIPS);
1124 ok(!IsWindow(tooltips), "Unexpected tooltip window %p.\n", tooltips);
1126 tooltips = (HWND)SendMessageA(hTree, TVM_GETTOOLTIPS, 0, 0);
1127 ok(tooltips == NULL, "Unexpected tooltip window %p.\n", tooltips);
1128 ok(IsWindow(hwnd), "Expected valid window.\n");
1130 DestroyWindow(hTree);
1131 ok(IsWindow(hwnd), "Expected valid window.\n");
1133 /* Set window, disable tooltips. */
1134 hTree = create_treeview_control(0);
1136 tooltips = (HWND)SendMessageA(hTree, TVM_SETTOOLTIPS, (WPARAM)hwnd, 0);
1137 ok(IsWindow(tooltips), "Unexpected ret value %p.\n", tooltips);
1139 style = GetWindowLongA(hTree, GWL_STYLE);
1140 SetWindowLongA(hTree, GWL_STYLE, style | TVS_NOTOOLTIPS);
1141 ok(!IsWindow(hwnd), "Unexpected tooltip window %p.\n", tooltips);
1142 ok(IsWindow(tooltips), "Expected valid window %p.\n", tooltips);
1144 DestroyWindow(hTree);
1145 ok(IsWindow(tooltips), "Expected valid window %p.\n", tooltips);
1146 DestroyWindow(tooltips);
1147 DestroyWindow(hwnd);
1149 for (i = 0; i < 2; i++)
1151 DWORD style = i == 0 ? 0 : TVS_NOTOOLTIPS;
1153 hwnd = CreateWindowA(WC_STATICA, "Test", WS_VISIBLE|WS_CHILD, 5, 5, 100, 100, hMainWnd, NULL, NULL, 0);
1154 ok(hwnd != NULL, "Failed to create child window.\n");
1156 hTree = create_treeview_control(style);
1158 tooltips = (HWND)SendMessageA(hTree, TVM_SETTOOLTIPS, (WPARAM)hwnd, 0);
1159 ok(style & TVS_NOTOOLTIPS ? tooltips == NULL : IsWindow(tooltips), "Unexpected ret value %p.\n", tooltips);
1160 DestroyWindow(tooltips);
1162 tooltips = (HWND)SendMessageA(hTree, TVM_GETTOOLTIPS, 0, 0);
1163 ok(tooltips == hwnd, "Unexpected tooltip window %p.\n", tooltips);
1165 /* TreeView is destroyed, check if set window is still around. */
1166 DestroyWindow(hTree);
1167 ok(!IsWindow(hwnd), "Unexpected window.\n");
1170 hTree = create_treeview_control(0);
1171 fill_tree(hTree);
1173 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1175 /* show even WS_POPUP treeview don't send NM_TOOLTIPSCREATED */
1176 hwnd = CreateWindowA(WC_TREEVIEWA, NULL, WS_POPUP|WS_VISIBLE, 0, 0, 100, 100,
1177 hMainWnd, NULL, NULL, NULL);
1178 DestroyWindow(hwnd);
1180 /* Testing setting a NULL ToolTip */
1181 tooltips = (HWND)SendMessageA(hTree, TVM_SETTOOLTIPS, 0, 0);
1182 ok(IsWindow(tooltips), "Unexpected ret value %p.\n", tooltips);
1184 hwnd = (HWND)SendMessageA(hTree, TVM_GETTOOLTIPS, 0, 0);
1185 ok(hwnd == NULL, "Unexpected tooltip window %p.\n", hwnd);
1187 ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_tooltips_seq,
1188 "test get set tooltips", TRUE);
1190 DestroyWindow(hTree);
1191 ok(IsWindow(tooltips), "Expected valid window.\n");
1192 DestroyWindow(tooltips);
1195 static void test_get_set_unicodeformat(void)
1197 BOOL bPreviousSetting;
1198 BOOL bNewSetting;
1199 HWND hTree;
1201 hTree = create_treeview_control(0);
1202 fill_tree(hTree);
1204 /* Check that an invalid format returned by NF_QUERY defaults to ANSI */
1205 bPreviousSetting = SendMessageA(hTree, TVM_GETUNICODEFORMAT, 0, 0);
1206 ok(bPreviousSetting == FALSE, "Format should be ANSI.\n");
1208 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1210 /* Set to Unicode */
1211 bPreviousSetting = SendMessageA(hTree, TVM_SETUNICODEFORMAT, 1, 0);
1212 bNewSetting = SendMessageA(hTree, TVM_GETUNICODEFORMAT, 0, 0);
1213 ok(bNewSetting == TRUE, "Unicode setting did not work.\n");
1215 /* Set to ANSI */
1216 SendMessageA(hTree, TVM_SETUNICODEFORMAT, 0, 0);
1217 bNewSetting = SendMessageA(hTree, TVM_GETUNICODEFORMAT, 0, 0);
1218 ok(bNewSetting == FALSE, "ANSI setting did not work.\n");
1220 /* Revert to original setting */
1221 SendMessageA(hTree, TVM_SETUNICODEFORMAT, bPreviousSetting, 0);
1223 ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_get_set_unicodeformat_seq,
1224 "test get set unicode format", FALSE);
1226 DestroyWindow(hTree);
1229 static LRESULT CALLBACK parent_wnd_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1231 static LONG defwndproc_counter = 0;
1232 struct message msg = { 0 };
1233 LRESULT ret;
1234 RECT rect;
1235 HTREEITEM visibleItem;
1237 msg.message = message;
1238 msg.flags = sent|wparam|lparam;
1239 if (defwndproc_counter) msg.flags |= defwinproc;
1240 msg.wParam = wParam;
1241 msg.lParam = lParam;
1242 if (message == WM_NOTIFY && lParam)
1243 msg.id = ((NMHDR*)lParam)->code;
1245 /* log system messages, except for painting */
1246 if (message < WM_USER &&
1247 message != WM_PAINT &&
1248 message != WM_ERASEBKGND &&
1249 message != WM_NCPAINT &&
1250 message != WM_NCHITTEST &&
1251 message != WM_GETTEXT &&
1252 message != WM_GETICON &&
1253 message != WM_DEVICECHANGE)
1255 add_message(sequences, PARENT_SEQ_INDEX, &msg);
1258 switch(message) {
1259 case WM_NOTIFYFORMAT:
1261 /* Make NF_QUERY return an invalid format to show that it defaults to ANSI */
1262 if (lParam == NF_QUERY) return 0;
1263 break;
1266 case WM_NOTIFY:
1268 NMHDR *pHdr = (NMHDR *)lParam;
1270 ok(pHdr->code != NM_TOOLTIPSCREATED, "Treeview should not send NM_TOOLTIPSCREATED\n");
1271 if (pHdr->idFrom == 100)
1273 NMTREEVIEWA *pTreeView = (LPNMTREEVIEWA) lParam;
1274 switch(pHdr->code)
1276 case TVN_SELCHANGINGA:
1277 AddItem('(');
1278 IdentifyItem(pTreeView->itemOld.hItem);
1279 IdentifyItem(pTreeView->itemNew.hItem);
1280 break;
1281 case TVN_SELCHANGEDA:
1282 AddItem(')');
1283 IdentifyItem(pTreeView->itemOld.hItem);
1284 IdentifyItem(pTreeView->itemNew.hItem);
1285 break;
1286 case TVN_GETDISPINFOA: {
1287 NMTVDISPINFOA *disp = (NMTVDISPINFOA *)lParam;
1288 NMTVDISPINFOW *dispW = (NMTVDISPINFOW *)lParam;
1290 if (disp->item.mask & TVIF_TEXT) {
1291 lstrcpynA(disp->item.pszText, TEST_CALLBACK_TEXT, disp->item.cchTextMax);
1294 if (g_disp_A_to_W && (dispW->item.mask & TVIF_TEXT))
1296 dispW->hdr.code = TVN_GETDISPINFOW;
1297 lstrcpyW(dispW->item.pszText, L"TEST2");
1300 if (g_disp_set_stateimage)
1302 ok(disp->item.mask == TVIF_IMAGE, "got %x\n", disp->item.mask);
1303 /* both masks set here are necessary to change state bits */
1304 disp->item.mask |= TVIF_STATE;
1305 disp->item.state = TVIS_SELECTED | INDEXTOSTATEIMAGEMASK(2) | INDEXTOOVERLAYMASK(3);
1306 disp->item.stateMask = TVIS_SELECTED | TVIS_OVERLAYMASK | TVIS_STATEIMAGEMASK;
1309 break;
1311 case TVN_BEGINLABELEDITA:
1313 if (g_beginedit_alter_text)
1315 static const char* textA = "<edittextaltered>";
1316 HWND edit;
1318 edit = (HWND)SendMessageA(pHdr->hwndFrom, TVM_GETEDITCONTROL, 0, 0);
1319 ok(IsWindow(edit), "failed to get edit handle\n");
1320 SetWindowTextA(edit, textA);
1323 break;
1326 case TVN_ENDLABELEDITA:
1328 NMTVDISPINFOA *disp = (NMTVDISPINFOA *)lParam;
1329 if (disp->item.mask & TVIF_TEXT)
1331 ok(disp->item.cchTextMax == MAX_PATH, "cchTextMax is %d\n", disp->item.cchTextMax);
1332 if (g_endedit_overwrite_contents)
1333 strcpy(disp->item.pszText, g_endedit_overwrite_contents);
1334 if (g_endedit_overwrite_ptr)
1335 disp->item.pszText = g_endedit_overwrite_ptr;
1337 return TRUE;
1339 case TVN_ITEMEXPANDINGA:
1341 UINT newmask = pTreeView->itemNew.mask & ~TVIF_CHILDREN;
1342 ok(newmask ==
1343 (TVIF_HANDLE | TVIF_SELECTEDIMAGE | TVIF_IMAGE | TVIF_PARAM | TVIF_STATE),
1344 "got wrong mask %x\n", pTreeView->itemNew.mask);
1345 ok(pTreeView->itemOld.mask == 0,
1346 "got wrong mask %x\n", pTreeView->itemOld.mask);
1348 if (g_get_from_expand)
1350 g_item_expanding.mask = TVIF_STATE;
1351 g_item_expanding.hItem = hRoot;
1352 ret = SendMessageA(pHdr->hwndFrom, TVM_GETITEMA, 0, (LPARAM)&g_item_expanding);
1353 ok(ret == TRUE, "got %lu\n", ret);
1355 break;
1357 case TVN_ITEMEXPANDEDA:
1358 ok(pTreeView->itemNew.mask & TVIF_STATE, "got wrong mask %x\n", pTreeView->itemNew.mask);
1359 ok(pTreeView->itemNew.state & (TVIS_EXPANDED|TVIS_EXPANDEDONCE),
1360 "got wrong mask %x\n", pTreeView->itemNew.mask);
1361 ok(pTreeView->itemOld.mask == 0,
1362 "got wrong mask %x\n", pTreeView->itemOld.mask);
1364 if (g_get_from_expand)
1366 g_item_expanded.mask = TVIF_STATE;
1367 g_item_expanded.hItem = hRoot;
1368 ret = SendMessageA(pHdr->hwndFrom, TVM_GETITEMA, 0, (LPARAM)&g_item_expanded);
1369 ok(ret == TRUE, "got %lu\n", ret);
1371 if (g_get_rect_in_expand)
1373 visibleItem = (HTREEITEM)SendMessageA(pHdr->hwndFrom, TVM_GETNEXTITEM,
1374 TVGN_FIRSTVISIBLE, 0);
1375 ok(pTreeView->itemNew.hItem == visibleItem, "expanded item == first visible item\n");
1376 *(HTREEITEM*)&rect = visibleItem;
1377 ok(SendMessageA(pHdr->hwndFrom, TVM_GETITEMRECT, TRUE, (LPARAM)&rect),
1378 "Failed to get rect for first visible item.\n");
1379 visibleItem = (HTREEITEM)SendMessageA(pHdr->hwndFrom, TVM_GETNEXTITEM,
1380 TVGN_NEXTVISIBLE, (LPARAM)visibleItem);
1381 *(HTREEITEM*)&rect = visibleItem;
1382 ok(visibleItem != NULL, "There must be a visible item after the first one.\n");
1383 ok(SendMessageA(pHdr->hwndFrom, TVM_GETITEMRECT, TRUE, (LPARAM)&rect),
1384 "Failed to get rect for second visible item.\n");
1386 break;
1387 case TVN_DELETEITEMA:
1389 struct message item;
1391 ok(pTreeView->itemNew.mask == 0, "got wrong mask 0x%x\n", pTreeView->itemNew.mask);
1393 ok(pTreeView->itemOld.mask == (TVIF_HANDLE | TVIF_PARAM), "got wrong mask 0x%x\n", pTreeView->itemOld.mask);
1394 ok(pTreeView->itemOld.hItem != NULL, "got %p\n", pTreeView->itemOld.hItem);
1396 memset(&item, 0, sizeof(item));
1397 item.lParam = (LPARAM)pTreeView->itemOld.hItem;
1398 add_message(item_sequence, 0, &item);
1400 break;
1402 case NM_CUSTOMDRAW:
1404 NMTVCUSTOMDRAW *nmcd = (NMTVCUSTOMDRAW*)lParam;
1405 COLORREF c0ffee = RGB(0xc0,0xff,0xee), cafe = RGB(0xca,0xfe,0x00);
1406 COLORREF text = GetTextColor(nmcd->nmcd.hdc), bkgnd = GetBkColor(nmcd->nmcd.hdc);
1408 msg.flags |= custdraw;
1409 msg.stage = nmcd->nmcd.dwDrawStage;
1410 add_message(sequences, PARENT_CD_SEQ_INDEX, &msg);
1412 switch (msg.stage)
1414 case CDDS_PREPAINT:
1415 return CDRF_NOTIFYITEMDRAW|CDRF_NOTIFYITEMERASE|CDRF_NOTIFYPOSTPAINT;
1416 case CDDS_ITEMPREPAINT:
1417 ok(text == nmcd->clrText || (g_v6 && nmcd->clrText == 0xffffffff),
1418 "got %08x vs %08x\n", text, nmcd->clrText);
1419 ok(bkgnd == nmcd->clrTextBk || (g_v6 && nmcd->clrTextBk == 0xffffffff),
1420 "got %08x vs %08x\n", bkgnd, nmcd->clrTextBk);
1421 nmcd->clrText = cafe;
1422 nmcd->clrTextBk = c0ffee;
1423 SetTextColor(nmcd->nmcd.hdc, c0ffee);
1424 SetBkColor(nmcd->nmcd.hdc, cafe);
1425 if (g_customdraw_font)
1426 SelectObject(nmcd->nmcd.hdc, g_customdraw_font);
1427 return CDRF_NOTIFYPOSTPAINT|CDRF_NEWFONT;
1428 case CDDS_ITEMPOSTPAINT:
1429 /* at the point of post paint notification colors are already restored */
1430 ok(nmcd->clrText == cafe, "got 0%x\n", nmcd->clrText);
1431 ok(nmcd->clrTextBk == c0ffee, "got 0%x\n", nmcd->clrTextBk);
1432 ok(text != cafe, "got 0%x\n", text);
1433 ok(bkgnd != c0ffee, "got 0%x\n", bkgnd);
1434 if (g_customdraw_font)
1435 ok(GetCurrentObject(nmcd->nmcd.hdc, OBJ_FONT) != g_customdraw_font, "got %p\n",
1436 GetCurrentObject(nmcd->nmcd.hdc, OBJ_FONT));
1437 break;
1438 default:
1441 break;
1443 case NM_RCLICK:
1445 HTREEITEM selected = (HTREEITEM)SendMessageA(((NMHDR *)lParam)->hwndFrom,
1446 TVM_GETNEXTITEM, TVGN_CARET, 0);
1447 ok(selected == hChild, "child item should still be selected\n");
1448 break;
1452 break;
1457 defwndproc_counter++;
1458 ret = DefWindowProcA(hWnd, message, wParam, lParam);
1459 defwndproc_counter--;
1461 return ret;
1464 static void test_expandinvisible(void)
1466 static CHAR nodeText[][5] = {"0", "1", "2", "3", "4"};
1467 TVINSERTSTRUCTA ins;
1468 HTREEITEM node[5];
1469 RECT dummyRect;
1470 BOOL nodeVisible;
1471 LRESULT ret;
1472 HWND hTree;
1474 hTree = create_treeview_control(0);
1476 /* The test builds the following tree and expands node 1, while node 0 is collapsed.
1479 * |- 1
1480 * | |- 2
1481 * | |- 3
1482 * |- 4
1486 ret = SendMessageA(hTree, TVM_DELETEITEM, 0, (LPARAM)TVI_ROOT);
1487 ok(ret == TRUE, "ret\n");
1488 ins.hParent = TVI_ROOT;
1489 ins.hInsertAfter = TVI_ROOT;
1490 U(ins).item.mask = TVIF_TEXT;
1491 U(ins).item.pszText = nodeText[0];
1492 node[0] = TreeView_InsertItemA(hTree, &ins);
1493 ok(node[0] != NULL, "failed to set node[0]\n");
1495 ins.hInsertAfter = TVI_LAST;
1496 U(ins).item.mask = TVIF_TEXT;
1497 ins.hParent = node[0];
1499 U(ins).item.pszText = nodeText[1];
1500 node[1] = TreeView_InsertItemA(hTree, &ins);
1501 ok(node[1] != NULL, "failed to set node[1]\n");
1502 U(ins).item.pszText = nodeText[4];
1503 node[4] = TreeView_InsertItemA(hTree, &ins);
1504 ok(node[4] != NULL, "failed to set node[4]\n");
1506 ins.hParent = node[1];
1508 U(ins).item.pszText = nodeText[2];
1509 node[2] = TreeView_InsertItemA(hTree, &ins);
1510 ok(node[2] != NULL, "failed to set node[2]\n");
1511 U(ins).item.pszText = nodeText[3];
1512 node[3] = TreeView_InsertItemA(hTree, &ins);
1513 ok(node[3] != NULL, "failed to set node[3]\n");
1515 *(HTREEITEM *)&dummyRect = node[1];
1516 nodeVisible = SendMessageA(hTree, TVM_GETITEMRECT, FALSE, (LPARAM)&dummyRect);
1517 ok(!nodeVisible, "Node 1 should not be visible.\n");
1518 *(HTREEITEM *)&dummyRect = node[2];
1519 nodeVisible = SendMessageA(hTree, TVM_GETITEMRECT, FALSE, (LPARAM)&dummyRect);
1520 ok(!nodeVisible, "Node 2 should not be visible.\n");
1521 *(HTREEITEM *)&dummyRect = node[3];
1522 nodeVisible = SendMessageA(hTree, TVM_GETITEMRECT, FALSE, (LPARAM)&dummyRect);
1523 ok(!nodeVisible, "Node 3 should not be visible.\n");
1524 *(HTREEITEM *)&dummyRect = node[4];
1525 nodeVisible = SendMessageA(hTree, TVM_GETITEMRECT, FALSE, (LPARAM)&dummyRect);
1526 ok(!nodeVisible, "Node 4 should not be visible.\n");
1528 ok(SendMessageA(hTree, TVM_EXPAND, TVE_EXPAND, (LPARAM)node[1]), "Expand of node 1 failed.\n");
1530 *(HTREEITEM *)&dummyRect = node[1];
1531 nodeVisible = SendMessageA(hTree, TVM_GETITEMRECT, FALSE, (LPARAM)&dummyRect);
1532 ok(!nodeVisible, "Node 1 should not be visible.\n");
1533 *(HTREEITEM *)&dummyRect = node[2];
1534 nodeVisible = SendMessageA(hTree, TVM_GETITEMRECT, FALSE, (LPARAM)&dummyRect);
1535 ok(!nodeVisible, "Node 2 should not be visible.\n");
1536 *(HTREEITEM *)&dummyRect = node[3];
1537 nodeVisible = SendMessageA(hTree, TVM_GETITEMRECT, FALSE, (LPARAM)&dummyRect);
1538 ok(!nodeVisible, "Node 3 should not be visible.\n");
1539 *(HTREEITEM *)&dummyRect = node[4];
1540 nodeVisible = SendMessageA(hTree, TVM_GETITEMRECT, FALSE, (LPARAM)&dummyRect);
1541 ok(!nodeVisible, "Node 4 should not be visible.\n");
1543 DestroyWindow(hTree);
1546 static void test_expand(void)
1548 HTREEITEM first, second, last, child;
1549 TVINSERTSTRUCTA ins;
1550 BOOL visible;
1551 RECT rect;
1552 HWND tv;
1553 int i;
1555 tv = create_treeview_control(0);
1557 ins.hParent = TVI_ROOT;
1558 ins.hInsertAfter = TVI_LAST;
1559 U(ins).item.mask = 0;
1560 first = TreeView_InsertItemA(tv, &ins);
1561 ok(first != NULL, "failed to insert first node\n");
1562 second = TreeView_InsertItemA(tv, &ins);
1563 ok(second != NULL, "failed to insert second node\n");
1564 for (i=0; i<100; i++)
1566 last = TreeView_InsertItemA(tv, &ins);
1567 ok(last != NULL, "failed to insert %d node\n", i);
1570 ins.hParent = second;
1571 child = TreeView_InsertItemA(tv, &ins);
1572 ok(child != NULL, "failed to insert child node\n");
1574 ok(SendMessageA(tv, TVM_SELECTITEM, TVGN_CARET, (LPARAM)last), "last node selection failed\n");
1575 ok(SendMessageA(tv, TVM_EXPAND, TVE_EXPAND, (LPARAM)second), "expand of second node failed\n");
1576 ok(SendMessageA(tv, TVM_SELECTITEM, TVGN_CARET, (LPARAM)first), "first node selection failed\n");
1578 *(HTREEITEM *)&rect = first;
1579 visible = SendMessageA(tv, TVM_GETITEMRECT, FALSE, (LPARAM)&rect);
1580 ok(visible, "first node should be visible\n");
1581 ok(!rect.left, "rect.left = %d\n", rect.left);
1582 ok(!rect.top, "rect.top = %d\n", rect.top);
1583 ok(rect.right, "rect.right = 0\n");
1584 ok(rect.bottom, "rect.bottom = 0\n");
1586 DestroyWindow(tv);
1589 static void test_itemedit(void)
1591 DWORD r;
1592 HWND edit;
1593 TVITEMA item;
1594 CHAR buffA[500];
1595 HWND hTree;
1597 hTree = create_treeview_control(0);
1598 fill_tree(hTree);
1600 /* try with null item */
1601 edit = (HWND)SendMessageA(hTree, TVM_EDITLABELA, 0, 0);
1602 ok(!IsWindow(edit), "Expected valid handle\n");
1604 /* trigger edit */
1605 edit = (HWND)SendMessageA(hTree, TVM_EDITLABELA, 0, (LPARAM)hRoot);
1606 ok(IsWindow(edit), "Expected valid handle\n");
1607 /* item shouldn't be selected automatically after TVM_EDITLABELA */
1608 r = SendMessageA(hTree, TVM_GETITEMSTATE, (WPARAM)hRoot, TVIS_SELECTED);
1609 expect(0, r);
1610 /* try to cancel with wrong edit handle */
1611 r = SendMessageA(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), 0);
1612 expect(0, r);
1613 ok(IsWindow(edit), "Expected edit control to be valid\n");
1614 r = SendMessageA(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)edit);
1615 expect(0, r);
1616 ok(!IsWindow(edit), "Expected edit control to be destroyed\n");
1617 /* try to cancel without creating edit */
1618 r = SendMessageA(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), 0);
1619 expect(0, r);
1621 /* try to cancel with wrong (not null) handle */
1622 edit = (HWND)SendMessageA(hTree, TVM_EDITLABELA, 0, (LPARAM)hRoot);
1623 ok(IsWindow(edit), "Expected valid handle\n");
1624 r = SendMessageA(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)hTree);
1625 expect(0, r);
1626 ok(IsWindow(edit), "Expected edit control to be valid\n");
1627 r = SendMessageA(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)edit);
1628 expect(0, r);
1630 /* remove selection after starting edit */
1631 r = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hRoot);
1632 expect(TRUE, r);
1633 edit = (HWND)SendMessageA(hTree, TVM_EDITLABELA, 0, (LPARAM)hRoot);
1634 ok(IsWindow(edit), "Expected valid handle\n");
1635 r = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, 0);
1636 expect(TRUE, r);
1637 /* alter text */
1638 strcpy(buffA, "x");
1639 r = SendMessageA(edit, WM_SETTEXT, 0, (LPARAM)buffA);
1640 expect(TRUE, r);
1641 r = SendMessageA(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)edit);
1642 expect(0, r);
1643 ok(!IsWindow(edit), "Expected edit control to be destroyed\n");
1644 /* check that text is saved */
1645 item.mask = TVIF_TEXT;
1646 item.hItem = hRoot;
1647 item.pszText = buffA;
1648 item.cchTextMax = ARRAY_SIZE(buffA);
1649 r = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1650 expect(TRUE, r);
1651 ok(!strcmp("x", buffA), "Expected item text to change\n");
1653 /* try A/W messages */
1654 edit = (HWND)SendMessageA(hTree, TVM_EDITLABELA, 0, (LPARAM)hRoot);
1655 ok(IsWindow(edit), "Expected valid handle\n");
1656 ok(IsWindowUnicode(edit), "got ansi window\n");
1657 r = SendMessageA(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)edit);
1658 expect(0, r);
1659 ok(!IsWindow(edit), "expected invalid handle\n");
1661 edit = (HWND)SendMessageA(hTree, TVM_EDITLABELW, 0, (LPARAM)hRoot);
1662 ok(IsWindow(edit), "Expected valid handle\n");
1663 ok(IsWindowUnicode(edit), "got ansi window\n");
1664 r = SendMessageA(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)edit);
1665 expect(0, r);
1667 /* alter text during TVM_BEGINLABELEDIT, check that it's preserved */
1668 strcpy(buffA, "<root>");
1670 item.mask = TVIF_TEXT;
1671 item.hItem = hRoot;
1672 item.pszText = buffA;
1673 item.cchTextMax = 0;
1674 r = SendMessageA(hTree, TVM_SETITEMA, 0, (LPARAM)&item);
1675 expect(TRUE, r);
1677 g_beginedit_alter_text = TRUE;
1678 edit = (HWND)SendMessageA(hTree, TVM_EDITLABELA, 0, (LPARAM)hRoot);
1679 ok(IsWindow(edit), "Expected valid handle\n");
1680 g_beginedit_alter_text = FALSE;
1682 GetWindowTextA(edit, buffA, ARRAY_SIZE(buffA));
1683 ok(!strcmp(buffA, "<edittextaltered>"), "got string %s\n", buffA);
1685 r = SendMessageA(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)edit);
1686 expect(0, r);
1688 /* How much text can be typed? */
1689 edit = (HWND)SendMessageA(hTree, TVM_EDITLABELA, 0, (LPARAM)hRoot);
1690 ok(IsWindow(edit), "Expected valid handle\n");
1691 r = SendMessageA(edit, EM_GETLIMITTEXT, 0, 0);
1692 expect(MAX_PATH - 1, r);
1693 /* WM_SETTEXT can set more... */
1694 memset(buffA, 'a', ARRAY_SIZE(buffA));
1695 buffA[ARRAY_SIZE(buffA)-1] = 0;
1696 r = SetWindowTextA(edit, buffA);
1697 expect(TRUE, r);
1698 r = GetWindowTextA(edit, buffA, ARRAY_SIZE(buffA));
1699 ok( r == ARRAY_SIZE(buffA) - 1, "got %d\n", r );
1700 /* ...but it's trimmed to MAX_PATH chars when editing ends */
1701 r = SendMessageA(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)edit);
1702 expect(0, r);
1703 item.mask = TVIF_TEXT;
1704 item.hItem = hRoot;
1705 item.pszText = buffA;
1706 item.cchTextMax = ARRAY_SIZE(buffA);
1707 r = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1708 expect(TRUE, r);
1709 expect(MAX_PATH - 1, lstrlenA(item.pszText));
1711 /* We can't get around that MAX_PATH limit by increasing EM_SETLIMITTEXT */
1712 edit = (HWND)SendMessageA(hTree, TVM_EDITLABELA, 0, (LPARAM)hRoot);
1713 ok(IsWindow(edit), "Expected valid handle\n");
1714 SendMessageA(edit, EM_SETLIMITTEXT, ARRAY_SIZE(buffA)-1, 0);
1715 memset(buffA, 'a', ARRAY_SIZE(buffA));
1716 buffA[ARRAY_SIZE(buffA)-1] = 0;
1717 r = SetWindowTextA(edit, buffA);
1718 expect(TRUE, r);
1719 r = SendMessageA(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)edit);
1720 expect(0, r);
1721 item.mask = TVIF_TEXT;
1722 item.hItem = hRoot;
1723 item.pszText = buffA;
1724 item.cchTextMax = ARRAY_SIZE(buffA);
1725 r = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1726 expect(TRUE, r);
1727 expect(MAX_PATH - 1, lstrlenA(item.pszText));
1729 /* Overwriting of pszText contents in TVN_ENDLABELEDIT */
1730 edit = (HWND)SendMessageA(hTree, TVM_EDITLABELA, 0, (LPARAM)hRoot);
1731 ok(IsWindow(edit), "Expected valid handle\n");
1732 r = SetWindowTextA(edit, "old");
1733 expect(TRUE, r);
1734 g_endedit_overwrite_contents = "<new_contents>";
1735 r = SendMessageA(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)edit);
1736 expect(0, r);
1737 g_endedit_overwrite_contents = NULL;
1738 item.mask = TVIF_TEXT;
1739 item.hItem = hRoot;
1740 item.pszText = buffA;
1741 item.cchTextMax = ARRAY_SIZE(buffA);
1742 r = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1743 expect(TRUE, r);
1744 expect(0, strcmp(item.pszText, "<new_contents>"));
1746 /* Overwriting of pszText pointer in TVN_ENDLABELEDIT */
1747 edit = (HWND)SendMessageA(hTree, TVM_EDITLABELA, 0, (LPARAM)hRoot);
1748 ok(IsWindow(edit), "Expected valid handle\n");
1749 r = SetWindowTextA(edit, "old");
1750 expect(TRUE, r);
1751 g_endedit_overwrite_ptr = (char*) "<new_ptr>";
1752 r = SendMessageA(hTree, WM_COMMAND, MAKEWPARAM(0, EN_KILLFOCUS), (LPARAM)edit);
1753 expect(0, r);
1754 g_endedit_overwrite_ptr = NULL;
1755 item.mask = TVIF_TEXT;
1756 item.hItem = hRoot;
1757 item.pszText = buffA;
1758 item.cchTextMax = ARRAY_SIZE(buffA);
1759 r = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1760 expect(TRUE, r);
1761 expect(0, strcmp(item.pszText, "<new_ptr>"));
1763 DestroyWindow(hTree);
1766 static void test_treeview_classinfo(void)
1768 WNDCLASSA cls;
1770 memset(&cls, 0, sizeof(cls));
1771 GetClassInfoA(GetModuleHandleA("comctl32.dll"), WC_TREEVIEWA, &cls);
1772 ok(cls.hbrBackground == NULL, "Expected NULL background brush, got %p\n", cls.hbrBackground);
1773 ok(cls.style == (CS_GLOBALCLASS | CS_DBLCLKS), "Expected got %x\n", cls.style);
1774 expect(0, cls.cbClsExtra);
1777 static void test_get_linecolor(void)
1779 COLORREF clr;
1780 HWND hTree;
1782 hTree = create_treeview_control(0);
1784 /* newly created control has default color */
1785 clr = SendMessageA(hTree, TVM_GETLINECOLOR, 0, 0);
1786 if (clr == 0)
1787 win_skip("TVM_GETLINECOLOR is not supported on comctl32 < 5.80\n");
1788 else
1789 expect(CLR_DEFAULT, clr);
1791 DestroyWindow(hTree);
1794 static void test_get_insertmarkcolor(void)
1796 COLORREF clr;
1797 HWND hTree;
1799 hTree = create_treeview_control(0);
1801 /* newly created control has default color */
1802 clr = SendMessageA(hTree, TVM_GETINSERTMARKCOLOR, 0, 0);
1803 if (clr == 0)
1804 win_skip("TVM_GETINSERTMARKCOLOR is not supported on comctl32 < 5.80\n");
1805 else
1806 expect(CLR_DEFAULT, clr);
1808 DestroyWindow(hTree);
1811 static void test_expandnotify(void)
1813 HTREEITEM hitem;
1814 HWND hTree;
1815 BOOL ret;
1816 TVITEMA item;
1818 hTree = create_treeview_control(0);
1819 fill_tree(hTree);
1821 item.hItem = hRoot;
1822 item.mask = TVIF_STATE;
1824 item.state = TVIS_EXPANDED;
1825 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1826 expect(TRUE, ret);
1827 ok((item.state & TVIS_EXPANDED) == 0, "expected collapsed\n");
1829 /* preselect root node here */
1830 ret = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hRoot);
1831 expect(TRUE, ret);
1833 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1834 ret = SendMessageA(hTree, TVM_EXPAND, TVE_COLLAPSE, (LPARAM)hRoot);
1835 expect(FALSE, ret);
1836 ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq, "no collapse notifications", FALSE);
1838 g_get_from_expand = TRUE;
1839 /* expand */
1840 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1841 g_item_expanding.state = 0xdeadbeef;
1842 g_item_expanded.state = 0xdeadbeef;
1843 ret = SendMessageA(hTree, TVM_EXPAND, TVE_EXPAND, (LPARAM)hRoot);
1844 expect(TRUE, ret);
1845 ok(g_item_expanding.state == TVIS_SELECTED, "got state on TVN_ITEMEXPANDING 0x%08x\n",
1846 g_item_expanding.state);
1847 ok(g_item_expanded.state == (TVIS_SELECTED|TVIS_EXPANDED), "got state on TVN_ITEMEXPANDED 0x%08x\n",
1848 g_item_expanded.state);
1849 ok_sequence(sequences, PARENT_SEQ_INDEX, parent_expand_seq, "expand notifications", FALSE);
1850 g_get_from_expand = FALSE;
1852 /* check that it's expanded */
1853 item.state = TVIS_EXPANDED;
1854 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1855 expect(TRUE, ret);
1856 ok((item.state & TVIS_EXPANDED) == TVIS_EXPANDED, "expected expanded\n");
1858 /* collapse */
1859 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1860 ret = SendMessageA(hTree, TVM_EXPAND, TVE_COLLAPSE, (LPARAM)hRoot);
1861 expect(TRUE, ret);
1862 item.state = TVIS_EXPANDED;
1863 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
1864 expect(TRUE, ret);
1865 ok((item.state & TVIS_EXPANDED) == 0, "expected collapsed\n");
1866 /* all further collapse/expand attempts won't produce any notifications,
1867 the only way is to reset with all children removed */
1868 ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq, "collapse after expand notifications", FALSE);
1870 /* try to toggle child that doesn't have children itself */
1871 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1872 ret = SendMessageA(hTree, TVM_EXPAND, TVE_TOGGLE, (LPARAM)hChild);
1873 expect(FALSE, ret);
1874 ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq, "toggle node without children", FALSE);
1876 DestroyWindow(hTree);
1878 /* test TVM_GETITEMRECT inside TVN_ITEMEXPANDED notification */
1879 hTree = create_treeview_control(0);
1880 fill_tree(hTree);
1881 g_get_rect_in_expand = TRUE;
1882 ret = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hChild);
1883 expect(TRUE, ret);
1884 g_get_rect_in_expand = FALSE;
1886 DestroyWindow(hTree);
1888 /* TVE_TOGGLE acts as any other TVM_EXPAND */
1889 hTree = create_treeview_control(0);
1890 fill_tree(hTree);
1892 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1893 ret = SendMessageA(hTree, TVM_EXPAND, TVE_TOGGLE, (LPARAM)hRoot);
1894 expect(TRUE, ret);
1895 ok_sequence(sequences, PARENT_SEQ_INDEX, parent_expand_seq, "toggle node (expand)", FALSE);
1897 /* toggle again - no notifications */
1898 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1899 ret = SendMessageA(hTree, TVM_EXPAND, TVE_TOGGLE, (LPARAM)hRoot);
1900 expect(TRUE, ret);
1901 ok_sequence(sequences, PARENT_SEQ_INDEX, empty_seq, "toggle node (collapse)", FALSE);
1903 DestroyWindow(hTree);
1905 /* some keyboard events are also translated to expand */
1906 hTree = create_treeview_control(0);
1907 fill_tree(hTree);
1909 /* preselect root node here */
1910 ret = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hRoot);
1911 expect(TRUE, ret);
1913 g_get_from_expand = TRUE;
1914 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1915 ret = SendMessageA(hTree, WM_KEYDOWN, VK_ADD, 0);
1916 expect(FALSE, ret);
1917 ok_sequence(sequences, PARENT_SEQ_INDEX, parent_expand_kb_seq, "expand node", FALSE);
1918 ok(g_item_expanding.state == TVIS_SELECTED, "got state on TVN_ITEMEXPANDING 0x%08x\n",
1919 g_item_expanding.state);
1920 ok(g_item_expanded.state == (TVIS_SELECTED|TVIS_EXPANDED), "got state on TVN_ITEMEXPANDED 0x%08x\n",
1921 g_item_expanded.state);
1923 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1924 ret = SendMessageA(hTree, WM_KEYDOWN, VK_ADD, 0);
1925 expect(FALSE, ret);
1926 ok_sequence(sequences, PARENT_SEQ_INDEX, parent_expand_kb_seq, "expand node again", FALSE);
1927 ok(g_item_expanding.state == (TVIS_SELECTED|TVIS_EXPANDED|TVIS_EXPANDEDONCE), "got state on TVN_ITEMEXPANDING 0x%08x\n",
1928 g_item_expanding.state);
1929 ok(g_item_expanded.state == (TVIS_SELECTED|TVIS_EXPANDED|TVIS_EXPANDEDONCE), "got state on TVN_ITEMEXPANDED 0x%08x\n",
1930 g_item_expanded.state);
1932 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1933 ret = SendMessageA(hTree, WM_KEYDOWN, VK_SUBTRACT, 0);
1934 expect(FALSE, ret);
1935 ok_sequence(sequences, PARENT_SEQ_INDEX, parent_expand_kb_seq, "collapse node", FALSE);
1936 ok(g_item_expanding.state == (TVIS_SELECTED|TVIS_EXPANDED|TVIS_EXPANDEDONCE), "got state on TVN_ITEMEXPANDING 0x%08x\n",
1937 g_item_expanding.state);
1938 ok(g_item_expanded.state == (TVIS_SELECTED|TVIS_EXPANDEDONCE), "got state on TVN_ITEMEXPANDED 0x%08x\n",
1939 g_item_expanded.state);
1941 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1942 ret = SendMessageA(hTree, WM_KEYDOWN, VK_SUBTRACT, 0);
1943 expect(FALSE, ret);
1944 ok_sequence(sequences, PARENT_SEQ_INDEX, parent_collapse_2nd_kb_seq, "collapse node again", FALSE);
1945 ok(g_item_expanding.state == (TVIS_SELECTED|TVIS_EXPANDEDONCE), "got state on TVN_ITEMEXPANDING 0x%08x\n",
1946 g_item_expanding.state);
1947 g_get_from_expand = FALSE;
1949 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1950 ret = SendMessageA(hTree, WM_KEYDOWN, VK_ADD, 0);
1951 expect(FALSE, ret);
1952 ok_sequence(sequences, PARENT_SEQ_INDEX, parent_expand_kb_seq, "expand node", FALSE);
1954 /* go to child */
1955 ret = SendMessageA(hTree, WM_KEYDOWN, VK_RIGHT, 0);
1956 expect(FALSE, ret);
1958 /* try to expand child that doesn't have children itself */
1959 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1960 ret = SendMessageA(hTree, WM_KEYDOWN, VK_ADD, 0);
1961 expect(FALSE, ret);
1962 ok_sequence(sequences, PARENT_SEQ_INDEX, parent_expand_empty_kb_seq, "expand node with no children", FALSE);
1964 /* stay on current selection and set non-zero children count */
1965 hitem = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_CARET, 0);
1966 ok(hitem != NULL, "got %p\n", hitem);
1968 item.hItem = hitem;
1969 item.mask = TVIF_CHILDREN;
1970 item.cChildren = 0x80000000;
1972 ret = SendMessageA(hTree, TVM_SETITEMA, 0, (LPARAM)&item);
1973 expect(TRUE, ret);
1975 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1976 ret = SendMessageA(hTree, WM_KEYDOWN, VK_ADD, 0);
1977 expect(FALSE, ret);
1978 ok_sequence(sequences, PARENT_SEQ_INDEX, parent_collapse_2nd_kb_seq, "expand node with children", FALSE);
1980 DestroyWindow(hTree);
1983 static void test_expandedimage(void)
1985 TVITEMEXA item;
1986 HWND hTree;
1987 BOOL ret;
1989 hTree = create_treeview_control(0);
1990 fill_tree(hTree);
1992 item.mask = TVIF_EXPANDEDIMAGE;
1993 item.iExpandedImage = 1;
1994 item.hItem = hRoot;
1995 ret = SendMessageA(hTree, TVM_SETITEMA, 0, (LPARAM)&item);
1996 ok(ret, "got %d\n", ret);
1998 item.mask = TVIF_EXPANDEDIMAGE;
1999 item.iExpandedImage = -1;
2000 item.hItem = hRoot;
2001 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
2002 ok(ret, "got %d\n", ret);
2004 if (item.iExpandedImage != 1)
2006 win_skip("TVIF_EXPANDEDIMAGE not supported\n");
2007 DestroyWindow(hTree);
2008 return;
2011 /* test for default iExpandedImage value */
2012 item.mask = TVIF_EXPANDEDIMAGE;
2013 item.iExpandedImage = -1;
2014 item.hItem = hChild;
2015 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
2016 ok(ret, "got %d\n", ret);
2017 ok(item.iExpandedImage == (WORD)I_IMAGENONE, "got %d\n", item.iExpandedImage);
2019 DestroyWindow(hTree);
2022 static void test_TVS_SINGLEEXPAND(void)
2024 HWND hTree;
2025 HTREEITEM alpha, bravo, charlie, delta, echo, foxtrot, golf, hotel, india, juliet;
2026 TVINSERTSTRUCTA ins;
2027 char foo[] = "foo";
2028 char context[32];
2029 int i;
2030 BOOL ret;
2032 /* build a fairly complex tree
2033 * - TVI_ROOT
2034 * - alpha
2035 * - bravo
2036 * - charlie
2037 * - delta
2038 * - echo
2039 * - foxtrot
2040 * - golf
2041 * - hotel
2042 * - india
2043 * - juliet
2045 struct
2047 HTREEITEM *handle;
2048 HTREEITEM *parent;
2049 UINT final_state;
2051 items[] =
2053 { &alpha, NULL, TVIS_EXPANDEDONCE },
2054 { &bravo, &alpha, TVIS_EXPANDEDONCE },
2055 { &charlie, &bravo, 0 },
2056 { &delta, &alpha, TVIS_EXPANDEDONCE },
2057 { &echo, &delta, 0 },
2058 { &foxtrot, NULL, TVIS_EXPANDEDONCE|TVIS_EXPANDED },
2059 { &golf, &foxtrot, TVIS_EXPANDEDONCE|TVIS_EXPANDED },
2060 { &hotel, &golf, 0 },
2061 { &india, &golf, TVIS_SELECTED },
2062 { &juliet, &foxtrot, 0 }
2065 struct
2067 HTREEITEM *select;
2068 const struct message *sequence;
2070 sequence_tests[] =
2072 { &alpha, parent_singleexpand_seq0 },
2073 { &bravo, parent_singleexpand_seq1 },
2074 { &delta, parent_singleexpand_seq2 },
2075 { &foxtrot, parent_singleexpand_seq3 },
2076 { &alpha, parent_singleexpand_seq4 },
2077 { &golf, parent_singleexpand_seq5 },
2078 { &hotel, parent_singleexpand_seq6 },
2079 { &india, parent_singleexpand_seq7 },
2080 { &india, empty_seq }
2083 hTree = create_treeview_control(0);
2084 SetWindowLongA(hTree, GWL_STYLE, GetWindowLongA(hTree, GWL_STYLE) | TVS_SINGLEEXPAND);
2085 /* to avoid painting related notifications */
2086 ShowWindow(hTree, SW_HIDE);
2087 for (i = 0; i < ARRAY_SIZE(items); i++)
2089 ins.hParent = items[i].parent ? *items[i].parent : TVI_ROOT;
2090 ins.hInsertAfter = TVI_FIRST;
2091 U(ins).item.mask = TVIF_TEXT;
2092 U(ins).item.pszText = foo;
2093 *items[i].handle = TreeView_InsertItemA(hTree, &ins);
2096 for (i = 0; i < ARRAY_SIZE(sequence_tests); i++)
2098 flush_sequences(sequences, NUM_MSG_SEQUENCES);
2099 ret = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)(*sequence_tests[i].select));
2100 ok(ret, "got %d\n", ret);
2101 sprintf(context, "singleexpand notifications %d", i);
2102 ok_sequence(sequences, PARENT_SEQ_INDEX, sequence_tests[i].sequence, context, FALSE);
2105 for (i = 0; i < ARRAY_SIZE(items); i++)
2107 ret = SendMessageA(hTree, TVM_GETITEMSTATE, (WPARAM)(*items[i].handle), 0xFFFF);
2108 ok(ret == items[i].final_state, "singleexpand items[%d]: expected state 0x%x got 0x%x\n",
2109 i, items[i].final_state, ret);
2112 /* a workaround for NT4 that sends expand notifications when nothing is about to expand */
2113 ret = SendMessageA(hTree, TVM_DELETEITEM, 0, (LPARAM)TVI_ROOT);
2114 ok(ret, "got %d\n", ret);
2115 fill_tree(hTree);
2116 ret = SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, 0);
2117 ok(ret, "got %d\n", ret);
2119 DestroyWindow(hTree);
2122 static void test_WM_PAINT(void)
2124 HWND hTree;
2125 COLORREF clr;
2126 LONG ret;
2127 RECT rc;
2128 HDC hdc;
2130 hTree = create_treeview_control(0);
2132 clr = SendMessageA(hTree, TVM_SETBKCOLOR, 0, RGB(255, 0, 0));
2133 ok(clr == ~0u, "got %d, expected -1\n", clr);
2135 hdc = GetDC(hMainWnd);
2137 GetClientRect(hMainWnd, &rc);
2138 FillRect(hdc, &rc, GetStockObject(BLACK_BRUSH));
2140 clr = GetPixel(hdc, 1, 1);
2141 ok(clr == RGB(0, 0, 0), "got 0x%x\n", clr);
2143 ret = SendMessageA(hTree, WM_PAINT, (WPARAM)hdc, 0);
2144 ok(ret == 0, "got %d\n", ret);
2146 clr = GetPixel(hdc, 1, 1);
2147 ok(clr == RGB(255, 0, 0) || broken(clr == RGB(0, 0, 0)) /* win98 */,
2148 "got 0x%x\n", clr);
2150 ReleaseDC(hMainWnd, hdc);
2152 DestroyWindow(hTree);
2155 static void test_delete_items(void)
2157 const struct message *msg;
2158 HWND hTree;
2159 HTREEITEM hItem1, hItem2;
2160 TVINSERTSTRUCTA ins;
2161 INT ret;
2163 static CHAR item1[] = "Item 1";
2164 static CHAR item2[] = "Item 2";
2166 hTree = create_treeview_control(0);
2167 fill_tree(hTree);
2169 /* check delete order */
2170 flush_sequences(item_sequence, 1);
2171 ret = SendMessageA(hTree, TVM_DELETEITEM, 0, 0);
2172 ok(ret == TRUE, "got %d\n", ret);
2174 msg = item_sequence[0]->sequence;
2175 ok(item_sequence[0]->count == 2, "expected 2 items, got %d\n", item_sequence[0]->count);
2177 if (item_sequence[0]->count == 2)
2179 ok(msg[0].lParam == (LPARAM)hChild, "expected %p, got 0x%lx\n", hChild, msg[0].lParam);
2180 ok(msg[1].lParam == (LPARAM)hRoot, "expected %p, got 0x%lx\n", hRoot, msg[1].lParam);
2183 ret = SendMessageA(hTree, TVM_GETCOUNT, 0, 0);
2184 ok(ret == 0, "got %d\n", ret);
2186 DestroyWindow(hTree);
2188 /* Regression test for a crash when deleting the first visible item while bRedraw == false. */
2189 hTree = create_treeview_control(0);
2191 ret = SendMessageA(hTree, WM_SETREDRAW, FALSE, 0);
2192 ok(ret == 0, "got %d\n", ret);
2194 ins.hParent = TVI_ROOT;
2195 ins.hInsertAfter = TVI_ROOT;
2196 U(ins).item.mask = TVIF_TEXT;
2197 U(ins).item.pszText = item1;
2198 hItem1 = TreeView_InsertItemA(hTree, &ins);
2199 ok(hItem1 != NULL, "InsertItem failed\n");
2201 ins.hParent = TVI_ROOT;
2202 ins.hInsertAfter = hItem1;
2203 U(ins).item.mask = TVIF_TEXT;
2204 U(ins).item.pszText = item2;
2205 hItem2 = TreeView_InsertItemA(hTree, &ins);
2206 ok(hItem2 != NULL, "InsertItem failed\n");
2208 ret = SendMessageA(hTree, TVM_DELETEITEM, 0, (LPARAM)hItem1);
2209 ok(ret == TRUE, "got %d\n", ret);
2211 ret = SendMessageA(hTree, WM_SETREDRAW, TRUE, 0);
2212 ok(ret == 0, "got %d\n", ret);
2214 DestroyWindow(hTree);
2217 static void test_cchildren(void)
2219 HWND hTree;
2220 INT ret;
2221 TVITEMA item;
2223 hTree = create_treeview_control(0);
2224 fill_tree(hTree);
2226 ret = SendMessageA(hTree, TVM_DELETEITEM, 0, (LPARAM)hChild);
2227 expect(TRUE, ret);
2229 /* check cChildren - automatic mode */
2230 item.hItem = hRoot;
2231 item.mask = TVIF_CHILDREN;
2233 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
2234 expect(TRUE, ret);
2235 expect(0, item.cChildren);
2237 DestroyWindow(hTree);
2239 /* start over */
2240 hTree = create_treeview_control(0);
2241 fill_tree(hTree);
2243 /* turn off automatic mode by setting cChildren explicitly */
2244 item.hItem = hRoot;
2245 item.mask = TVIF_CHILDREN;
2247 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
2248 expect(TRUE, ret);
2249 expect(1, item.cChildren);
2251 ret = SendMessageA(hTree, TVM_SETITEMA, 0, (LPARAM)&item);
2252 expect(TRUE, ret);
2254 ret = SendMessageA(hTree, TVM_DELETEITEM, 0, (LPARAM)hChild);
2255 expect(TRUE, ret);
2257 /* check cChildren */
2258 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
2259 expect(TRUE, ret);
2260 todo_wine
2261 expect(1, item.cChildren);
2263 DestroyWindow(hTree);
2266 struct _ITEM_DATA
2268 HTREEITEM parent; /* for root value of parent field is unidetified */
2269 HTREEITEM nextsibling;
2270 HTREEITEM firstchild;
2271 void *unk[2];
2272 DWORD unk2;
2273 WORD pad;
2274 WORD width;
2277 struct _ITEM_DATA_V6
2279 HTREEITEM parent; /* for root value of parent field is unidetified */
2280 HTREEITEM nextsibling;
2281 HTREEITEM firstchild;
2282 void *unk[3];
2283 DWORD unk2[2];
2284 WORD pad;
2285 WORD width;
2288 static void _check_item(HWND hwnd, HTREEITEM item, BOOL is_version_6, int line)
2290 struct _ITEM_DATA *data = (struct _ITEM_DATA *)item;
2291 HTREEITEM parent, nextsibling, firstchild, root;
2292 RECT rect;
2293 BOOL ret;
2295 root = (HTREEITEM)SendMessageA(hwnd, TVM_GETNEXTITEM, TVGN_ROOT, (LPARAM)item);
2296 parent = (HTREEITEM)SendMessageA(hwnd, TVM_GETNEXTITEM, TVGN_PARENT, (LPARAM)item);
2297 nextsibling = (HTREEITEM)SendMessageA(hwnd, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)item);
2298 firstchild = (HTREEITEM)SendMessageA(hwnd, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)item);
2300 *(HTREEITEM*)&rect = item;
2301 ret = SendMessageA(hwnd, TVM_GETITEMRECT, TRUE, (LPARAM)&rect);
2303 ok_(__FILE__, line)(item == root ? data->parent != NULL : data->parent == parent,
2304 "Unexpected parent item %p, got %p, %p\n", parent, data->parent, hwnd);
2305 ok_(__FILE__, line)(data->nextsibling == nextsibling, "Unexpected sibling %p, got %p\n",
2306 nextsibling, data->nextsibling);
2307 ok_(__FILE__, line)(data->firstchild == firstchild, "Unexpected first child %p, got %p\n",
2308 firstchild, data->firstchild);
2309 if (ret)
2311 WORD width;
2313 if (is_version_6)
2315 struct _ITEM_DATA_V6 *data_v6 = (struct _ITEM_DATA_V6 *)item;
2316 width = data_v6->width;
2318 else
2319 width = data->width;
2320 todo_wine
2321 ok_(__FILE__, line)(width == (rect.right - rect.left) || broken(is_version_6 && width == 0) /* XP */,
2322 "Width %d, rect width %d.\n", width, rect.right - rect.left);
2326 #define CHECK_ITEM(a, b) _check_item(a, b, is_version_6, __LINE__)
2328 static void test_htreeitem_layout(BOOL is_version_6)
2330 TVINSERTSTRUCTA ins;
2331 HTREEITEM item1, item2;
2332 HWND hTree;
2334 hTree = create_treeview_control(0);
2335 fill_tree(hTree);
2337 /* root has some special pointer in parent field */
2338 CHECK_ITEM(hTree, hRoot);
2339 CHECK_ITEM(hTree, hChild);
2341 ins.hParent = hChild;
2342 ins.hInsertAfter = TVI_FIRST;
2343 U(ins).item.mask = 0;
2344 item1 = TreeView_InsertItemA(hTree, &ins);
2346 CHECK_ITEM(hTree, item1);
2348 ins.hParent = hRoot;
2349 ins.hInsertAfter = TVI_FIRST;
2350 U(ins).item.mask = 0;
2351 item2 = TreeView_InsertItemA(hTree, &ins);
2353 CHECK_ITEM(hTree, item2);
2355 SendMessageA(hTree, TVM_DELETEITEM, 0, (LPARAM)hChild);
2357 /* without children now */
2358 CHECK_ITEM(hTree, hRoot);
2360 DestroyWindow(hTree);
2363 static void test_TVS_CHECKBOXES(void)
2365 HIMAGELIST himl, himl2;
2366 HWND hTree, hTree2;
2367 TVITEMA item;
2368 DWORD ret;
2369 MSG msg;
2371 hTree = create_treeview_control(0);
2372 fill_tree(hTree);
2374 himl = (HIMAGELIST)SendMessageA(hTree, TVM_GETIMAGELIST, TVSIL_STATE, 0);
2375 ok(himl == NULL, "got %p\n", himl);
2377 item.hItem = hRoot;
2378 item.mask = TVIF_STATE;
2379 item.state = INDEXTOSTATEIMAGEMASK(1);
2380 item.stateMask = TVIS_STATEIMAGEMASK;
2381 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
2382 expect(TRUE, ret);
2383 ok(item.state == 0, "got 0x%x\n", item.state);
2385 /* set some index for a child */
2386 item.hItem = hChild;
2387 item.mask = TVIF_STATE;
2388 item.state = INDEXTOSTATEIMAGEMASK(4);
2389 item.stateMask = TVIS_STATEIMAGEMASK;
2390 ret = SendMessageA(hTree, TVM_SETITEMA, 0, (LPARAM)&item);
2391 expect(TRUE, ret);
2393 /* enabling check boxes set all items to 1 state image index */
2394 SetWindowLongA(hTree, GWL_STYLE, GetWindowLongA(hTree, GWL_STYLE) | TVS_CHECKBOXES);
2395 himl = (HIMAGELIST)SendMessageA(hTree, TVM_GETIMAGELIST, TVSIL_STATE, 0);
2396 ok(himl != NULL, "got %p\n", himl);
2398 himl2 = (HIMAGELIST)SendMessageA(hTree, TVM_GETIMAGELIST, TVSIL_STATE, 0);
2399 ok(himl2 != NULL, "got %p\n", himl2);
2400 ok(himl2 == himl, "got %p, expected %p\n", himl2, himl);
2402 item.hItem = hRoot;
2403 item.mask = TVIF_STATE;
2404 item.state = 0;
2405 item.stateMask = TVIS_STATEIMAGEMASK;
2406 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
2407 expect(TRUE, ret);
2408 ok(item.state == INDEXTOSTATEIMAGEMASK(1), "got 0x%x\n", item.state);
2410 item.hItem = hChild;
2411 item.mask = TVIF_STATE;
2412 item.state = 0;
2413 item.stateMask = TVIS_STATEIMAGEMASK;
2414 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
2415 expect(TRUE, ret);
2416 ok(item.state == INDEXTOSTATEIMAGEMASK(1), "got 0x%x\n", item.state);
2418 /* create another control and check its checkbox list */
2419 hTree2 = create_treeview_control(0);
2420 fill_tree(hTree2);
2422 /* set some index for a child */
2423 item.hItem = hChild;
2424 item.mask = TVIF_STATE;
2425 item.state = INDEXTOSTATEIMAGEMASK(4);
2426 item.stateMask = TVIS_STATEIMAGEMASK;
2427 ret = SendMessageA(hTree2, TVM_SETITEMA, 0, (LPARAM)&item);
2428 expect(TRUE, ret);
2430 /* enabling check boxes set all items to 1 state image index */
2431 SetWindowLongA(hTree2, GWL_STYLE, GetWindowLongA(hTree, GWL_STYLE) | TVS_CHECKBOXES);
2432 himl2 = (HIMAGELIST)SendMessageA(hTree2, TVM_GETIMAGELIST, TVSIL_STATE, 0);
2433 ok(himl2 != NULL, "got %p\n", himl2);
2434 ok(himl != himl2, "got %p, expected %p\n", himl2, himl);
2436 DestroyWindow(hTree2);
2437 DestroyWindow(hTree);
2439 /* the same, but initially created with TVS_CHECKBOXES */
2440 hTree = create_treeview_control(TVS_CHECKBOXES);
2441 fill_tree(hTree);
2442 himl = (HIMAGELIST)SendMessageA(hTree, TVM_GETIMAGELIST, TVSIL_STATE, 0);
2443 ok(himl == NULL, "got %p\n", himl);
2445 item.hItem = hRoot;
2446 item.mask = TVIF_STATE;
2447 item.state = 0;
2448 item.stateMask = TVIS_STATEIMAGEMASK;
2449 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
2450 expect(TRUE, ret);
2451 ok(item.state == INDEXTOSTATEIMAGEMASK(1), "got 0x%x\n", item.state);
2453 item.hItem = hChild;
2454 item.mask = TVIF_STATE;
2455 item.state = 0;
2456 item.stateMask = TVIS_STATEIMAGEMASK;
2457 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
2458 expect(TRUE, ret);
2459 ok(item.state == INDEXTOSTATEIMAGEMASK(1), "got 0x%x\n", item.state);
2461 item.hItem = hChild;
2462 item.mask = TVIF_STATE;
2463 item.state = INDEXTOSTATEIMAGEMASK(2);
2464 item.stateMask = TVIS_STATEIMAGEMASK;
2465 ret = SendMessageA(hTree, TVM_SETITEMA, 0, (LPARAM)&item);
2466 expect(TRUE, ret);
2468 item.hItem = hChild;
2469 item.mask = TVIF_STATE;
2470 item.state = 0;
2471 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
2472 expect(TRUE, ret);
2473 ok(item.state == INDEXTOSTATEIMAGEMASK(2), "got 0x%x\n", item.state);
2475 while(GetMessageA(&msg, 0, 0, 0))
2477 TranslateMessage(&msg);
2478 DispatchMessageA(&msg);
2480 if((msg.hwnd == hTree) && (msg.message == WM_PAINT))
2481 break;
2484 item.hItem = hChild;
2485 item.mask = TVIF_STATE;
2486 item.state = 0;
2487 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
2488 expect(TRUE, ret);
2489 ok(item.state == INDEXTOSTATEIMAGEMASK(1), "got 0x%x\n", item.state);
2491 himl = (HIMAGELIST)SendMessageA(hTree, TVM_GETIMAGELIST, TVSIL_STATE, 0);
2492 ok(himl != NULL, "got %p\n", himl);
2494 DestroyWindow(hTree);
2496 /* check what happens if TVSIL_STATE image list is removed */
2497 hTree = create_treeview_control(0);
2498 fill_tree(hTree);
2499 himl = (HIMAGELIST)SendMessageA(hTree, TVM_GETIMAGELIST, TVSIL_STATE, 0);
2500 ok(himl == NULL, "got %p\n", himl);
2502 SetWindowLongA(hTree, GWL_STYLE, GetWindowLongA(hTree, GWL_STYLE) | TVS_CHECKBOXES);
2503 himl = (HIMAGELIST)SendMessageA(hTree, TVM_GETIMAGELIST, TVSIL_STATE, 0);
2504 ok(himl != NULL, "got %p\n", himl);
2506 himl2 = (HIMAGELIST)SendMessageA(hTree, TVM_SETIMAGELIST, TVSIL_STATE, 0);
2507 ok(himl2 == himl, "got %p\n", himl2);
2509 himl2 = (HIMAGELIST)SendMessageA(hTree, TVM_GETIMAGELIST, TVSIL_STATE, 0);
2510 ok(himl2 == NULL, "got %p\n", himl2);
2512 item.hItem = hChild;
2513 item.mask = TVIF_STATE;
2514 item.state = INDEXTOSTATEIMAGEMASK(2);
2515 item.stateMask = TVIS_STATEIMAGEMASK;
2516 ret = SendMessageA(hTree, TVM_SETITEMA, 0, (LPARAM)&item);
2517 expect(TRUE, ret);
2519 item.hItem = hChild;
2520 item.mask = TVIF_STATE;
2521 item.state = 0;
2522 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
2523 expect(TRUE, ret);
2524 ok(item.state == INDEXTOSTATEIMAGEMASK(2), "got 0x%x\n", item.state);
2526 while(GetMessageA(&msg, 0, 0, 0))
2528 TranslateMessage(&msg);
2529 DispatchMessageA(&msg);
2531 if((msg.hwnd == hTree) && (msg.message == WM_PAINT))
2532 break;
2535 item.hItem = hChild;
2536 item.mask = TVIF_STATE;
2537 item.state = 0;
2538 ret = SendMessageA(hTree, TVM_GETITEMA, 0, (LPARAM)&item);
2539 expect(TRUE, ret);
2540 ok(item.state == INDEXTOSTATEIMAGEMASK(1), "got 0x%x\n", item.state);
2542 himl = (HIMAGELIST)SendMessageA(hTree, TVM_GETIMAGELIST, TVSIL_STATE, 0);
2543 ok(himl != NULL, "got %p\n", himl);
2545 DestroyWindow(hTree);
2548 static void test_TVM_GETNEXTITEM(void)
2550 HTREEITEM item;
2551 HWND hTree;
2553 hTree = create_treeview_control(0);
2554 fill_tree(hTree);
2556 item = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_ROOT, 0);
2557 ok(item == hRoot, "got %p, expected %p\n", item, hRoot);
2559 item = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_ROOT, (LPARAM)TVI_ROOT);
2560 ok(item == hRoot, "got %p, expected %p\n", item, hRoot);
2562 item = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_ROOT, (LPARAM)hRoot);
2563 ok(item == hRoot, "got %p, expected %p\n", item, hRoot);
2565 item = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_ROOT, (LPARAM)hChild);
2566 ok(item == hRoot, "got %p, expected %p\n", item, hRoot);
2568 item = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_CHILD, 0);
2569 ok(item == hRoot, "got %p, expected %p\n", item, hRoot);
2571 item = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hRoot);
2572 ok(item == hChild, "got %p, expected %p\n", item, hChild);
2574 item = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)TVI_ROOT);
2575 ok(item == hRoot, "got %p, expected %p\n", item, hRoot);
2577 item = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_PARENT, 0);
2578 ok(item == NULL, "got %p\n", item);
2580 item = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_PARENT, (LPARAM)hChild);
2581 ok(item == hRoot, "got %p, expected %p\n", item, hRoot);
2583 DestroyWindow(hTree);
2586 static void test_TVM_HITTEST(void)
2588 HWND hTree;
2589 LRESULT ret;
2590 RECT rc;
2591 TVHITTESTINFO ht;
2593 hTree = create_treeview_control(0);
2594 fill_tree(hTree);
2596 *(HTREEITEM*)&rc = hRoot;
2597 ret = SendMessageA(hTree, TVM_GETITEMRECT, TRUE, (LPARAM)&rc);
2598 expect(TRUE, (BOOL)ret);
2600 ht.pt.x = rc.left-1;
2601 ht.pt.y = rc.top;
2603 ret = SendMessageA(hTree, TVM_HITTEST, 0, (LPARAM)&ht);
2604 ok((HTREEITEM)ret == hRoot, "got %p, expected %p\n", (HTREEITEM)ret, hRoot);
2605 ok(ht.hItem == hRoot, "got %p, expected %p\n", ht.hItem, hRoot);
2606 ok(ht.flags == TVHT_ONITEMBUTTON, "got %d, expected %d\n", ht.flags, TVHT_ONITEMBUTTON);
2608 ret = SendMessageA(hTree, TVM_EXPAND, TVE_EXPAND, (LPARAM)hRoot);
2609 expect(TRUE, (BOOL)ret);
2611 *(HTREEITEM*)&rc = hChild;
2612 ret = SendMessageA(hTree, TVM_GETITEMRECT, TRUE, (LPARAM)&rc);
2613 expect(TRUE, (BOOL)ret);
2615 ht.pt.x = rc.left-1;
2616 ht.pt.y = rc.top;
2618 ret = SendMessageA(hTree, TVM_HITTEST, 0, (LPARAM)&ht);
2619 ok((HTREEITEM)ret == hChild, "got %p, expected %p\n", (HTREEITEM)ret, hChild);
2620 ok(ht.hItem == hChild, "got %p, expected %p\n", ht.hItem, hChild);
2621 /* Wine returns item button here, but this item has no button */
2622 todo_wine ok(ht.flags == TVHT_ONITEMINDENT, "got %d, expected %d\n", ht.flags, TVHT_ONITEMINDENT);
2624 DestroyWindow(hTree);
2627 static void test_WM_GETDLGCODE(void)
2629 DWORD code;
2630 HWND hTree;
2632 hTree = create_treeview_control(0);
2634 code = SendMessageA(hTree, WM_GETDLGCODE, VK_TAB, 0);
2635 ok(code == (DLGC_WANTCHARS | DLGC_WANTARROWS), "0x%08x\n", code);
2637 DestroyWindow(hTree);
2640 static void test_customdraw(void)
2642 LOGFONTA lf;
2643 HWND hwnd;
2645 hwnd = create_treeview_control(0);
2646 fill_tree(hwnd);
2647 SendMessageA(hwnd, TVM_EXPAND, TVE_EXPAND, (WPARAM)hRoot);
2649 /* create additional font, custom draw handler will select it */
2650 SystemParametersInfoA(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
2651 lf.lfHeight *= 2;
2652 g_customdraw_font = CreateFontIndirectA(&lf);
2653 flush_sequences(sequences, NUM_MSG_SEQUENCES);
2654 InvalidateRect(hwnd, NULL, TRUE);
2655 UpdateWindow(hwnd);
2656 ok_sequence(sequences, PARENT_CD_SEQ_INDEX, parent_cd_seq, "custom draw notifications", FALSE);
2657 DeleteObject(g_customdraw_font);
2658 g_customdraw_font = NULL;
2660 DestroyWindow(hwnd);
2663 static void test_WM_KEYDOWN(void)
2665 static const char *rootA = "root";
2666 TVINSERTSTRUCTA ins;
2667 HTREEITEM hRoot;
2668 HWND hwnd;
2670 hwnd = create_treeview_control(0);
2672 ins.hParent = TVI_ROOT;
2673 ins.hInsertAfter = TVI_ROOT;
2674 U(ins).item.mask = TVIF_TEXT;
2675 U(ins).item.pszText = (char*)rootA;
2676 hRoot = TreeView_InsertItemA(hwnd, &ins);
2677 ok(hRoot != NULL, "got %p\n", hRoot);
2679 flush_sequences(sequences, NUM_MSG_SEQUENCES);
2680 SendMessageA(hwnd, WM_KEYDOWN, VK_RETURN, 0);
2681 ok_sequence(sequences, PARENT_SEQ_INDEX, parent_vk_return_seq, "WM_KEYDOWN/VK_RETURN parent notification", TRUE);
2683 DestroyWindow(hwnd);
2686 static void test_TVS_FULLROWSELECT(void)
2688 DWORD style;
2689 HWND hwnd;
2691 /* try to create both with TVS_HASLINES and TVS_FULLROWSELECT */
2692 hwnd = create_treeview_control(TVS_FULLROWSELECT);
2694 style = GetWindowLongA(hwnd, GWL_STYLE);
2695 ok((style & (TVS_FULLROWSELECT | TVS_HASLINES)) == (TVS_FULLROWSELECT | TVS_HASLINES), "got style 0x%08x\n", style);
2697 DestroyWindow(hwnd);
2699 /* create just with TVS_HASLINES, try to enable TVS_FULLROWSELECT later */
2700 hwnd = create_treeview_control(0);
2702 style = GetWindowLongA(hwnd, GWL_STYLE);
2703 SetWindowLongA(hwnd, GWL_STYLE, style | TVS_FULLROWSELECT);
2704 style = GetWindowLongA(hwnd, GWL_STYLE);
2705 ok(style & TVS_FULLROWSELECT, "got style 0x%08x\n", style);
2707 DestroyWindow(hwnd);
2710 static void get_item_names_string(HWND hwnd, HTREEITEM item, char *str)
2712 TVITEMA tvitem = { 0 };
2713 HTREEITEM child;
2714 char name[16];
2716 if (!item)
2718 item = (HTREEITEM)SendMessageA(hwnd, TVM_GETNEXTITEM, TVGN_ROOT, 0);
2719 str[0] = 0;
2722 child = (HTREEITEM)SendMessageA(hwnd, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)item);
2724 tvitem.mask = TVIF_TEXT;
2725 tvitem.hItem = item;
2726 tvitem.pszText = name;
2727 tvitem.cchTextMax = sizeof(name);
2728 SendMessageA(hwnd, TVM_GETITEMA, 0, (LPARAM)&tvitem);
2729 strcat(str, tvitem.pszText);
2731 while (child != NULL)
2733 get_item_names_string(hwnd, child, str);
2734 child = (HTREEITEM)SendMessageA(hwnd, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)child);
2738 static void fill_treeview_sort_test(HWND hwnd)
2740 static const char *itemnames[] =
2742 "root", "Wasp", "Caribou", "Vacuum",
2743 "Ocelot", "Newspaper", "Litter bin"
2746 HTREEITEM root, children[2];
2747 TVINSERTSTRUCTA ins;
2748 unsigned i = 0;
2750 SendMessageA(hwnd, TVM_DELETEITEM, 0, 0);
2752 /* root, two children, with two children each */
2753 ins.hParent = TVI_ROOT;
2754 ins.hInsertAfter = TVI_ROOT;
2755 U(ins).item.mask = TVIF_TEXT;
2756 U(ins).item.pszText = (char *)itemnames[i++];
2757 root = (HTREEITEM)SendMessageA(hwnd, TVM_INSERTITEMA, 0, (LPARAM)&ins);
2759 ins.hParent = root;
2760 ins.hInsertAfter = TVI_LAST;
2761 U(ins).item.mask = TVIF_TEXT;
2762 U(ins).item.pszText = (char *)itemnames[i++];
2763 children[0] = (HTREEITEM)SendMessageA(hwnd, TVM_INSERTITEMA, 0, (LPARAM)&ins);
2765 U(ins).item.pszText = (char *)itemnames[i++];
2766 children[1] = (HTREEITEM)SendMessageA(hwnd, TVM_INSERTITEMA, 0, (LPARAM)&ins);
2768 ins.hParent = children[0];
2769 U(ins).item.pszText = (char *)itemnames[i++];
2770 SendMessageA(hwnd, TVM_INSERTITEMA, 0, (LPARAM)&ins);
2772 U(ins).item.pszText = (char *)itemnames[i++];
2773 SendMessageA(hwnd, TVM_INSERTITEMA, 0, (LPARAM)&ins);
2775 ins.hParent = children[1];
2776 U(ins).item.pszText = (char *)itemnames[i++];
2777 SendMessageA(hwnd, TVM_INSERTITEMA, 0, (LPARAM)&ins);
2779 U(ins).item.pszText = (char *)itemnames[i++];
2780 SendMessageA(hwnd, TVM_INSERTITEMA, 0, (LPARAM)&ins);
2783 static void test_TVM_SORTCHILDREN(void)
2785 static const char *initial_order = "rootWaspVacuumOcelotCaribouNewspaperLitter bin";
2786 static const char *sorted_order = "rootCaribouNewspaperLitter binWaspVacuumOcelot";
2787 TVINSERTSTRUCTA ins;
2788 char buff[256];
2789 HTREEITEM root;
2790 HWND hwnd;
2791 BOOL ret;
2793 hwnd = create_treeview_control(0);
2795 /* call on empty tree */
2796 ret = SendMessageA(hwnd, TVM_SORTCHILDREN, 0, 0);
2797 ok(!ret, "Unexpected ret value %d\n", ret);
2799 ret = SendMessageA(hwnd, TVM_SORTCHILDREN, 0, (LPARAM)TVI_ROOT);
2800 ok(!ret, "Unexpected ret value %d\n", ret);
2802 /* add only root, sort from it */
2803 ins.hParent = TVI_ROOT;
2804 ins.hInsertAfter = TVI_ROOT;
2805 U(ins).item.mask = TVIF_TEXT;
2806 U(ins).item.pszText = (char *)"root";
2807 root = (HTREEITEM)SendMessageA(hwnd, TVM_INSERTITEMA, 0, (LPARAM)&ins);
2808 ok(root != NULL, "Expected root node\n");
2810 ret = SendMessageA(hwnd, TVM_SORTCHILDREN, 0, (LPARAM)root);
2811 ok(!ret, "Unexpected ret value %d\n", ret);
2813 ret = SendMessageA(hwnd, TVM_SORTCHILDREN, TRUE, (LPARAM)root);
2814 ok(!ret, "Unexpected ret value %d\n", ret);
2816 /* root, two children, with two children each */
2817 fill_treeview_sort_test(hwnd);
2818 get_item_names_string(hwnd, NULL, buff);
2819 ok(!strcmp(buff, initial_order), "Wrong initial order %s, expected %s\n", buff, initial_order);
2821 /* with NULL item nothing is sorted */
2822 fill_treeview_sort_test(hwnd);
2823 ret = SendMessageA(hwnd, TVM_SORTCHILDREN, 0, 0);
2824 todo_wine
2825 ok(ret, "Unexpected ret value %d\n", ret);
2826 get_item_names_string(hwnd, NULL, buff);
2827 ok(!strcmp(buff, initial_order), "Wrong sorted order %s, expected %s\n", buff, initial_order);
2829 /* TVI_ROOT as item */
2830 fill_treeview_sort_test(hwnd);
2831 ret = SendMessageA(hwnd, TVM_SORTCHILDREN, 0, (LPARAM)TVI_ROOT);
2832 todo_wine
2833 ok(ret, "Unexpected ret value %d\n", ret);
2834 get_item_names_string(hwnd, NULL, buff);
2835 ok(!strcmp(buff, initial_order), "Wrong sorted order %s, expected %s\n", buff, initial_order);
2837 /* zero WPARAM, item is specified */
2838 fill_treeview_sort_test(hwnd);
2839 root = (HTREEITEM)SendMessageA(hwnd, TVM_GETNEXTITEM, TVGN_ROOT, 0);
2840 ok(root != NULL, "Failed to get root item\n");
2841 ret = SendMessageA(hwnd, TVM_SORTCHILDREN, 0, (LPARAM)root);
2842 ok(ret, "Unexpected ret value %d\n", ret);
2843 get_item_names_string(hwnd, NULL, buff);
2844 ok(!strcmp(buff, sorted_order), "Wrong sorted order %s, expected %s\n", buff, sorted_order);
2846 /* non-zero WPARAM, NULL item */
2847 fill_treeview_sort_test(hwnd);
2848 ret = SendMessageA(hwnd, TVM_SORTCHILDREN, TRUE, 0);
2849 todo_wine
2850 ok(ret, "Unexpected ret value %d\n", ret);
2851 get_item_names_string(hwnd, NULL, buff);
2852 ok(!strcmp(buff, initial_order), "Wrong sorted order %s, expected %s\n", buff, sorted_order);
2854 /* TVI_ROOT as item */
2855 fill_treeview_sort_test(hwnd);
2856 ret = SendMessageA(hwnd, TVM_SORTCHILDREN, TRUE, (LPARAM)TVI_ROOT);
2857 todo_wine
2858 ok(ret, "Unexpected ret value %d\n", ret);
2859 get_item_names_string(hwnd, NULL, buff);
2860 ok(!strcmp(buff, initial_order), "Wrong sorted order %s, expected %s\n", buff, sorted_order);
2862 /* non-zero WPARAM, item is specified */
2863 fill_treeview_sort_test(hwnd);
2864 root = (HTREEITEM)SendMessageA(hwnd, TVM_GETNEXTITEM, TVGN_ROOT, 0);
2865 ok(root != NULL, "Failed to get root item\n");
2866 ret = SendMessageA(hwnd, TVM_SORTCHILDREN, TRUE, (LPARAM)root);
2867 ok(ret, "Unexpected ret value %d\n", ret);
2868 get_item_names_string(hwnd, NULL, buff);
2869 ok(!strcmp(buff, sorted_order), "Wrong sorted order %s, expected %s\n", buff, sorted_order);
2871 /* case insensitive comparison */
2872 SendMessageA(hwnd, TVM_DELETEITEM, 0, 0);
2874 ins.hParent = TVI_ROOT;
2875 ins.hInsertAfter = TVI_ROOT;
2876 U(ins).item.mask = TVIF_TEXT;
2877 U(ins).item.pszText = (char *)"root";
2878 root = (HTREEITEM)SendMessageA(hwnd, TVM_INSERTITEMA, 0, (LPARAM)&ins);
2879 ok(root != NULL, "Expected root node\n");
2881 ins.hParent = root;
2882 ins.hInsertAfter = TVI_LAST;
2883 U(ins).item.pszText = (char *)"I1";
2884 SendMessageA(hwnd, TVM_INSERTITEMA, 0, (LPARAM)&ins);
2886 ins.hParent = root;
2887 ins.hInsertAfter = TVI_LAST;
2888 U(ins).item.pszText = (char *)"i1";
2889 SendMessageA(hwnd, TVM_INSERTITEMA, 0, (LPARAM)&ins);
2891 ret = SendMessageA(hwnd, TVM_SORTCHILDREN, TRUE, (LPARAM)root);
2892 ok(ret, "Unexpected ret value %d\n", ret);
2893 get_item_names_string(hwnd, NULL, buff);
2894 ok(!strcmp(buff, "rootI1i1"), "Wrong sorted order %s\n", buff);
2896 DestroyWindow(hwnd);
2899 static void test_right_click(void)
2901 HWND hTree;
2902 HTREEITEM selected;
2903 RECT rc;
2904 LRESULT result;
2905 POINT pt, orig_pos;
2907 hTree = create_treeview_control(0);
2908 fill_tree(hTree);
2910 SendMessageA(hTree, TVM_ENSUREVISIBLE, 0, (LPARAM)hChild);
2911 SendMessageA(hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hChild);
2912 selected = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_CARET, 0);
2913 ok(selected == hChild, "child item not selected\n");
2915 *(HTREEITEM *)&rc = hRoot;
2916 result = SendMessageA(hTree, TVM_GETITEMRECT, TRUE, (LPARAM)&rc);
2917 ok(result, "TVM_GETITEMRECT failed\n");
2919 flush_events();
2921 pt.x = (rc.left + rc.right) / 2;
2922 pt.y = (rc.top + rc.bottom) / 2;
2923 ClientToScreen(hMainWnd, &pt);
2924 GetCursorPos(&orig_pos);
2925 SetCursorPos(pt.x, pt.y);
2927 flush_events();
2928 flush_sequences(sequences, NUM_MSG_SEQUENCES);
2930 PostMessageA(hTree, WM_RBUTTONDOWN, MK_RBUTTON, MAKELPARAM(pt.x, pt.y));
2931 PostMessageA(hTree, WM_RBUTTONUP, 0, MAKELPARAM(pt.x, pt.y));
2933 flush_events();
2935 ok_sequence(sequences, TREEVIEW_SEQ_INDEX, test_right_click_seq, "right click sequence", FALSE);
2936 ok_sequence(sequences, PARENT_SEQ_INDEX, parent_right_click_seq, "parent right click sequence", FALSE);
2938 selected = (HTREEITEM)SendMessageA(hTree, TVM_GETNEXTITEM, TVGN_CARET, 0);
2939 ok(selected == hChild, "child item should still be selected\n");
2941 SetCursorPos(orig_pos.x, orig_pos.y);
2942 DestroyWindow(hTree);
2945 static void init_functions(void)
2947 HMODULE hComCtl32 = LoadLibraryA("comctl32.dll");
2949 #define X(f) p##f = (void*)GetProcAddress(hComCtl32, #f);
2950 X(InitCommonControlsEx);
2951 #undef X
2954 START_TEST(treeview)
2956 INITCOMMONCONTROLSEX iccex;
2957 ULONG_PTR ctx_cookie;
2958 HANDLE hCtx;
2959 WNDCLASSA wc;
2961 init_functions();
2963 iccex.dwSize = sizeof(iccex);
2964 iccex.dwICC = ICC_TREEVIEW_CLASSES;
2965 pInitCommonControlsEx(&iccex);
2967 init_msg_sequences(sequences, NUM_MSG_SEQUENCES);
2968 init_msg_sequences(item_sequence, 1);
2970 wc.style = CS_HREDRAW | CS_VREDRAW;
2971 wc.cbClsExtra = 0;
2972 wc.cbWndExtra = 0;
2973 wc.hInstance = GetModuleHandleA(NULL);
2974 wc.hIcon = NULL;
2975 wc.hCursor = LoadCursorA(NULL, (LPCSTR)IDC_IBEAM);
2976 wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
2977 wc.lpszMenuName = NULL;
2978 wc.lpszClassName = "MyTestWnd";
2979 wc.lpfnWndProc = parent_wnd_proc;
2980 RegisterClassA(&wc);
2982 hMainWnd = CreateWindowExA(0, "MyTestWnd", "Blah", WS_OVERLAPPEDWINDOW,
2983 CW_USEDEFAULT, CW_USEDEFAULT, 130, 105, NULL, NULL, GetModuleHandleA(NULL), 0);
2985 ok(hMainWnd != NULL, "Failed to create parent window. Tests aborted.\n");
2986 if (!hMainWnd) return;
2988 test_fillroot();
2989 test_select();
2990 test_getitemtext();
2991 test_focus();
2992 test_get_set_bkcolor();
2993 test_get_set_imagelist();
2994 test_get_set_indent();
2995 test_get_set_insertmark();
2996 test_get_set_item();
2997 test_get_set_itemheight();
2998 test_get_set_scrolltime();
2999 test_get_set_textcolor();
3000 test_get_linecolor();
3001 test_get_insertmarkcolor();
3002 test_get_set_tooltips();
3003 test_get_set_unicodeformat();
3004 test_callback();
3005 test_expandinvisible();
3006 test_itemedit();
3007 test_treeview_classinfo();
3008 test_expandnotify();
3009 test_TVS_SINGLEEXPAND();
3010 test_WM_PAINT();
3011 test_delete_items();
3012 test_cchildren();
3013 test_htreeitem_layout(FALSE);
3014 test_TVS_CHECKBOXES();
3015 test_TVM_GETNEXTITEM();
3016 test_TVM_HITTEST();
3017 test_WM_GETDLGCODE();
3018 test_customdraw();
3019 test_WM_KEYDOWN();
3020 test_TVS_FULLROWSELECT();
3021 test_TVM_SORTCHILDREN();
3022 test_right_click();
3024 if (!load_v6_module(&ctx_cookie, &hCtx))
3026 DestroyWindow(hMainWnd);
3027 return;
3030 /* comctl32 version 6 tests start here */
3031 g_v6 = TRUE;
3033 test_fillroot();
3034 test_getitemtext();
3035 test_get_set_insertmark();
3036 test_get_set_item();
3037 test_get_set_scrolltime();
3038 test_get_set_textcolor();
3039 test_get_linecolor();
3040 test_get_insertmarkcolor();
3041 test_expandedimage();
3042 test_get_set_tooltips();
3043 test_get_set_unicodeformat();
3044 test_expandinvisible();
3045 test_expand();
3046 test_itemedit();
3047 test_treeview_classinfo();
3048 test_delete_items();
3049 test_cchildren();
3050 test_htreeitem_layout(TRUE);
3051 test_TVM_GETNEXTITEM();
3052 test_TVM_HITTEST();
3053 test_WM_GETDLGCODE();
3054 test_customdraw();
3055 test_WM_KEYDOWN();
3056 test_TVS_FULLROWSELECT();
3057 test_TVM_SORTCHILDREN();
3059 unload_v6_module(ctx_cookie, hCtx);