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 SendMessage(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)hItem );
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 SendMessage(hwndTV, TVM_GETITEM, 0, (LPARAM) &tvi);
149 if (!_tcsicmp(tvi.pszText, lpItemName)) {
150 SendMessage(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)hItem );
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)
178 TVINSERTSTRUCT tvins;
181 if (RegQueryInfoKey(hKey, 0, 0, 0, &dwChildren, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
186 tvins.u.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
187 tvins.u.item.pszText = label;
188 tvins.u.item.cchTextMax = lstrlen(label);
189 tvins.u.item.iImage = Image_Closed;
190 tvins.u.item.iSelectedImage = Image_Open;
191 tvins.u.item.cChildren = dwChildren;
192 tvins.u.item.lParam = (LPARAM)hKey;
193 tvins.hInsertAfter = (HTREEITEM)(hKey ? TVI_LAST : TVI_SORT);
194 tvins.hParent = hParent;
195 return TreeView_InsertItem(hwndTV, &tvins);
198 static BOOL match_string(LPCTSTR sstring1, LPCTSTR sstring2, int mode)
200 if (mode & SEARCH_WHOLE)
201 return !stricmp(sstring1, sstring2);
203 return NULL != StrStrI(sstring1, sstring2);
206 static BOOL match_item(HWND hwndTV, HTREEITEM hItem, LPCTSTR sstring, int mode, int *row)
209 TCHAR keyname[KEY_MAX_LEN];
210 item.mask = TVIF_TEXT;
212 item.pszText = keyname;
213 item.cchTextMax = KEY_MAX_LEN;
214 if (!TreeView_GetItem(hwndTV, &item)) return FALSE;
215 if ((mode & SEARCH_KEYS) && match_string(keyname, sstring, mode)) {
220 if (mode & (SEARCH_VALUES | SEARCH_CONTENT)) {
222 TCHAR valName[KEY_MAX_LEN], *KeyPath;
226 KeyPath = GetItemPath(hwndTV, hItem, &hRoot);
228 if (!KeyPath || !hRoot)
231 if (RegOpenKeyEx(hRoot, KeyPath, 0, KEY_READ, &hKey) != ERROR_SUCCESS) {
232 HeapFree(GetProcessHeap(), 0, KeyPath);
236 HeapFree(GetProcessHeap(), 0, KeyPath);
237 lenName = KEY_MAX_LEN;
239 /* RegEnumValue won't return empty default value, so fake it when dealing with *row,
240 which corresponds to list view rows, not value ids */
241 if (ERROR_SUCCESS == RegEnumValue(hKey, 0, valName, &lenName, NULL, NULL, NULL, NULL) && *valName)
247 DWORD lenValue = 0, type = 0;
248 lenName = KEY_MAX_LEN;
250 if (ERROR_SUCCESS != RegEnumValue(hKey,
251 i, valName, &lenName, NULL, &type, NULL, &lenValue))
254 if (mode & SEARCH_VALUES) {
255 if (match_string(valName, sstring, mode)) {
262 if ((mode & SEARCH_CONTENT) && (type == REG_EXPAND_SZ || type == REG_SZ)) {
264 buffer = HeapAlloc(GetProcessHeap(), 0, lenValue);
265 RegEnumValue(hKey, i, valName, &lenName, NULL, &type, (LPBYTE)buffer, &lenValue);
266 if (match_string(buffer, sstring, mode)) {
267 HeapFree(GetProcessHeap(), 0, buffer);
272 HeapFree(GetProcessHeap(), 0, buffer);
282 HTREEITEM FindNext(HWND hwndTV, HTREEITEM hItem, LPCTSTR sstring, int mode, int *row)
284 HTREEITEM hTry, hLast;
288 if (match_item(hwndTV, hLast, sstring, mode & ~SEARCH_KEYS, row)) {
294 /* first look in subtree */
295 /* no children? maybe we haven't loaded them yet? */
296 if (!TreeView_GetChild(hwndTV, hLast)) {
297 UpdateExpandingTree(hwndTV, hLast, TreeView_GetItemState(hwndTV, hLast, -1));
299 hTry = TreeView_GetChild(hwndTV, hLast);
301 if (match_item(hwndTV, hTry, sstring, mode, row))
306 /* no more children, maybe there are any siblings? */
307 hTry = TreeView_GetNextSibling(hwndTV, hLast);
309 if (match_item(hwndTV, hTry, sstring, mode, row))
314 /* no more siblings, look at the next siblings in parent(s) */
315 hLast = TreeView_GetParent(hwndTV, hLast);
318 while (hLast && (hTry = TreeView_GetNextSibling(hwndTV, hLast)) == NULL) {
319 hLast = TreeView_GetParent(hwndTV, hLast);
321 if (match_item(hwndTV, hTry, sstring, mode, row))
328 static BOOL RefreshTreeItem(HWND hwndTV, HTREEITEM hItem)
330 HKEY hRoot, hKey, hSubKey;
333 DWORD dwCount, dwIndex, dwMaxSubKeyLen;
338 KeyPath = GetItemPath(hwndTV, hItem, &hRoot);
340 if (!KeyPath || !hRoot)
344 if (RegOpenKeyEx(hRoot, KeyPath, 0, KEY_READ, &hKey) != ERROR_SUCCESS) {
345 WINE_TRACE("RegOpenKeyEx failed, \"%s\" was probably removed.\n", KeyPath);
351 HeapFree(GetProcessHeap(), 0, KeyPath);
353 if (RegQueryInfoKey(hKey, 0, 0, 0, &dwCount, &dwMaxSubKeyLen, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
357 /* Set the number of children again */
358 tvItem.mask = TVIF_CHILDREN;
359 tvItem.hItem = hItem;
360 tvItem.cChildren = dwCount;
361 if (!TreeView_SetItem(hwndTV, &tvItem)) {
365 /* We don't have to bother with the rest if it's not expanded. */
366 if (TreeView_GetItemState(hwndTV, hItem, TVIS_EXPANDED) == 0) {
371 dwMaxSubKeyLen++; /* account for the \0 terminator */
372 if (!(Name = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(TCHAR)))) {
375 tvItem.cchTextMax = dwMaxSubKeyLen;
376 if (!(tvItem.pszText = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(TCHAR)))) {
380 /* Now go through all the children in the registry, and check if any have to be added. */
381 for (dwIndex = 0; dwIndex < dwCount; dwIndex++) {
382 DWORD cName = dwMaxSubKeyLen, dwSubCount;
386 if (RegEnumKeyEx(hKey, dwIndex, Name, &cName, 0, 0, 0, NULL) != ERROR_SUCCESS) {
390 /* Find the number of children of the node. */
392 if (RegOpenKeyEx(hKey, Name, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
393 if (RegQueryInfoKey(hSubKey, 0, 0, 0, &dwSubCount, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
396 RegCloseKey(hSubKey);
399 /* Check if the node is already in there. */
400 for (childItem = TreeView_GetChild(hwndTV, hItem); childItem;
401 childItem = TreeView_GetNextSibling(hwndTV, childItem)) {
402 tvItem.mask = TVIF_TEXT;
403 tvItem.hItem = childItem;
404 if (!TreeView_GetItem(hwndTV, &tvItem)) {
408 if (!stricmp(tvItem.pszText, Name)) {
414 if (found == FALSE) {
415 WINE_TRACE("New subkey %s\n", Name);
416 AddEntryToTree(hwndTV, hItem, Name, NULL, dwSubCount);
419 HeapFree(GetProcessHeap(), 0, Name);
420 HeapFree(GetProcessHeap(), 0, tvItem.pszText);
423 /* Now go through all the children in the tree, and check if any have to be removed. */
424 childItem = TreeView_GetChild(hwndTV, hItem);
426 HTREEITEM nextItem = TreeView_GetNextSibling(hwndTV, childItem);
427 if (RefreshTreeItem(hwndTV, childItem) == FALSE) {
428 SendMessage(hwndTV, TVM_DELETEITEM, 0, (LPARAM)childItem);
430 childItem = nextItem;
436 BOOL RefreshTreeView(HWND hwndTV)
439 HTREEITEM hSelectedItem;
443 hSelectedItem = TreeView_GetSelection(hwndTV);
444 hcursorOld = SetCursor(LoadCursor(NULL, IDC_WAIT));
445 SendMessage(hwndTV, WM_SETREDRAW, FALSE, 0);
447 hItem = TreeView_GetChild(hwndTV, TreeView_GetRoot(hwndTV));
449 RefreshTreeItem(hwndTV, hItem);
450 hItem = TreeView_GetNextSibling(hwndTV, hItem);
453 SendMessage(hwndTV, WM_SETREDRAW, TRUE, 0);
454 InvalidateRect(hwndTV, NULL, FALSE);
455 SetCursor(hcursorOld);
457 /* We reselect the currently selected node, this will prompt a refresh of the listview. */
458 SendMessage(hwndTV, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hSelectedItem);
462 HTREEITEM InsertNode(HWND hwndTV, HTREEITEM hItem, LPTSTR name)
464 TCHAR buf[MAX_NEW_KEY_LEN];
465 HTREEITEM hNewItem = 0;
468 if (!hItem) hItem = TreeView_GetSelection(hwndTV);
469 if (!hItem) return FALSE;
470 if (TreeView_GetItemState(hwndTV, hItem, TVIS_EXPANDEDONCE)) {
471 hNewItem = AddEntryToTree(hwndTV, hItem, name, 0, 0);
473 item.mask = TVIF_CHILDREN | TVIF_HANDLE;
475 if (!TreeView_GetItem(hwndTV, &item)) return FALSE;
477 if (!TreeView_SetItem(hwndTV, &item)) return FALSE;
479 SendMessage(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)hItem );
481 for(hNewItem = TreeView_GetChild(hwndTV, hItem); hNewItem; hNewItem = TreeView_GetNextSibling(hwndTV, hNewItem)) {
482 item.mask = TVIF_HANDLE | TVIF_TEXT;
483 item.hItem = hNewItem;
485 item.cchTextMax = COUNT_OF(buf);
486 if (!TreeView_GetItem(hwndTV, &item)) continue;
487 if (lstrcmp(name, item.pszText) == 0) break;
491 SendMessage(hwndTV, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hNewItem);
496 HWND StartKeyRename(HWND hwndTV)
500 if(!(hItem = TreeView_GetSelection(hwndTV))) return 0;
501 return TreeView_EditLabel(hwndTV, hItem);
504 static BOOL InitTreeViewItems(HWND hwndTV, LPTSTR pHostName)
506 TVINSERTSTRUCT tvins;
509 tvins.u.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
510 /* Set the text of the item. */
511 tvins.u.item.pszText = pHostName;
512 tvins.u.item.cchTextMax = lstrlen(pHostName);
513 /* Assume the item is not a parent item, so give it an image. */
514 tvins.u.item.iImage = Image_Root;
515 tvins.u.item.iSelectedImage = Image_Root;
516 tvins.u.item.cChildren = 5;
517 /* Save the heading level in the item's application-defined data area. */
518 tvins.u.item.lParam = (LPARAM)NULL;
519 tvins.hInsertAfter = (HTREEITEM)TVI_FIRST;
520 tvins.hParent = TVI_ROOT;
521 /* Add the item to the tree view control. */
522 if (!(hRoot = TreeView_InsertItem(hwndTV, &tvins))) return FALSE;
524 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_CLASSES_ROOT"), HKEY_CLASSES_ROOT, 1)) return FALSE;
525 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_CURRENT_USER"), HKEY_CURRENT_USER, 1)) return FALSE;
526 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_LOCAL_MACHINE"), HKEY_LOCAL_MACHINE, 1)) return FALSE;
527 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_USERS"), HKEY_USERS, 1)) return FALSE;
528 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_CURRENT_CONFIG"), HKEY_CURRENT_CONFIG, 1)) return FALSE;
529 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_DYN_DATA"), HKEY_DYN_DATA, 1)) return FALSE;
531 /* expand and select host name */
532 SendMessage(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)hRoot );
533 SendMessage(hwndTV, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hRoot);
539 * InitTreeViewImageLists - creates an image list, adds three bitmaps
540 * to it, and associates the image list with a tree view control.
541 * Returns TRUE if successful, or FALSE otherwise.
542 * hwndTV - handle to the tree view control.
544 static BOOL InitTreeViewImageLists(HWND hwndTV)
546 HIMAGELIST himl; /* handle to image list */
547 HICON hico; /* handle to icon */
549 /* Create the image list. */
550 if ((himl = ImageList_Create(CX_ICON, CY_ICON,
551 ILC_MASK, 0, NUM_ICONS)) == NULL)
554 /* Add the open file, closed file, and document bitmaps. */
555 hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_OPEN_FILE));
556 Image_Open = ImageList_AddIcon(himl, hico);
558 hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_CLOSED_FILE));
559 Image_Closed = ImageList_AddIcon(himl, hico);
561 hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_ROOT));
562 Image_Root = ImageList_AddIcon(himl, hico);
564 /* Fail if not all of the images were added. */
565 if (ImageList_GetImageCount(himl) < NUM_ICONS)
570 /* Associate the image list with the tree view control. */
571 SendMessage(hwndTV, TVM_SETIMAGELIST, TVSIL_NORMAL, (LPARAM)himl);
576 BOOL UpdateExpandingTree(HWND hwndTV, HTREEITEM hItem, int state)
578 DWORD dwCount, dwIndex, dwMaxSubKeyLen;
579 HKEY hRoot, hNewKey, hKey;
585 static int expanding;
586 if (expanding) return FALSE;
587 if (state & TVIS_EXPANDEDONCE ) {
591 hcursorOld = SetCursor(LoadCursor(NULL, IDC_WAIT));
592 SendMessage(hwndTV, WM_SETREDRAW, FALSE, 0);
594 keyPath = GetItemPath(hwndTV, hItem, &hRoot);
595 if (!keyPath) goto done;
598 errCode = RegOpenKeyEx(hRoot, keyPath, 0, KEY_READ, &hNewKey);
599 if (errCode != ERROR_SUCCESS) goto done;
604 errCode = RegQueryInfoKey(hNewKey, 0, 0, 0, &dwCount, &dwMaxSubKeyLen, 0, 0, 0, 0, 0, 0);
605 if (errCode != ERROR_SUCCESS) goto done;
606 dwMaxSubKeyLen++; /* account for the \0 terminator */
607 Name = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(TCHAR));
608 if (!Name) goto done;
610 for (dwIndex = 0; dwIndex < dwCount; dwIndex++) {
611 DWORD cName = dwMaxSubKeyLen, dwSubCount;
613 errCode = RegEnumKeyEx(hNewKey, dwIndex, Name, &cName, 0, 0, 0, 0);
614 if (errCode != ERROR_SUCCESS) continue;
615 errCode = RegOpenKeyEx(hNewKey, Name, 0, KEY_QUERY_VALUE, &hKey);
616 if (errCode == ERROR_SUCCESS) {
617 errCode = RegQueryInfoKey(hKey, 0, 0, 0, &dwSubCount, 0, 0, 0, 0, 0, 0, 0);
620 if (errCode != ERROR_SUCCESS) dwSubCount = 0;
621 AddEntryToTree(hwndTV, hItem, Name, NULL, dwSubCount);
623 RegCloseKey(hNewKey);
624 HeapFree(GetProcessHeap(), 0, Name);
627 TreeView_SetItemState(hwndTV, hItem, TVIS_EXPANDEDONCE, TVIS_EXPANDEDONCE);
628 SendMessage(hwndTV, WM_SETREDRAW, TRUE, 0);
629 SetCursor(hcursorOld);
632 HeapFree(GetProcessHeap(), 0, keyPath);
637 BOOL OnTreeExpanding(HWND hwndTV, NMTREEVIEW* pnmtv)
639 return UpdateExpandingTree(hwndTV, pnmtv->itemNew.hItem, pnmtv->itemNew.state);
644 * CreateTreeView - creates a tree view control.
645 * Returns the handle to the new control if successful, or NULL otherwise.
646 * hwndParent - handle to the control's parent window.
648 HWND CreateTreeView(HWND hwndParent, LPTSTR pHostName, int id)
653 /* Get the dimensions of the parent window's client area, and create the tree view control. */
654 GetClientRect(hwndParent, &rcClient);
655 hwndTV = CreateWindowEx(WS_EX_CLIENTEDGE, WC_TREEVIEW, _T("Tree View"),
656 WS_VISIBLE | WS_CHILD | WS_TABSTOP | TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT,
657 0, 0, rcClient.right, rcClient.bottom,
658 hwndParent, (HMENU)id, hInst, NULL);
659 /* Initialize the image list, and add items to the control. */
660 if (!InitTreeViewImageLists(hwndTV) || !InitTreeViewItems(hwndTV, pHostName)) {
661 DestroyWindow(hwndTV);