regedit: Fix possible memory leaks.
[wine/hacks.git] / programs / regedit / treeview.c
blob45563851e1d46fb58eb795546a30b75fdf915ad4
1 /*
2 * Regedit treeview
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #define WIN32_LEAN_AND_MEAN /* Exclude rarely-used stuff from Windows headers */
23 #define NONAMELESSUNION
24 #define NONAMELESSSTRUCT
25 #include <windows.h>
26 #include <commctrl.h>
27 #include <stdlib.h>
28 #include <tchar.h>
29 #include <stdio.h>
30 #include <wine/debug.h>
31 #include <shlwapi.h>
33 #include "main.h"
34 #include "regproc.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. */
42 int Image_Open;
43 int Image_Closed;
44 int Image_Root;
46 #define CX_ICON 16
47 #define CY_ICON 16
48 #define NUM_ICONS 3
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)
54 TVITEM item;
55 int maxLen, len;
56 LPTSTR newStr;
58 item.mask = TVIF_PARAM;
59 item.hItem = hItem;
60 if (!TreeView_GetItem(hwndTV, &item)) return FALSE;
62 if (item.lParam) {
63 /* found root key with valid key value */
64 *phKey = (HKEY)item.lParam;
65 return TRUE;
68 if(!get_item_path(hwndTV, TreeView_GetParent(hwndTV, hItem), phKey, pKeyPath, pPathLen, pMaxLen)) return FALSE;
69 if (*pPathLen) {
70 (*pKeyPath)[*pPathLen] = _T('\\');
71 ++(*pPathLen);
74 do {
75 item.mask = TVIF_TEXT;
76 item.hItem = hItem;
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) {
82 *pPathLen += len;
83 break;
85 newStr = HeapReAlloc(GetProcessHeap(), 0, *pKeyPath, *pMaxLen * 2);
86 if (!newStr) return FALSE;
87 *pKeyPath = newStr;
88 *pMaxLen *= 2;
89 } while(TRUE);
91 return TRUE;
94 static BOOL get_item_pathW(HWND hwndTV, HTREEITEM hItem, HKEY* phKey, LPWSTR* pKeyPath, int* pPathLen, int* pMaxChars)
96 TVITEMW item;
97 int maxChars, chars;
98 LPWSTR newStr;
100 item.mask = TVIF_PARAM;
101 item.hItem = hItem;
102 if (!TreeView_GetItem(hwndTV, &item)) return FALSE;
104 if (item.lParam) {
105 /* found root key with valid key value */
106 *phKey = (HKEY)item.lParam;
107 return TRUE;
110 if(!get_item_pathW(hwndTV, TreeView_GetParent(hwndTV, hItem), phKey, pKeyPath, pPathLen, pMaxChars)) return FALSE;
111 if (*pPathLen) {
112 (*pKeyPath)[*pPathLen] = '\\';
113 ++(*pPathLen);
116 do {
117 item.mask = TVIF_TEXT;
118 item.hItem = hItem;
119 item.pszText = *pKeyPath + *pPathLen;
120 item.cchTextMax = maxChars = *pMaxChars - *pPathLen;
121 if (!TreeView_GetItemW(hwndTV, &item)) return FALSE;
122 chars = lstrlenW(item.pszText);
123 if (chars < maxChars - 1) {
124 *pPathLen += chars;
125 break;
127 newStr = HeapReAlloc(GetProcessHeap(), 0, *pKeyPath, *pMaxChars * 2);
128 if (!newStr) return FALSE;
129 *pKeyPath = newStr;
130 *pMaxChars *= 2;
131 } while(TRUE);
133 return TRUE;
136 LPTSTR GetItemPath(HWND hwndTV, HTREEITEM hItem, HKEY* phRootKey)
138 int pathLen = 0, maxLen;
139 TCHAR *pathBuffer;
141 pathBuffer = HeapAlloc(GetProcessHeap(), 0, 1024);
142 if (!pathBuffer) return NULL;
143 *pathBuffer = 0;
144 maxLen = HeapSize(GetProcessHeap(), 0, pathBuffer);
145 if (maxLen == (SIZE_T) - 1) return NULL;
146 if (!hItem) hItem = TreeView_GetSelection(hwndTV);
147 if (!hItem) return NULL;
148 if (!get_item_path(hwndTV, hItem, phRootKey, &pathBuffer, &pathLen, &maxLen)) return NULL;
149 return pathBuffer;
152 LPWSTR GetItemPathW(HWND hwndTV, HTREEITEM hItem, HKEY* phRootKey)
154 int pathLen = 0, maxLen;
155 WCHAR *pathBuffer;
157 pathBuffer = HeapAlloc(GetProcessHeap(), 0, 1024*sizeof(WCHAR));
158 if (!pathBuffer) return NULL;
159 *pathBuffer = 0;
160 maxLen = HeapSize(GetProcessHeap(), 0, pathBuffer);
161 if (maxLen == (SIZE_T) - 1) return NULL;
162 maxLen = maxLen / sizeof(WCHAR);
163 if (!hItem) hItem = TreeView_GetSelection(hwndTV);
164 if (!hItem) return NULL;
165 if (!get_item_pathW(hwndTV, hItem, phRootKey, &pathBuffer, &pathLen, &maxLen)) return NULL;
166 return pathBuffer;
169 static LPTSTR get_path_component(LPCTSTR *lplpKeyName) {
170 LPCTSTR lpPos = *lplpKeyName;
171 LPTSTR lpResult = NULL;
172 int len;
173 if (!lpPos)
174 return NULL;
175 while(*lpPos && *lpPos != '\\')
176 lpPos++;
177 if (*lpPos && lpPos == *lplpKeyName)
178 return NULL;
179 len = (lpPos+1-(*lplpKeyName)) * sizeof(TCHAR);
180 lpResult = HeapAlloc(GetProcessHeap(), 0, len);
181 if (!lpResult) /* that would be very odd */
182 return NULL;
183 memcpy(lpResult, *lplpKeyName, len-1);
184 lpResult[len-1] = '\0';
185 *lplpKeyName = *lpPos ? lpPos+1 : NULL;
186 return lpResult;
189 HTREEITEM FindPathInTree(HWND hwndTV, LPCTSTR lpKeyName) {
190 TVITEMEX tvi;
191 TCHAR buf[261]; /* tree view has 260 character limitation on item name */
192 HTREEITEM hItem, hOldItem;
194 buf[260] = '\0';
195 hItem = TreeView_GetRoot(hwndTV);
196 SendMessage(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)hItem );
197 hItem = TreeView_GetChild(hwndTV, hItem);
198 hOldItem = hItem;
199 while(1) {
200 LPTSTR lpItemName = get_path_component(&lpKeyName);
201 if (lpItemName) {
202 while(hItem) {
203 tvi.mask = TVIF_TEXT | TVIF_HANDLE;
204 tvi.hItem = hItem;
205 tvi.pszText = buf;
206 tvi.cchTextMax = 260;
207 SendMessage(hwndTV, TVM_GETITEM, 0, (LPARAM) &tvi);
208 if (!_tcsicmp(tvi.pszText, lpItemName)) {
209 SendMessage(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)hItem );
210 if (!lpKeyName)
211 return hItem;
212 hOldItem = hItem;
213 hItem = TreeView_GetChild(hwndTV, hItem);
214 break;
216 hItem = TreeView_GetNextSibling(hwndTV, hItem);
218 HeapFree(GetProcessHeap(), 0, lpItemName);
219 if (!hItem)
220 return hOldItem;
222 else
223 return hItem;
227 BOOL DeleteNode(HWND hwndTV, HTREEITEM hItem)
229 if (!hItem) hItem = TreeView_GetSelection(hwndTV);
230 if (!hItem) return FALSE;
231 return TreeView_DeleteItem(hwndTV, hItem);
234 /* Add an entry to the tree. Only give hKey for root nodes (HKEY_ constants) */
235 static HTREEITEM AddEntryToTree(HWND hwndTV, HTREEITEM hParent, LPWSTR label, HKEY hKey, DWORD dwChildren)
237 TVINSERTSTRUCTW tvins;
239 if (hKey) {
240 if (RegQueryInfoKeyW(hKey, 0, 0, 0, &dwChildren, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
241 dwChildren = 0;
245 tvins.u.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
246 tvins.u.item.pszText = label;
247 tvins.u.item.cchTextMax = lstrlenW(label);
248 tvins.u.item.iImage = Image_Closed;
249 tvins.u.item.iSelectedImage = Image_Open;
250 tvins.u.item.cChildren = dwChildren;
251 tvins.u.item.lParam = (LPARAM)hKey;
252 tvins.hInsertAfter = (HTREEITEM)(hKey ? TVI_LAST : TVI_SORT);
253 tvins.hParent = hParent;
255 return TreeView_InsertItemW(hwndTV, &tvins);
258 static BOOL match_string(LPCTSTR sstring1, LPCTSTR sstring2, int mode)
260 if (mode & SEARCH_WHOLE)
261 return !stricmp(sstring1, sstring2);
262 else
263 return NULL != StrStrI(sstring1, sstring2);
266 static BOOL match_item(HWND hwndTV, HTREEITEM hItem, LPCTSTR sstring, int mode, int *row)
268 TVITEM item;
269 TCHAR keyname[KEY_MAX_LEN];
270 item.mask = TVIF_TEXT;
271 item.hItem = hItem;
272 item.pszText = keyname;
273 item.cchTextMax = KEY_MAX_LEN;
274 if (!TreeView_GetItem(hwndTV, &item)) return FALSE;
275 if ((mode & SEARCH_KEYS) && match_string(keyname, sstring, mode)) {
276 *row = -1;
277 return TRUE;
280 if (mode & (SEARCH_VALUES | SEARCH_CONTENT)) {
281 int i, adjust;
282 TCHAR valName[KEY_MAX_LEN], *KeyPath;
283 HKEY hKey, hRoot;
284 DWORD lenName;
286 KeyPath = GetItemPath(hwndTV, hItem, &hRoot);
288 if (!KeyPath || !hRoot)
289 return FALSE;
291 if (RegOpenKeyEx(hRoot, KeyPath, 0, KEY_READ, &hKey) != ERROR_SUCCESS) {
292 HeapFree(GetProcessHeap(), 0, KeyPath);
293 return FALSE;
296 HeapFree(GetProcessHeap(), 0, KeyPath);
297 lenName = KEY_MAX_LEN;
298 adjust = 0;
299 /* RegEnumValue won't return empty default value, so fake it when dealing with *row,
300 which corresponds to list view rows, not value ids */
301 if (ERROR_SUCCESS == RegEnumValue(hKey, 0, valName, &lenName, NULL, NULL, NULL, NULL) && *valName)
302 adjust = 1;
304 i = (*row)-adjust;
305 if (i < 0) i = 0;
306 while(1) {
307 DWORD lenValue = 0, type = 0;
308 lenName = KEY_MAX_LEN;
310 if (ERROR_SUCCESS != RegEnumValue(hKey,
311 i, valName, &lenName, NULL, &type, NULL, &lenValue))
312 break;
314 if (mode & SEARCH_VALUES) {
315 if (match_string(valName, sstring, mode)) {
316 RegCloseKey(hKey);
317 *row = i+adjust;
318 return TRUE;
322 if ((mode & SEARCH_CONTENT) && (type == REG_EXPAND_SZ || type == REG_SZ)) {
323 LPTSTR buffer;
324 buffer = HeapAlloc(GetProcessHeap(), 0, lenValue);
325 RegEnumValue(hKey, i, valName, &lenName, NULL, &type, (LPBYTE)buffer, &lenValue);
326 if (match_string(buffer, sstring, mode)) {
327 HeapFree(GetProcessHeap(), 0, buffer);
328 RegCloseKey(hKey);
329 *row = i+adjust;
330 return TRUE;
332 HeapFree(GetProcessHeap(), 0, buffer);
335 i++;
337 RegCloseKey(hKey);
339 return FALSE;
342 HTREEITEM FindNext(HWND hwndTV, HTREEITEM hItem, LPCTSTR sstring, int mode, int *row)
344 HTREEITEM hTry, hLast;
346 hLast = hItem;
347 (*row)++;
348 if (match_item(hwndTV, hLast, sstring, mode & ~SEARCH_KEYS, row)) {
349 return hLast;
351 *row = 0;
353 while(hLast) {
354 /* first look in subtree */
355 /* no children? maybe we haven't loaded them yet? */
356 if (!TreeView_GetChild(hwndTV, hLast)) {
357 UpdateExpandingTree(hwndTV, hLast, TreeView_GetItemState(hwndTV, hLast, -1));
359 hTry = TreeView_GetChild(hwndTV, hLast);
360 if (hTry) {
361 if (match_item(hwndTV, hTry, sstring, mode, row))
362 return hTry;
363 hLast = hTry;
364 continue;
366 /* no more children, maybe there are any siblings? */
367 hTry = TreeView_GetNextSibling(hwndTV, hLast);
368 if (hTry) {
369 if (match_item(hwndTV, hTry, sstring, mode, row))
370 return hTry;
371 hLast = hTry;
372 continue;
374 /* no more siblings, look at the next siblings in parent(s) */
375 hLast = TreeView_GetParent(hwndTV, hLast);
376 if (!hLast)
377 return NULL;
378 while (hLast && (hTry = TreeView_GetNextSibling(hwndTV, hLast)) == NULL) {
379 hLast = TreeView_GetParent(hwndTV, hLast);
381 if (match_item(hwndTV, hTry, sstring, mode, row))
382 return hTry;
383 hLast = hTry;
385 return NULL;
388 static BOOL RefreshTreeItem(HWND hwndTV, HTREEITEM hItem)
390 HKEY hRoot, hKey, hSubKey;
391 HTREEITEM childItem;
392 LPWSTR KeyPath;
393 DWORD dwCount, dwIndex, dwMaxSubKeyLen;
394 LPWSTR Name;
395 TVITEMW tvItem;
397 hRoot = NULL;
398 KeyPath = GetItemPathW(hwndTV, hItem, &hRoot);
400 if (!KeyPath || !hRoot)
401 return FALSE;
403 if (*KeyPath) {
404 if (RegOpenKeyExW(hRoot, KeyPath, 0, KEY_READ, &hKey) != ERROR_SUCCESS) {
405 WINE_TRACE("RegOpenKeyEx failed, %s was probably removed.\n", wine_dbgstr_w(KeyPath));
406 return FALSE;
408 } else {
409 hKey = hRoot;
411 HeapFree(GetProcessHeap(), 0, KeyPath);
413 if (RegQueryInfoKeyW(hKey, 0, 0, 0, &dwCount, &dwMaxSubKeyLen, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
414 return FALSE;
417 /* Set the number of children again */
418 tvItem.mask = TVIF_CHILDREN;
419 tvItem.hItem = hItem;
420 tvItem.cChildren = dwCount;
421 if (!TreeView_SetItemW(hwndTV, &tvItem)) {
422 return FALSE;
425 /* We don't have to bother with the rest if it's not expanded. */
426 if (TreeView_GetItemState(hwndTV, hItem, TVIS_EXPANDED) == 0) {
427 RegCloseKey(hKey);
428 return TRUE;
431 dwMaxSubKeyLen++; /* account for the \0 terminator */
432 if (!(Name = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(WCHAR)))) {
433 return FALSE;
435 tvItem.cchTextMax = dwMaxSubKeyLen;
436 if (!(tvItem.pszText = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(WCHAR)))) {
437 HeapFree(GetProcessHeap(), 0, Name);
438 return FALSE;
441 /* Now go through all the children in the registry, and check if any have to be added. */
442 for (dwIndex = 0; dwIndex < dwCount; dwIndex++) {
443 DWORD cName = dwMaxSubKeyLen, dwSubCount;
444 BOOL found;
446 found = FALSE;
447 if (RegEnumKeyExW(hKey, dwIndex, Name, &cName, 0, 0, 0, NULL) != ERROR_SUCCESS) {
448 continue;
451 /* Find the number of children of the node. */
452 dwSubCount = 0;
453 if (RegOpenKeyExW(hKey, Name, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
454 if (RegQueryInfoKey(hSubKey, 0, 0, 0, &dwSubCount, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
455 dwSubCount = 0;
457 RegCloseKey(hSubKey);
460 /* Check if the node is already in there. */
461 for (childItem = TreeView_GetChild(hwndTV, hItem); childItem;
462 childItem = TreeView_GetNextSibling(hwndTV, childItem)) {
463 tvItem.mask = TVIF_TEXT;
464 tvItem.hItem = childItem;
465 if (!TreeView_GetItemW(hwndTV, &tvItem)) {
466 HeapFree(GetProcessHeap(), 0, Name);
467 HeapFree(GetProcessHeap(), 0, tvItem.pszText);
468 return FALSE;
471 if (!lstrcmpiW(tvItem.pszText, Name)) {
472 found = TRUE;
473 break;
477 if (found == FALSE) {
478 WINE_TRACE("New subkey %s\n", wine_dbgstr_w(Name));
479 AddEntryToTree(hwndTV, hItem, Name, NULL, dwSubCount);
482 HeapFree(GetProcessHeap(), 0, Name);
483 HeapFree(GetProcessHeap(), 0, tvItem.pszText);
484 RegCloseKey(hKey);
486 /* Now go through all the children in the tree, and check if any have to be removed. */
487 childItem = TreeView_GetChild(hwndTV, hItem);
488 while (childItem) {
489 HTREEITEM nextItem = TreeView_GetNextSibling(hwndTV, childItem);
490 if (RefreshTreeItem(hwndTV, childItem) == FALSE) {
491 SendMessageW(hwndTV, TVM_DELETEITEM, 0, (LPARAM)childItem);
493 childItem = nextItem;
496 return TRUE;
499 BOOL RefreshTreeView(HWND hwndTV)
501 HTREEITEM hItem;
502 HTREEITEM hSelectedItem;
503 HCURSOR hcursorOld;
505 WINE_TRACE("\n");
506 hSelectedItem = TreeView_GetSelection(hwndTV);
507 hcursorOld = SetCursor(LoadCursor(NULL, IDC_WAIT));
508 SendMessage(hwndTV, WM_SETREDRAW, FALSE, 0);
510 hItem = TreeView_GetChild(hwndTV, TreeView_GetRoot(hwndTV));
511 while (hItem) {
512 RefreshTreeItem(hwndTV, hItem);
513 hItem = TreeView_GetNextSibling(hwndTV, hItem);
516 SendMessage(hwndTV, WM_SETREDRAW, TRUE, 0);
517 InvalidateRect(hwndTV, NULL, FALSE);
518 SetCursor(hcursorOld);
520 /* We reselect the currently selected node, this will prompt a refresh of the listview. */
521 SendMessage(hwndTV, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hSelectedItem);
522 return TRUE;
525 HTREEITEM InsertNode(HWND hwndTV, HTREEITEM hItem, LPWSTR name)
527 WCHAR buf[MAX_NEW_KEY_LEN];
528 HTREEITEM hNewItem = 0;
529 TVITEMEXW item;
531 if (!hItem) hItem = TreeView_GetSelection(hwndTV);
532 if (!hItem) return FALSE;
533 if (TreeView_GetItemState(hwndTV, hItem, TVIS_EXPANDEDONCE)) {
534 hNewItem = AddEntryToTree(hwndTV, hItem, name, 0, 0);
535 } else {
536 item.mask = TVIF_CHILDREN | TVIF_HANDLE;
537 item.hItem = hItem;
538 if (!TreeView_GetItemW(hwndTV, &item)) return FALSE;
539 item.cChildren = 1;
540 if (!TreeView_SetItemW(hwndTV, &item)) return FALSE;
542 SendMessageW(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)hItem );
543 if (!hNewItem) {
544 for(hNewItem = TreeView_GetChild(hwndTV, hItem); hNewItem; hNewItem = TreeView_GetNextSibling(hwndTV, hNewItem)) {
545 item.mask = TVIF_HANDLE | TVIF_TEXT;
546 item.hItem = hNewItem;
547 item.pszText = buf;
548 item.cchTextMax = COUNT_OF(buf);
549 if (!TreeView_GetItemW(hwndTV, &item)) continue;
550 if (lstrcmpW(name, item.pszText) == 0) break;
553 if (hNewItem)
554 SendMessageW(hwndTV, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hNewItem);
556 return hNewItem;
559 HWND StartKeyRename(HWND hwndTV)
561 HTREEITEM hItem;
563 if(!(hItem = TreeView_GetSelection(hwndTV))) return 0;
564 return TreeView_EditLabel(hwndTV, hItem);
567 static BOOL InitTreeViewItems(HWND hwndTV, LPTSTR pHostName)
569 TVINSERTSTRUCT tvins;
570 HTREEITEM hRoot;
571 static WCHAR hkcr[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0},
572 hkcu[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0},
573 hklm[] = {'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0},
574 hku[] = {'H','K','E','Y','_','U','S','E','R','S',0},
575 hkcc[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0},
576 hkdd[] = {'H','K','E','Y','_','D','Y','N','_','D','A','T','A',0};
578 tvins.u.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
579 /* Set the text of the item. */
580 tvins.u.item.pszText = pHostName;
581 tvins.u.item.cchTextMax = lstrlen(pHostName);
582 /* Assume the item is not a parent item, so give it an image. */
583 tvins.u.item.iImage = Image_Root;
584 tvins.u.item.iSelectedImage = Image_Root;
585 tvins.u.item.cChildren = 5;
586 /* Save the heading level in the item's application-defined data area. */
587 tvins.u.item.lParam = (LPARAM)NULL;
588 tvins.hInsertAfter = (HTREEITEM)TVI_FIRST;
589 tvins.hParent = TVI_ROOT;
590 /* Add the item to the tree view control. */
591 if (!(hRoot = TreeView_InsertItem(hwndTV, &tvins))) return FALSE;
593 if (!AddEntryToTree(hwndTV, hRoot, hkcr, HKEY_CLASSES_ROOT, 1)) return FALSE;
594 if (!AddEntryToTree(hwndTV, hRoot, hkcu, HKEY_CURRENT_USER, 1)) return FALSE;
595 if (!AddEntryToTree(hwndTV, hRoot, hklm, HKEY_LOCAL_MACHINE, 1)) return FALSE;
596 if (!AddEntryToTree(hwndTV, hRoot, hku, HKEY_USERS, 1)) return FALSE;
597 if (!AddEntryToTree(hwndTV, hRoot, hkcc, HKEY_CURRENT_CONFIG, 1)) return FALSE;
598 if (!AddEntryToTree(hwndTV, hRoot, hkdd, HKEY_DYN_DATA, 1)) return FALSE;
600 /* expand and select host name */
601 SendMessage(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)hRoot );
602 SendMessage(hwndTV, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hRoot);
603 return TRUE;
608 * InitTreeViewImageLists - creates an image list, adds three bitmaps
609 * to it, and associates the image list with a tree view control.
610 * Returns TRUE if successful, or FALSE otherwise.
611 * hwndTV - handle to the tree view control.
613 static BOOL InitTreeViewImageLists(HWND hwndTV)
615 HIMAGELIST himl; /* handle to image list */
616 HICON hico; /* handle to icon */
618 /* Create the image list. */
619 if ((himl = ImageList_Create(CX_ICON, CY_ICON,
620 ILC_MASK, 0, NUM_ICONS)) == NULL)
621 return FALSE;
623 /* Add the open file, closed file, and document bitmaps. */
624 hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_OPEN_FILE));
625 Image_Open = ImageList_AddIcon(himl, hico);
627 hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_CLOSED_FILE));
628 Image_Closed = ImageList_AddIcon(himl, hico);
630 hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_ROOT));
631 Image_Root = ImageList_AddIcon(himl, hico);
633 /* Fail if not all of the images were added. */
634 if (ImageList_GetImageCount(himl) < NUM_ICONS)
636 return FALSE;
639 /* Associate the image list with the tree view control. */
640 SendMessage(hwndTV, TVM_SETIMAGELIST, TVSIL_NORMAL, (LPARAM)himl);
642 return TRUE;
645 BOOL UpdateExpandingTree(HWND hwndTV, HTREEITEM hItem, int state)
647 DWORD dwCount, dwIndex, dwMaxSubKeyLen;
648 HKEY hRoot, hNewKey, hKey;
649 LPWSTR keyPath;
650 LPWSTR Name;
651 LONG errCode;
652 HCURSOR hcursorOld;
654 static int expanding;
655 if (expanding) return FALSE;
656 if (state & TVIS_EXPANDEDONCE ) {
657 return TRUE;
659 expanding = TRUE;
660 hcursorOld = SetCursor(LoadCursor(NULL, IDC_WAIT));
661 SendMessageW(hwndTV, WM_SETREDRAW, FALSE, 0);
663 keyPath = GetItemPathW(hwndTV, hItem, &hRoot);
664 if (!keyPath) goto done;
666 if (*keyPath) {
667 errCode = RegOpenKeyExW(hRoot, keyPath, 0, KEY_READ, &hNewKey);
668 if (errCode != ERROR_SUCCESS) goto done;
669 } else {
670 hNewKey = hRoot;
673 errCode = RegQueryInfoKeyW(hNewKey, 0, 0, 0, &dwCount, &dwMaxSubKeyLen, 0, 0, 0, 0, 0, 0);
674 if (errCode != ERROR_SUCCESS) goto done;
675 dwMaxSubKeyLen++; /* account for the \0 terminator */
676 Name = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(WCHAR));
677 if (!Name) goto done;
679 for (dwIndex = 0; dwIndex < dwCount; dwIndex++) {
680 DWORD cName = dwMaxSubKeyLen, dwSubCount;
682 errCode = RegEnumKeyExW(hNewKey, dwIndex, Name, &cName, 0, 0, 0, 0);
683 if (errCode != ERROR_SUCCESS) continue;
684 errCode = RegOpenKeyExW(hNewKey, Name, 0, KEY_QUERY_VALUE, &hKey);
685 if (errCode == ERROR_SUCCESS) {
686 errCode = RegQueryInfoKeyW(hKey, 0, 0, 0, &dwSubCount, 0, 0, 0, 0, 0, 0, 0);
687 RegCloseKey(hKey);
689 if (errCode != ERROR_SUCCESS) dwSubCount = 0;
690 AddEntryToTree(hwndTV, hItem, Name, NULL, dwSubCount);
692 RegCloseKey(hNewKey);
693 HeapFree(GetProcessHeap(), 0, Name);
695 done:
696 TreeView_SetItemState(hwndTV, hItem, TVIS_EXPANDEDONCE, TVIS_EXPANDEDONCE);
697 SendMessage(hwndTV, WM_SETREDRAW, TRUE, 0);
698 SetCursor(hcursorOld);
699 expanding = FALSE;
700 HeapFree(GetProcessHeap(), 0, keyPath);
702 return TRUE;
705 BOOL OnTreeExpanding(HWND hwndTV, NMTREEVIEW* pnmtv)
707 return UpdateExpandingTree(hwndTV, pnmtv->itemNew.hItem, pnmtv->itemNew.state);
712 * CreateTreeView - creates a tree view control.
713 * Returns the handle to the new control if successful, or NULL otherwise.
714 * hwndParent - handle to the control's parent window.
716 HWND CreateTreeView(HWND hwndParent, LPTSTR pHostName, UINT id)
718 RECT rcClient;
719 HWND hwndTV;
721 /* Get the dimensions of the parent window's client area, and create the tree view control. */
722 GetClientRect(hwndParent, &rcClient);
723 hwndTV = CreateWindowEx(WS_EX_CLIENTEDGE, WC_TREEVIEW, _T("Tree View"),
724 WS_VISIBLE | WS_CHILD | WS_TABSTOP | TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT,
725 0, 0, rcClient.right, rcClient.bottom,
726 hwndParent, (HMENU)ULongToHandle(id), hInst, NULL);
727 /* Initialize the image list, and add items to the control. */
728 if (!InitTreeViewImageLists(hwndTV) || !InitTreeViewItems(hwndTV, pHostName)) {
729 DestroyWindow(hwndTV);
730 return NULL;
732 return hwndTV;