4 * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #define WIN32_LEAN_AND_MEAN /* Exclude rarely-used stuff from Windows headers */
23 #define NONAMELESSUNION
24 #define NONAMELESSSTRUCT
31 #include <wine/debug.h>
35 WINE_DEFAULT_DEBUG_CHANNEL(regedit);
37 /* Global variables and constants */
38 /* Image_Open, Image_Closed, and Image_Root - integer variables for indexes of the images. */
39 /* CX_BITMAP and CY_BITMAP - width and height of an icon. */
40 /* NUM_BITMAPS - number of bitmaps to add to the image list. */
45 static LPTSTR pathBuffer;
51 static BOOL get_item_path(HWND hwndTV, HTREEITEM hItem, HKEY* phKey, LPTSTR* pKeyPath, int* pPathLen, int* pMaxLen)
57 item.mask = TVIF_PARAM;
59 if (!TreeView_GetItem(hwndTV, &item)) return FALSE;
62 /* found root key with valid key value */
63 *phKey = (HKEY)item.lParam;
67 if(!get_item_path(hwndTV, TreeView_GetParent(hwndTV, hItem), phKey, pKeyPath, pPathLen, pMaxLen)) return FALSE;
69 (*pKeyPath)[*pPathLen] = _T('\\');
74 item.mask = TVIF_TEXT;
76 item.pszText = *pKeyPath + *pPathLen;
77 item.cchTextMax = maxLen = *pMaxLen - *pPathLen;
78 if (!TreeView_GetItem(hwndTV, &item)) return FALSE;
79 len = _tcslen(item.pszText);
80 if (len < maxLen - 1) {
84 newStr = HeapReAlloc(GetProcessHeap(), 0, *pKeyPath, *pMaxLen * 2);
85 if (!newStr) return FALSE;
93 LPCTSTR GetItemPath(HWND hwndTV, HTREEITEM hItem, HKEY* phRootKey)
95 int pathLen = 0, maxLen;
97 if (!pathBuffer) pathBuffer = HeapAlloc(GetProcessHeap(), 0, 1024);
98 if (!pathBuffer) return NULL;
100 maxLen = HeapSize(GetProcessHeap(), 0, pathBuffer);
101 if (maxLen == (SIZE_T) - 1) return NULL;
102 if (!hItem) hItem = TreeView_GetSelection(hwndTV);
103 if (!hItem) return NULL;
104 if (!get_item_path(hwndTV, hItem, phRootKey, &pathBuffer, &pathLen, &maxLen)) return NULL;
108 BOOL DeleteNode(HWND hwndTV, HTREEITEM hItem)
110 if (!hItem) hItem = TreeView_GetSelection(hwndTV);
111 if (!hItem) return FALSE;
112 return TreeView_DeleteItem(hwndTV, hItem);
115 /* Add an entry to the tree. Only give hKey for root nodes (HKEY_ constants) */
116 static HTREEITEM AddEntryToTree(HWND hwndTV, HTREEITEM hParent, LPTSTR label, HKEY hKey, DWORD dwChildren)
119 TVINSERTSTRUCT tvins;
122 if (RegQueryInfoKey(hKey, 0, 0, 0, &dwChildren, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
127 tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
129 tvi.cchTextMax = lstrlen(tvi.pszText);
130 tvi.iImage = Image_Closed;
131 tvi.iSelectedImage = Image_Open;
132 tvi.cChildren = dwChildren;
133 tvi.lParam = (LPARAM)hKey;
135 tvins.hInsertAfter = (HTREEITEM)(hKey ? TVI_LAST : TVI_SORT);
136 tvins.hParent = hParent;
137 return TreeView_InsertItem(hwndTV, &tvins);
140 BOOL RefreshTreeItem(HWND hwndTV, HTREEITEM hItem)
142 HKEY hRoot, hKey, hSubKey;
145 DWORD dwCount, dwIndex, dwMaxSubKeyLen;
149 KeyPath = GetItemPath(hwndTV, hItem, &hRoot);
152 if (RegOpenKeyEx(hRoot, KeyPath, 0, KEY_READ, &hKey) != ERROR_SUCCESS) {
153 WINE_TRACE("RegOpenKeyEx failed, \"%s\" was probably removed.\n", KeyPath);
160 if (RegQueryInfoKey(hKey, 0, 0, 0, &dwCount, &dwMaxSubKeyLen, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
164 /* Set the number of children again */
165 tvItem.mask = TVIF_CHILDREN;
166 tvItem.hItem = hItem;
167 tvItem.cChildren = dwCount;
168 if (!TreeView_SetItem(hwndTV, &tvItem)) {
172 /* We don't have to bother with the rest if it's not expanded. */
173 if (TreeView_GetItemState(hwndTV, hItem, TVIS_EXPANDED) == 0) {
178 dwMaxSubKeyLen++; /* account for the \0 terminator */
179 if (!(Name = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(TCHAR)))) {
182 tvItem.cchTextMax = dwMaxSubKeyLen;
183 if (!(tvItem.pszText = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(TCHAR)))) {
187 /* Now go through all the children in the registry, and check if any have to be added. */
188 for (dwIndex = 0; dwIndex < dwCount; dwIndex++) {
189 DWORD cName = dwMaxSubKeyLen, dwSubCount;
193 if (RegEnumKeyEx(hKey, dwIndex, Name, &cName, 0, 0, 0, NULL) != ERROR_SUCCESS) {
197 /* Find the number of children of the node. */
199 if (RegOpenKeyEx(hKey, Name, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
200 if (RegQueryInfoKey(hSubKey, 0, 0, 0, &dwSubCount, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
203 RegCloseKey(hSubKey);
206 /* Check if the node is already in there. */
207 for (childItem = TreeView_GetChild(hwndTV, hItem); childItem;
208 childItem = TreeView_GetNextSibling(hwndTV, childItem)) {
209 tvItem.mask = TVIF_TEXT;
210 tvItem.hItem = childItem;
211 if (!TreeView_GetItem(hwndTV, &tvItem)) {
215 if (!strcmp(tvItem.pszText, Name)) {
221 if (found == FALSE) {
222 WINE_TRACE("New subkey %s\n", Name);
223 AddEntryToTree(hwndTV, hItem, Name, NULL, dwSubCount);
226 HeapFree(GetProcessHeap(), 0, Name);
227 HeapFree(GetProcessHeap(), 0, tvItem.pszText);
230 /* Now go through all the children in the tree, and check if any have to be removed. */
231 childItem = TreeView_GetChild(hwndTV, hItem);
233 HTREEITEM nextItem = TreeView_GetNextSibling(hwndTV, childItem);
234 if (RefreshTreeItem(hwndTV, childItem) == FALSE) {
235 TreeView_DeleteItem(hwndTV, childItem);
237 childItem = nextItem;
243 BOOL RefreshTreeView(HWND hwndTV)
246 HTREEITEM hSelectedItem;
250 hSelectedItem = TreeView_GetSelection(hwndTV);
251 hcursorOld = SetCursor(LoadCursor(NULL, IDC_WAIT));
252 SendMessage(hwndTV, WM_SETREDRAW, FALSE, 0);
254 hItem = TreeView_GetChild(hwndTV, TreeView_GetRoot(hwndTV));
256 RefreshTreeItem(hwndTV, hItem);
257 hItem = TreeView_GetNextSibling(hwndTV, hItem);
260 SendMessage(hwndTV, WM_SETREDRAW, TRUE, 0);
261 SetCursor(hcursorOld);
263 /* We reselect the currently selected node, this will prompt a refresh of the listview. */
264 TreeView_SelectItem(hwndTV, hSelectedItem);
268 HTREEITEM InsertNode(HWND hwndTV, HTREEITEM hItem, LPTSTR name)
270 TCHAR buf[MAX_NEW_KEY_LEN];
271 HTREEITEM hNewItem = 0;
274 if (!hItem) hItem = TreeView_GetSelection(hwndTV);
275 if (!hItem) return FALSE;
276 if (TreeView_GetItemState(hwndTV, hItem, TVIS_EXPANDEDONCE)) {
277 hNewItem = AddEntryToTree(hwndTV, hItem, name, 0, 0);
279 item.mask = TVIF_CHILDREN | TVIF_HANDLE;
281 if (!TreeView_GetItem(hwndTV, &item)) return FALSE;
283 if (!TreeView_SetItem(hwndTV, &item)) return FALSE;
285 TreeView_Expand(hwndTV, hItem, TVE_EXPAND);
287 for(hNewItem = TreeView_GetChild(hwndTV, hItem); hNewItem; hNewItem = TreeView_GetNextSibling(hwndTV, hNewItem)) {
288 item.mask = TVIF_HANDLE | TVIF_TEXT;
289 item.hItem = hNewItem;
291 item.cchTextMax = COUNT_OF(buf);
292 if (!TreeView_GetItem(hwndTV, &item)) continue;
293 if (lstrcmp(name, item.pszText) == 0) break;
296 if (hNewItem) TreeView_SelectItem(hwndTV, hNewItem);
301 HWND StartKeyRename(HWND hwndTV)
305 if(!(hItem = TreeView_GetSelection(hwndTV))) return 0;
306 return TreeView_EditLabel(hwndTV, hItem);
309 static BOOL InitTreeViewItems(HWND hwndTV, LPTSTR pHostName)
312 TVINSERTSTRUCT tvins;
315 tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
316 /* Set the text of the item. */
317 tvi.pszText = pHostName;
318 tvi.cchTextMax = lstrlen(tvi.pszText);
319 /* Assume the item is not a parent item, so give it an image. */
320 tvi.iImage = Image_Root;
321 tvi.iSelectedImage = Image_Root;
323 /* Save the heading level in the item's application-defined data area. */
324 tvi.lParam = (LPARAM)NULL;
326 tvins.hInsertAfter = (HTREEITEM)TVI_FIRST;
327 tvins.hParent = TVI_ROOT;
328 /* Add the item to the tree view control. */
329 if (!(hRoot = TreeView_InsertItem(hwndTV, &tvins))) return FALSE;
331 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_CLASSES_ROOT"), HKEY_CLASSES_ROOT, 1)) return FALSE;
332 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_CURRENT_USER"), HKEY_CURRENT_USER, 1)) return FALSE;
333 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_LOCAL_MACHINE"), HKEY_LOCAL_MACHINE, 1)) return FALSE;
334 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_USERS"), HKEY_USERS, 1)) return FALSE;
335 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_CURRENT_CONFIG"), HKEY_CURRENT_CONFIG, 1)) return FALSE;
336 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_DYN_DATA"), HKEY_DYN_DATA, 1)) return FALSE;
338 /* expand and select host name */
339 TreeView_Expand(hwndTV, hRoot, TVE_EXPAND);
340 TreeView_Select(hwndTV, hRoot, TVGN_CARET);
346 * InitTreeViewImageLists - creates an image list, adds three bitmaps
347 * to it, and associates the image list with a tree view control.
348 * Returns TRUE if successful, or FALSE otherwise.
349 * hwndTV - handle to the tree view control.
351 static BOOL InitTreeViewImageLists(HWND hwndTV)
353 HIMAGELIST himl; /* handle to image list */
354 HICON hico; /* handle to icon */
356 /* Create the image list. */
357 if ((himl = ImageList_Create(CX_ICON, CY_ICON,
358 ILC_MASK, 0, NUM_ICONS)) == NULL)
361 /* Add the open file, closed file, and document bitmaps. */
362 hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_OPEN_FILE));
363 Image_Open = ImageList_AddIcon(himl, hico);
365 hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_CLOSED_FILE));
366 Image_Closed = ImageList_AddIcon(himl, hico);
368 hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_ROOT));
369 Image_Root = ImageList_AddIcon(himl, hico);
371 /* Fail if not all of the images were added. */
372 if (ImageList_GetImageCount(himl) < NUM_ICONS)
377 /* Associate the image list with the tree view control. */
378 TreeView_SetImageList(hwndTV, himl, TVSIL_NORMAL);
383 BOOL OnTreeExpanding(HWND hwndTV, NMTREEVIEW* pnmtv)
385 DWORD dwCount, dwIndex, dwMaxSubKeyLen;
386 HKEY hRoot, hNewKey, hKey;
392 static int expanding;
393 if (expanding) return FALSE;
394 if (pnmtv->itemNew.state & TVIS_EXPANDEDONCE ) {
398 hcursorOld = SetCursor(LoadCursor(NULL, IDC_WAIT));
399 SendMessage(hwndTV, WM_SETREDRAW, FALSE, 0);
401 keyPath = GetItemPath(hwndTV, pnmtv->itemNew.hItem, &hRoot);
402 if (!keyPath) goto done;
405 errCode = RegOpenKeyEx(hRoot, keyPath, 0, KEY_READ, &hNewKey);
406 if (errCode != ERROR_SUCCESS) goto done;
411 errCode = RegQueryInfoKey(hNewKey, 0, 0, 0, &dwCount, &dwMaxSubKeyLen, 0, 0, 0, 0, 0, 0);
412 if (errCode != ERROR_SUCCESS) goto done;
413 dwMaxSubKeyLen++; /* account for the \0 terminator */
414 Name = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(TCHAR));
415 if (!Name) goto done;
417 for (dwIndex = 0; dwIndex < dwCount; dwIndex++) {
418 DWORD cName = dwMaxSubKeyLen, dwSubCount;
420 errCode = RegEnumKeyEx(hNewKey, dwIndex, Name, &cName, 0, 0, 0, 0);
421 if (errCode != ERROR_SUCCESS) continue;
422 errCode = RegOpenKeyEx(hNewKey, Name, 0, KEY_QUERY_VALUE, &hKey);
423 if (errCode == ERROR_SUCCESS) {
424 errCode = RegQueryInfoKey(hKey, 0, 0, 0, &dwSubCount, 0, 0, 0, 0, 0, 0, 0);
427 if (errCode != ERROR_SUCCESS) dwSubCount = 0;
428 AddEntryToTree(hwndTV, pnmtv->itemNew.hItem, Name, NULL, dwSubCount);
430 RegCloseKey(hNewKey);
431 HeapFree(GetProcessHeap(), 0, Name);
434 SendMessage(hwndTV, WM_SETREDRAW, TRUE, 0);
435 SetCursor(hcursorOld);
443 * CreateTreeView - creates a tree view control.
444 * Returns the handle to the new control if successful, or NULL otherwise.
445 * hwndParent - handle to the control's parent window.
447 HWND CreateTreeView(HWND hwndParent, LPTSTR pHostName, int id)
452 /* Get the dimensions of the parent window's client area, and create the tree view control. */
453 GetClientRect(hwndParent, &rcClient);
454 hwndTV = CreateWindowEx(WS_EX_CLIENTEDGE, WC_TREEVIEW, _T("Tree View"),
455 WS_VISIBLE | WS_CHILD | WS_TABSTOP | TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT,
456 0, 0, rcClient.right, rcClient.bottom,
457 hwndParent, (HMENU)id, hInst, NULL);
458 /* Initialize the image list, and add items to the control. */
459 if (!InitTreeViewImageLists(hwndTV) || !InitTreeViewItems(hwndTV, pHostName)) {
460 DestroyWindow(hwndTV);