comctl32: A couple fixes for tab icon offsets.
[wine] / dlls / comctl32 / tests / treeview.c
1 /* Unit tests for treeview.
2  *
3  * Copyright 2005 Krzysztof Foltman
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include <assert.h>
21 #include <stdarg.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "winuser.h"
27 #include "winnls.h"
28 #include "winreg.h"
29 #include "commctrl.h" 
30
31 #include "wine/test.h"
32  
33 static HWND hMainWnd;
34
35 static HWND hTree;
36 static HTREEITEM hRoot, hChild;
37
38 static int pos = 0;
39 static char sequence[256];
40
41 static void Clear(void)
42 {
43     pos = 0;
44     sequence[0] = '\0';
45 }
46
47 static void AddItem(char ch)
48 {
49     sequence[pos++] = ch;
50     sequence[pos] = '\0';
51 }
52
53 static void IdentifyItem(HTREEITEM hItem)
54 {
55     if (hItem == hRoot) {
56         AddItem('R');
57         return;
58     }
59     if (hItem == hChild) {
60         AddItem('C');
61         return;
62     }
63     if (hItem == NULL) {
64         AddItem('n');
65         return;
66     }
67     AddItem('?');
68 }
69
70 static void FillRoot(void)
71 {
72     TVINSERTSTRUCTA ins;
73
74     Clear();
75     AddItem('A');    
76     ins.hParent = TVI_ROOT;
77     ins.hInsertAfter = TVI_ROOT;
78     U(ins).item.mask = TVIF_TEXT;
79     U(ins).item.pszText = "Root";
80     hRoot = TreeView_InsertItem(hTree, &ins);
81     assert(hRoot);
82   
83     AddItem('B');
84     ins.hParent = hRoot;
85     ins.hInsertAfter = TVI_FIRST;
86     U(ins).item.mask = TVIF_TEXT;
87     U(ins).item.pszText = "Child";
88     hChild = TreeView_InsertItem(hTree, &ins);
89     assert(hChild);
90     AddItem('.');
91     
92     ok(!strcmp(sequence, "AB."), "Item creation\n");
93 }
94
95 static void DoTest1(void)
96 {
97     BOOL r;
98     r = TreeView_SelectItem(hTree, NULL);
99     Clear();
100     AddItem('1');
101     r = TreeView_SelectItem(hTree, hRoot);
102     AddItem('2');
103     r = TreeView_SelectItem(hTree, hRoot);
104     AddItem('3');
105     r = TreeView_SelectItem(hTree, NULL);
106     AddItem('4');
107     r = TreeView_SelectItem(hTree, NULL);
108     AddItem('5');
109     r = TreeView_SelectItem(hTree, hRoot);
110     AddItem('.');
111     ok(!strcmp(sequence, "1(nR)nR23(Rn)Rn45(nR)nR."), "root-none select test\n");
112 }
113
114 static void DoTest2(void)
115 {
116     BOOL r;
117     r = TreeView_SelectItem(hTree, NULL);
118     Clear();
119     AddItem('1');
120     r = TreeView_SelectItem(hTree, hRoot);
121     AddItem('2');
122     r = TreeView_SelectItem(hTree, hRoot);
123     AddItem('3');
124     r = TreeView_SelectItem(hTree, hChild);
125     AddItem('4');
126     r = TreeView_SelectItem(hTree, hChild);
127     AddItem('5');
128     r = TreeView_SelectItem(hTree, hRoot);
129     AddItem('.');
130     ok(!strcmp(sequence, "1(nR)nR23(RC)RC45(CR)CR."), "root-child select test\n");
131 }
132
133 LRESULT CALLBACK MyWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
134 {
135     switch(msg) {
136                   
137     case WM_CREATE:
138     {
139         hTree = CreateWindowExA(WS_EX_CLIENTEDGE, WC_TREEVIEWA, NULL, WS_CHILD|WS_VISIBLE|
140             TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS, 
141             0, 0, 300, 50, hWnd, (HMENU)100, GetModuleHandleA(0), 0);
142     
143         SetFocus(hTree);
144         return 0;
145     }
146     case WM_NOTIFY:
147     {
148         NMHDR *pHdr = (NMHDR *)lParam;
149     
150         if (pHdr->idFrom == 100) {
151             NMTREEVIEWA *pTreeView = (LPNMTREEVIEWA) lParam;
152             switch(pHdr->code) {
153             case TVN_SELCHANGINGA:
154                 AddItem('(');
155                 IdentifyItem(pTreeView->itemOld.hItem);
156                 IdentifyItem(pTreeView->itemNew.hItem);
157                 return 0;
158             case TVN_SELCHANGEDA:
159                 AddItem(')');
160                 IdentifyItem(pTreeView->itemOld.hItem);
161                 IdentifyItem(pTreeView->itemNew.hItem);
162                 return 0;
163             }
164         }
165         return 0;
166     }
167   
168     case WM_SIZE:
169         MoveWindow(hTree, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
170         break;
171       
172     case WM_DESTROY:
173         PostQuitMessage(0);
174         break;
175   
176     default:
177         return DefWindowProcA(hWnd, msg, wParam, lParam);
178     }
179     return 0L;
180 }
181
182 START_TEST(treeview)
183 {
184     WNDCLASSA wc;
185     MSG msg;
186     INITCOMMONCONTROLSEX icex;
187     RECT rc;
188   
189     icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
190     icex.dwICC   = ICC_TREEVIEW_CLASSES;
191     InitCommonControlsEx(&icex);
192   
193     wc.style = CS_HREDRAW | CS_VREDRAW;
194     wc.cbClsExtra = 0;
195     wc.cbWndExtra = 0;
196     wc.hInstance = GetModuleHandleA(NULL);
197     wc.hIcon = NULL;
198     wc.hCursor = LoadCursorA(NULL, MAKEINTRESOURCEA(IDC_IBEAM));
199     wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
200     wc.lpszMenuName = NULL;
201     wc.lpszClassName = "MyTestWnd";
202     wc.lpfnWndProc = MyWndProc;
203     RegisterClassA(&wc);
204     
205     
206     hMainWnd = CreateWindowExA(0, "MyTestWnd", "Blah", WS_OVERLAPPEDWINDOW, 
207       CW_USEDEFAULT, CW_USEDEFAULT, 680, 260, NULL, NULL, GetModuleHandleA(NULL), 0);
208     GetClientRect(hMainWnd, &rc);
209
210     FillRoot();
211     DoTest1();
212     DoTest2();
213
214     PostMessageA(hMainWnd, WM_CLOSE, 0, 0);
215     while(GetMessageA(&msg,0,0,0)) {
216         TranslateMessage(&msg);
217         DispatchMessageA(&msg);
218     }
219 }