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
30 #include <wine/debug.h>
36 WINE_DEFAULT_DEBUG_CHANNEL(regedit);
38 /* Global variables and constants */
39 /* Image_Open, Image_Closed, and Image_Root - integer variables for indexes of the images. */
40 /* CX_BITMAP and CY_BITMAP - width and height of an icon. */
41 /* NUM_BITMAPS - number of bitmaps to add to the image list. */
50 static BOOL UpdateExpandingTree(HWND hwndTV, HTREEITEM hItem, int state);
52 static BOOL get_item_path(HWND hwndTV, HTREEITEM hItem, HKEY* phKey, LPTSTR* pKeyPath, int* pPathLen, int* pMaxLen)
58 item.mask = TVIF_PARAM;
60 if (!TreeView_GetItem(hwndTV, &item)) return FALSE;
63 /* found root key with valid key value */
64 *phKey = (HKEY)item.lParam;
68 if(!get_item_path(hwndTV, TreeView_GetParent(hwndTV, hItem), phKey, pKeyPath, pPathLen, pMaxLen)) return FALSE;
70 (*pKeyPath)[*pPathLen] = _T('\\');
75 item.mask = TVIF_TEXT;
77 item.pszText = *pKeyPath + *pPathLen;
78 item.cchTextMax = maxLen = *pMaxLen - *pPathLen;
79 if (!TreeView_GetItem(hwndTV, &item)) return FALSE;
80 len = _tcslen(item.pszText);
81 if (len < maxLen - 1) {
85 newStr = HeapReAlloc(GetProcessHeap(), 0, *pKeyPath, *pMaxLen * 2);
86 if (!newStr) return FALSE;
94 LPTSTR GetItemPath(HWND hwndTV, HTREEITEM hItem, HKEY* phRootKey)
96 int pathLen = 0, maxLen;
99 pathBuffer = HeapAlloc(GetProcessHeap(), 0, 1024);
100 if (!pathBuffer) return NULL;
102 maxLen = HeapSize(GetProcessHeap(), 0, pathBuffer);
103 if (maxLen == (SIZE_T) - 1) return NULL;
104 if (!hItem) hItem = TreeView_GetSelection(hwndTV);
105 if (!hItem) return NULL;
106 if (!get_item_path(hwndTV, hItem, phRootKey, &pathBuffer, &pathLen, &maxLen)) return NULL;
110 static LPTSTR get_path_component(LPCTSTR *lplpKeyName) {
111 LPCTSTR lpPos = *lplpKeyName;
112 LPTSTR lpResult = NULL;
116 while(*lpPos && *lpPos != '\\')
118 if (*lpPos && lpPos == *lplpKeyName)
120 len = (lpPos+1-(*lplpKeyName)) * sizeof(TCHAR);
121 lpResult = HeapAlloc(GetProcessHeap(), 0, len);
122 if (!lpResult) /* that would be very odd */
124 memcpy(lpResult, *lplpKeyName, len-1);
125 lpResult[len-1] = '\0';
126 *lplpKeyName = *lpPos ? lpPos+1 : NULL;
130 HTREEITEM FindPathInTree(HWND hwndTV, LPCTSTR lpKeyName) {
132 TCHAR buf[261]; /* tree view has 260 character limitation on item name */
133 HTREEITEM hItem, hOldItem;
136 hItem = TreeView_GetRoot(hwndTV);
137 TreeView_Expand(hwndTV, hItem, TVE_EXPAND);
138 hItem = TreeView_GetChild(hwndTV, hItem);
141 LPTSTR lpItemName = get_path_component(&lpKeyName);
144 tvi.mask = TVIF_TEXT | TVIF_HANDLE;
147 tvi.cchTextMax = 260;
148 TreeView_GetItem(hwndTV, &tvi);
149 if (!_tcsicmp(tvi.pszText, lpItemName)) {
150 TreeView_Expand(hwndTV, hItem, TVE_EXPAND);
154 hItem = TreeView_GetChild(hwndTV, hItem);
157 hItem = TreeView_GetNextSibling(hwndTV, hItem);
159 HeapFree(GetProcessHeap(), 0, lpItemName);
168 BOOL DeleteNode(HWND hwndTV, HTREEITEM hItem)
170 if (!hItem) hItem = TreeView_GetSelection(hwndTV);
171 if (!hItem) return FALSE;
172 return TreeView_DeleteItem(hwndTV, hItem);
175 /* Add an entry to the tree. Only give hKey for root nodes (HKEY_ constants) */
176 static HTREEITEM AddEntryToTree(HWND hwndTV, HTREEITEM hParent, LPTSTR label, HKEY hKey, DWORD dwChildren)
179 TVINSERTSTRUCT tvins;
182 if (RegQueryInfoKey(hKey, 0, 0, 0, &dwChildren, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
187 tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
189 tvi.cchTextMax = lstrlen(tvi.pszText);
190 tvi.iImage = Image_Closed;
191 tvi.iSelectedImage = Image_Open;
192 tvi.cChildren = dwChildren;
193 tvi.lParam = (LPARAM)hKey;
195 tvins.hInsertAfter = (HTREEITEM)(hKey ? TVI_LAST : TVI_SORT);
196 tvins.hParent = hParent;
197 return TreeView_InsertItem(hwndTV, &tvins);
200 static BOOL match_string(LPCTSTR sstring1, LPCTSTR sstring2, int mode)
202 if (mode & SEARCH_WHOLE)
203 return !stricmp(sstring1, sstring2);
205 return NULL != StrStrI(sstring1, sstring2);
208 static BOOL match_item(HWND hwndTV, HTREEITEM hItem, LPCTSTR sstring, int mode, int *row)
211 TCHAR keyname[KEY_MAX_LEN];
212 item.mask = TVIF_TEXT;
214 item.pszText = keyname;
215 item.cchTextMax = KEY_MAX_LEN;
216 if (!TreeView_GetItem(hwndTV, &item)) return FALSE;
217 if ((mode & SEARCH_KEYS) && match_string(keyname, sstring, mode)) {
222 if (mode & (SEARCH_VALUES | SEARCH_CONTENT)) {
224 TCHAR valName[KEY_MAX_LEN], *KeyPath;
228 KeyPath = GetItemPath(hwndTV, hItem, &hRoot);
230 if (!KeyPath || !hRoot)
233 if (RegOpenKeyEx(hRoot, KeyPath, 0, KEY_READ, &hKey) != ERROR_SUCCESS) {
234 HeapFree(GetProcessHeap(), 0, KeyPath);
238 HeapFree(GetProcessHeap(), 0, KeyPath);
239 lenName = KEY_MAX_LEN;
241 /* RegEnumValue won't return empty default value, so fake it when dealing with *row,
242 which corresponds to list view rows, not value ids */
243 if (ERROR_SUCCESS == RegEnumValue(hKey, 0, valName, &lenName, NULL, NULL, NULL, NULL) && *valName)
249 DWORD lenValue = 0, type = 0;
250 lenName = KEY_MAX_LEN;
252 if (ERROR_SUCCESS != RegEnumValue(hKey,
253 i, valName, &lenName, NULL, &type, NULL, &lenValue))
256 if (mode & SEARCH_VALUES) {
257 if (match_string(valName, sstring, mode)) {
264 if ((mode & SEARCH_CONTENT) && (type == REG_EXPAND_SZ || type == REG_SZ)) {
266 buffer = HeapAlloc(GetProcessHeap(), 0, lenValue);
267 RegEnumValue(hKey, i, valName, &lenName, NULL, &type, (LPBYTE)buffer, &lenValue);
268 if (match_string(buffer, sstring, mode)) {
269 HeapFree(GetProcessHeap(), 0, buffer);
274 HeapFree(GetProcessHeap(), 0, buffer);
284 HTREEITEM FindNext(HWND hwndTV, HTREEITEM hItem, LPCTSTR sstring, int mode, int *row)
286 HTREEITEM hTry, hLast;
290 if (match_item(hwndTV, hLast, sstring, mode & ~SEARCH_KEYS, row)) {
296 /* first look in subtree */
297 /* no children? maybe we haven't loaded them yet? */
298 if (!TreeView_GetChild(hwndTV, hLast)) {
299 UpdateExpandingTree(hwndTV, hLast, TreeView_GetItemState(hwndTV, hLast, -1));
301 hTry = TreeView_GetChild(hwndTV, hLast);
303 if (match_item(hwndTV, hTry, sstring, mode, row))
308 /* no more children, maybe there are any siblings? */
309 hTry = TreeView_GetNextSibling(hwndTV, hLast);
311 if (match_item(hwndTV, hTry, sstring, mode, row))
316 /* no more siblings, look at the next siblings in parent(s) */
317 hLast = TreeView_GetParent(hwndTV, hLast);
320 while (hLast && (hTry = TreeView_GetNextSibling(hwndTV, hLast)) == NULL) {
321 hLast = TreeView_GetParent(hwndTV, hLast);
323 if (match_item(hwndTV, hTry, sstring, mode, row))
330 static BOOL RefreshTreeItem(HWND hwndTV, HTREEITEM hItem)
332 HKEY hRoot, hKey, hSubKey;
335 DWORD dwCount, dwIndex, dwMaxSubKeyLen;
340 KeyPath = GetItemPath(hwndTV, hItem, &hRoot);
342 if (!KeyPath || !hRoot)
346 if (RegOpenKeyEx(hRoot, KeyPath, 0, KEY_READ, &hKey) != ERROR_SUCCESS) {
347 WINE_TRACE("RegOpenKeyEx failed, \"%s\" was probably removed.\n", KeyPath);
353 HeapFree(GetProcessHeap(), 0, KeyPath);
355 if (RegQueryInfoKey(hKey, 0, 0, 0, &dwCount, &dwMaxSubKeyLen, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
359 /* Set the number of children again */
360 tvItem.mask = TVIF_CHILDREN;
361 tvItem.hItem = hItem;
362 tvItem.cChildren = dwCount;
363 if (!TreeView_SetItem(hwndTV, &tvItem)) {
367 /* We don't have to bother with the rest if it's not expanded. */
368 if (TreeView_GetItemState(hwndTV, hItem, TVIS_EXPANDED) == 0) {
373 dwMaxSubKeyLen++; /* account for the \0 terminator */
374 if (!(Name = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(TCHAR)))) {
377 tvItem.cchTextMax = dwMaxSubKeyLen;
378 if (!(tvItem.pszText = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(TCHAR)))) {
382 /* Now go through all the children in the registry, and check if any have to be added. */
383 for (dwIndex = 0; dwIndex < dwCount; dwIndex++) {
384 DWORD cName = dwMaxSubKeyLen, dwSubCount;
388 if (RegEnumKeyEx(hKey, dwIndex, Name, &cName, 0, 0, 0, NULL) != ERROR_SUCCESS) {
392 /* Find the number of children of the node. */
394 if (RegOpenKeyEx(hKey, Name, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
395 if (RegQueryInfoKey(hSubKey, 0, 0, 0, &dwSubCount, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
398 RegCloseKey(hSubKey);
401 /* Check if the node is already in there. */
402 for (childItem = TreeView_GetChild(hwndTV, hItem); childItem;
403 childItem = TreeView_GetNextSibling(hwndTV, childItem)) {
404 tvItem.mask = TVIF_TEXT;
405 tvItem.hItem = childItem;
406 if (!TreeView_GetItem(hwndTV, &tvItem)) {
410 if (!stricmp(tvItem.pszText, Name)) {
416 if (found == FALSE) {
417 WINE_TRACE("New subkey %s\n", Name);
418 AddEntryToTree(hwndTV, hItem, Name, NULL, dwSubCount);
421 HeapFree(GetProcessHeap(), 0, Name);
422 HeapFree(GetProcessHeap(), 0, tvItem.pszText);
425 /* Now go through all the children in the tree, and check if any have to be removed. */
426 childItem = TreeView_GetChild(hwndTV, hItem);
428 HTREEITEM nextItem = TreeView_GetNextSibling(hwndTV, childItem);
429 if (RefreshTreeItem(hwndTV, childItem) == FALSE) {
430 TreeView_DeleteItem(hwndTV, childItem);
432 childItem = nextItem;
438 BOOL RefreshTreeView(HWND hwndTV)
441 HTREEITEM hSelectedItem;
445 hSelectedItem = TreeView_GetSelection(hwndTV);
446 hcursorOld = SetCursor(LoadCursor(NULL, IDC_WAIT));
447 SendMessage(hwndTV, WM_SETREDRAW, FALSE, 0);
449 hItem = TreeView_GetChild(hwndTV, TreeView_GetRoot(hwndTV));
451 RefreshTreeItem(hwndTV, hItem);
452 hItem = TreeView_GetNextSibling(hwndTV, hItem);
455 SendMessage(hwndTV, WM_SETREDRAW, TRUE, 0);
456 InvalidateRect(hwndTV, NULL, FALSE);
457 SetCursor(hcursorOld);
459 /* We reselect the currently selected node, this will prompt a refresh of the listview. */
460 TreeView_SelectItem(hwndTV, hSelectedItem);
464 HTREEITEM InsertNode(HWND hwndTV, HTREEITEM hItem, LPTSTR name)
466 TCHAR buf[MAX_NEW_KEY_LEN];
467 HTREEITEM hNewItem = 0;
470 if (!hItem) hItem = TreeView_GetSelection(hwndTV);
471 if (!hItem) return FALSE;
472 if (TreeView_GetItemState(hwndTV, hItem, TVIS_EXPANDEDONCE)) {
473 hNewItem = AddEntryToTree(hwndTV, hItem, name, 0, 0);
475 item.mask = TVIF_CHILDREN | TVIF_HANDLE;
477 if (!TreeView_GetItem(hwndTV, &item)) return FALSE;
479 if (!TreeView_SetItem(hwndTV, &item)) return FALSE;
481 TreeView_Expand(hwndTV, hItem, TVE_EXPAND);
483 for(hNewItem = TreeView_GetChild(hwndTV, hItem); hNewItem; hNewItem = TreeView_GetNextSibling(hwndTV, hNewItem)) {
484 item.mask = TVIF_HANDLE | TVIF_TEXT;
485 item.hItem = hNewItem;
487 item.cchTextMax = COUNT_OF(buf);
488 if (!TreeView_GetItem(hwndTV, &item)) continue;
489 if (lstrcmp(name, item.pszText) == 0) break;
492 if (hNewItem) TreeView_SelectItem(hwndTV, hNewItem);
497 HWND StartKeyRename(HWND hwndTV)
501 if(!(hItem = TreeView_GetSelection(hwndTV))) return 0;
502 return TreeView_EditLabel(hwndTV, hItem);
505 static BOOL InitTreeViewItems(HWND hwndTV, LPTSTR pHostName)
508 TVINSERTSTRUCT tvins;
511 tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
512 /* Set the text of the item. */
513 tvi.pszText = pHostName;
514 tvi.cchTextMax = lstrlen(tvi.pszText);
515 /* Assume the item is not a parent item, so give it an image. */
516 tvi.iImage = Image_Root;
517 tvi.iSelectedImage = Image_Root;
519 /* Save the heading level in the item's application-defined data area. */
520 tvi.lParam = (LPARAM)NULL;
522 tvins.hInsertAfter = (HTREEITEM)TVI_FIRST;
523 tvins.hParent = TVI_ROOT;
524 /* Add the item to the tree view control. */
525 if (!(hRoot = TreeView_InsertItem(hwndTV, &tvins))) return FALSE;
527 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_CLASSES_ROOT"), HKEY_CLASSES_ROOT, 1)) return FALSE;
528 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_CURRENT_USER"), HKEY_CURRENT_USER, 1)) return FALSE;
529 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_LOCAL_MACHINE"), HKEY_LOCAL_MACHINE, 1)) return FALSE;
530 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_USERS"), HKEY_USERS, 1)) return FALSE;
531 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_CURRENT_CONFIG"), HKEY_CURRENT_CONFIG, 1)) return FALSE;
532 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_DYN_DATA"), HKEY_DYN_DATA, 1)) return FALSE;
534 /* expand and select host name */
535 TreeView_Expand(hwndTV, hRoot, TVE_EXPAND);
536 TreeView_Select(hwndTV, hRoot, TVGN_CARET);
542 * InitTreeViewImageLists - creates an image list, adds three bitmaps
543 * to it, and associates the image list with a tree view control.
544 * Returns TRUE if successful, or FALSE otherwise.
545 * hwndTV - handle to the tree view control.
547 static BOOL InitTreeViewImageLists(HWND hwndTV)
549 HIMAGELIST himl; /* handle to image list */
550 HICON hico; /* handle to icon */
552 /* Create the image list. */
553 if ((himl = ImageList_Create(CX_ICON, CY_ICON,
554 ILC_MASK, 0, NUM_ICONS)) == NULL)
557 /* Add the open file, closed file, and document bitmaps. */
558 hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_OPEN_FILE));
559 Image_Open = ImageList_AddIcon(himl, hico);
561 hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_CLOSED_FILE));
562 Image_Closed = ImageList_AddIcon(himl, hico);
564 hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_ROOT));
565 Image_Root = ImageList_AddIcon(himl, hico);
567 /* Fail if not all of the images were added. */
568 if (ImageList_GetImageCount(himl) < NUM_ICONS)
573 /* Associate the image list with the tree view control. */
574 TreeView_SetImageList(hwndTV, himl, TVSIL_NORMAL);
579 BOOL UpdateExpandingTree(HWND hwndTV, HTREEITEM hItem, int state)
581 DWORD dwCount, dwIndex, dwMaxSubKeyLen;
582 HKEY hRoot, hNewKey, hKey;
588 static int expanding;
589 if (expanding) return FALSE;
590 if (state & TVIS_EXPANDEDONCE ) {
594 hcursorOld = SetCursor(LoadCursor(NULL, IDC_WAIT));
595 SendMessage(hwndTV, WM_SETREDRAW, FALSE, 0);
597 keyPath = GetItemPath(hwndTV, hItem, &hRoot);
598 if (!keyPath) goto done;
601 errCode = RegOpenKeyEx(hRoot, keyPath, 0, KEY_READ, &hNewKey);
602 if (errCode != ERROR_SUCCESS) goto done;
607 errCode = RegQueryInfoKey(hNewKey, 0, 0, 0, &dwCount, &dwMaxSubKeyLen, 0, 0, 0, 0, 0, 0);
608 if (errCode != ERROR_SUCCESS) goto done;
609 dwMaxSubKeyLen++; /* account for the \0 terminator */
610 Name = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(TCHAR));
611 if (!Name) goto done;
613 for (dwIndex = 0; dwIndex < dwCount; dwIndex++) {
614 DWORD cName = dwMaxSubKeyLen, dwSubCount;
616 errCode = RegEnumKeyEx(hNewKey, dwIndex, Name, &cName, 0, 0, 0, 0);
617 if (errCode != ERROR_SUCCESS) continue;
618 errCode = RegOpenKeyEx(hNewKey, Name, 0, KEY_QUERY_VALUE, &hKey);
619 if (errCode == ERROR_SUCCESS) {
620 errCode = RegQueryInfoKey(hKey, 0, 0, 0, &dwSubCount, 0, 0, 0, 0, 0, 0, 0);
623 if (errCode != ERROR_SUCCESS) dwSubCount = 0;
624 AddEntryToTree(hwndTV, hItem, Name, NULL, dwSubCount);
626 RegCloseKey(hNewKey);
627 HeapFree(GetProcessHeap(), 0, Name);
630 TreeView_SetItemState(hwndTV, hItem, TVIS_EXPANDEDONCE, TVIS_EXPANDEDONCE);
631 SendMessage(hwndTV, WM_SETREDRAW, TRUE, 0);
632 SetCursor(hcursorOld);
635 HeapFree(GetProcessHeap(), 0, keyPath);
640 BOOL OnTreeExpanding(HWND hwndTV, NMTREEVIEW* pnmtv)
642 return UpdateExpandingTree(hwndTV, pnmtv->itemNew.hItem, pnmtv->itemNew.state);
647 * CreateTreeView - creates a tree view control.
648 * Returns the handle to the new control if successful, or NULL otherwise.
649 * hwndParent - handle to the control's parent window.
651 HWND CreateTreeView(HWND hwndParent, LPTSTR pHostName, int id)
656 /* Get the dimensions of the parent window's client area, and create the tree view control. */
657 GetClientRect(hwndParent, &rcClient);
658 hwndTV = CreateWindowEx(WS_EX_CLIENTEDGE, WC_TREEVIEW, _T("Tree View"),
659 WS_VISIBLE | WS_CHILD | WS_TABSTOP | TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT,
660 0, 0, rcClient.right, rcClient.bottom,
661 hwndParent, (HMENU)id, hInst, NULL);
662 /* Initialize the image list, and add items to the control. */
663 if (!InitTreeViewImageLists(hwndTV) || !InitTreeViewItems(hwndTV, pHostName)) {
664 DestroyWindow(hwndTV);