crypt32: Split the test for ret and last error up.
[wine.git] / programs / regedit / treeview.c
blob12dd87a91e5984ebee7e884d0c497d2fe28efd6f
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 LPWSTR get_path_component(LPCWSTR *lplpKeyName) {
170 LPCWSTR lpPos = *lplpKeyName;
171 LPWSTR 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);
180 lpResult = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
181 if (!lpResult) /* that would be very odd */
182 return NULL;
183 lstrcpynW(lpResult, *lplpKeyName, len);
184 *lplpKeyName = *lpPos ? lpPos+1 : NULL;
185 return lpResult;
188 HTREEITEM FindPathInTree(HWND hwndTV, LPCWSTR lpKeyName) {
189 TVITEMEXW tvi;
190 WCHAR buf[261]; /* tree view has 260 character limitation on item name */
191 HTREEITEM hItem, hOldItem;
193 buf[260] = '\0';
194 hItem = TreeView_GetRoot(hwndTV);
195 SendMessageW(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)hItem );
196 hItem = TreeView_GetChild(hwndTV, hItem);
197 hOldItem = hItem;
198 while(1) {
199 LPWSTR 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 SendMessageW(hwndTV, TVM_GETITEMW, 0, (LPARAM) &tvi);
208 if (!lstrcmpiW(tvi.pszText, lpItemName)) {
209 SendMessageW(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)hItem );
210 if (!lpKeyName)
212 HeapFree(GetProcessHeap(), 0, lpItemName);
213 return hItem;
215 hOldItem = hItem;
216 hItem = TreeView_GetChild(hwndTV, hItem);
217 break;
219 hItem = TreeView_GetNextSibling(hwndTV, hItem);
221 HeapFree(GetProcessHeap(), 0, lpItemName);
222 if (!hItem)
223 return hOldItem;
225 else
226 return hItem;
230 BOOL DeleteNode(HWND hwndTV, HTREEITEM hItem)
232 if (!hItem) hItem = TreeView_GetSelection(hwndTV);
233 if (!hItem) return FALSE;
234 return TreeView_DeleteItem(hwndTV, hItem);
237 /* Add an entry to the tree. Only give hKey for root nodes (HKEY_ constants) */
238 static HTREEITEM AddEntryToTree(HWND hwndTV, HTREEITEM hParent, LPWSTR label, HKEY hKey, DWORD dwChildren)
240 TVINSERTSTRUCTW tvins;
242 if (hKey) {
243 if (RegQueryInfoKeyW(hKey, 0, 0, 0, &dwChildren, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
244 dwChildren = 0;
248 tvins.u.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
249 tvins.u.item.pszText = label;
250 tvins.u.item.cchTextMax = lstrlenW(label);
251 tvins.u.item.iImage = Image_Closed;
252 tvins.u.item.iSelectedImage = Image_Open;
253 tvins.u.item.cChildren = dwChildren;
254 tvins.u.item.lParam = (LPARAM)hKey;
255 tvins.hInsertAfter = (HTREEITEM)(hKey ? TVI_LAST : TVI_SORT);
256 tvins.hParent = hParent;
258 return TreeView_InsertItemW(hwndTV, &tvins);
261 static BOOL match_string(LPCWSTR sstring1, LPCWSTR sstring2, int mode)
263 if (mode & SEARCH_WHOLE)
264 return !lstrcmpiW(sstring1, sstring2);
265 else
266 return NULL != StrStrIW(sstring1, sstring2);
269 static BOOL match_item(HWND hwndTV, HTREEITEM hItem, LPCWSTR sstring, int mode, int *row)
271 TVITEMW item;
272 WCHAR keyname[KEY_MAX_LEN];
273 item.mask = TVIF_TEXT;
274 item.hItem = hItem;
275 item.pszText = keyname;
276 item.cchTextMax = KEY_MAX_LEN;
277 if (!TreeView_GetItem(hwndTV, &item)) return FALSE;
278 if ((mode & SEARCH_KEYS) && match_string(keyname, sstring, mode)) {
279 *row = -1;
280 return TRUE;
283 if (mode & (SEARCH_VALUES | SEARCH_CONTENT)) {
284 int i, adjust;
285 WCHAR valName[KEY_MAX_LEN], *KeyPath;
286 HKEY hKey, hRoot;
287 DWORD lenName;
289 KeyPath = GetItemPathW(hwndTV, hItem, &hRoot);
291 if (!KeyPath || !hRoot)
292 return FALSE;
294 if (RegOpenKeyExW(hRoot, KeyPath, 0, KEY_READ, &hKey) != ERROR_SUCCESS) {
295 HeapFree(GetProcessHeap(), 0, KeyPath);
296 return FALSE;
299 HeapFree(GetProcessHeap(), 0, KeyPath);
300 lenName = KEY_MAX_LEN;
301 adjust = 0;
302 /* RegEnumValue won't return empty default value, so fake it when dealing with *row,
303 which corresponds to list view rows, not value ids */
304 if (ERROR_SUCCESS == RegEnumValueW(hKey, 0, valName, &lenName, NULL, NULL, NULL, NULL) && *valName)
305 adjust = 1;
307 i = (*row)-adjust;
308 if (i < 0) i = 0;
309 while(1) {
310 DWORD lenValue = 0, type = 0;
311 lenName = KEY_MAX_LEN;
313 if (ERROR_SUCCESS != RegEnumValueW(hKey,
314 i, valName, &lenName, NULL, &type, NULL, &lenValue))
315 break;
317 if (mode & SEARCH_VALUES) {
318 if (match_string(valName, sstring, mode)) {
319 RegCloseKey(hKey);
320 *row = i+adjust;
321 return TRUE;
325 if ((mode & SEARCH_CONTENT) && (type == REG_EXPAND_SZ || type == REG_SZ)) {
326 LPWSTR buffer;
327 buffer = HeapAlloc(GetProcessHeap(), 0, lenValue);
328 RegEnumValueW(hKey, i, valName, &lenName, NULL, &type, (LPBYTE)buffer, &lenValue);
329 if (match_string(buffer, sstring, mode)) {
330 HeapFree(GetProcessHeap(), 0, buffer);
331 RegCloseKey(hKey);
332 *row = i+adjust;
333 return TRUE;
335 HeapFree(GetProcessHeap(), 0, buffer);
338 i++;
340 RegCloseKey(hKey);
342 return FALSE;
345 HTREEITEM FindNext(HWND hwndTV, HTREEITEM hItem, LPCWSTR sstring, int mode, int *row)
347 HTREEITEM hTry, hLast;
349 hLast = hItem;
350 (*row)++;
351 if (match_item(hwndTV, hLast, sstring, mode & ~SEARCH_KEYS, row)) {
352 return hLast;
354 *row = 0;
356 while(hLast) {
357 /* first look in subtree */
358 /* no children? maybe we haven't loaded them yet? */
359 if (!TreeView_GetChild(hwndTV, hLast)) {
360 UpdateExpandingTree(hwndTV, hLast, TreeView_GetItemState(hwndTV, hLast, -1));
362 hTry = TreeView_GetChild(hwndTV, hLast);
363 if (hTry) {
364 if (match_item(hwndTV, hTry, sstring, mode, row))
365 return hTry;
366 hLast = hTry;
367 continue;
369 /* no more children, maybe there are any siblings? */
370 hTry = TreeView_GetNextSibling(hwndTV, hLast);
371 if (hTry) {
372 if (match_item(hwndTV, hTry, sstring, mode, row))
373 return hTry;
374 hLast = hTry;
375 continue;
377 /* no more siblings, look at the next siblings in parent(s) */
378 hLast = TreeView_GetParent(hwndTV, hLast);
379 if (!hLast)
380 return NULL;
381 while (hLast && (hTry = TreeView_GetNextSibling(hwndTV, hLast)) == NULL) {
382 hLast = TreeView_GetParent(hwndTV, hLast);
384 if (match_item(hwndTV, hTry, sstring, mode, row))
385 return hTry;
386 hLast = hTry;
388 return NULL;
391 static BOOL RefreshTreeItem(HWND hwndTV, HTREEITEM hItem)
393 HKEY hRoot, hKey, hSubKey;
394 HTREEITEM childItem;
395 LPWSTR KeyPath;
396 DWORD dwCount, dwIndex, dwMaxSubKeyLen;
397 LPWSTR Name;
398 TVITEMW tvItem;
400 hRoot = NULL;
401 KeyPath = GetItemPathW(hwndTV, hItem, &hRoot);
403 if (!KeyPath || !hRoot)
404 return FALSE;
406 if (*KeyPath) {
407 if (RegOpenKeyExW(hRoot, KeyPath, 0, KEY_READ, &hKey) != ERROR_SUCCESS) {
408 WINE_TRACE("RegOpenKeyEx failed, %s was probably removed.\n", wine_dbgstr_w(KeyPath));
409 return FALSE;
411 } else {
412 hKey = hRoot;
414 HeapFree(GetProcessHeap(), 0, KeyPath);
416 if (RegQueryInfoKeyW(hKey, 0, 0, 0, &dwCount, &dwMaxSubKeyLen, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
417 return FALSE;
420 /* Set the number of children again */
421 tvItem.mask = TVIF_CHILDREN;
422 tvItem.hItem = hItem;
423 tvItem.cChildren = dwCount;
424 if (!TreeView_SetItemW(hwndTV, &tvItem)) {
425 return FALSE;
428 /* We don't have to bother with the rest if it's not expanded. */
429 if (TreeView_GetItemState(hwndTV, hItem, TVIS_EXPANDED) == 0) {
430 RegCloseKey(hKey);
431 return TRUE;
434 dwMaxSubKeyLen++; /* account for the \0 terminator */
435 if (!(Name = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(WCHAR)))) {
436 return FALSE;
438 tvItem.cchTextMax = dwMaxSubKeyLen;
439 if (!(tvItem.pszText = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(WCHAR)))) {
440 HeapFree(GetProcessHeap(), 0, Name);
441 return FALSE;
444 /* Now go through all the children in the registry, and check if any have to be added. */
445 for (dwIndex = 0; dwIndex < dwCount; dwIndex++) {
446 DWORD cName = dwMaxSubKeyLen, dwSubCount;
447 BOOL found;
449 found = FALSE;
450 if (RegEnumKeyExW(hKey, dwIndex, Name, &cName, 0, 0, 0, NULL) != ERROR_SUCCESS) {
451 continue;
454 /* Find the number of children of the node. */
455 dwSubCount = 0;
456 if (RegOpenKeyExW(hKey, Name, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
457 if (RegQueryInfoKey(hSubKey, 0, 0, 0, &dwSubCount, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
458 dwSubCount = 0;
460 RegCloseKey(hSubKey);
463 /* Check if the node is already in there. */
464 for (childItem = TreeView_GetChild(hwndTV, hItem); childItem;
465 childItem = TreeView_GetNextSibling(hwndTV, childItem)) {
466 tvItem.mask = TVIF_TEXT;
467 tvItem.hItem = childItem;
468 if (!TreeView_GetItemW(hwndTV, &tvItem)) {
469 HeapFree(GetProcessHeap(), 0, Name);
470 HeapFree(GetProcessHeap(), 0, tvItem.pszText);
471 return FALSE;
474 if (!lstrcmpiW(tvItem.pszText, Name)) {
475 found = TRUE;
476 break;
480 if (found == FALSE) {
481 WINE_TRACE("New subkey %s\n", wine_dbgstr_w(Name));
482 AddEntryToTree(hwndTV, hItem, Name, NULL, dwSubCount);
485 HeapFree(GetProcessHeap(), 0, Name);
486 HeapFree(GetProcessHeap(), 0, tvItem.pszText);
487 RegCloseKey(hKey);
489 /* Now go through all the children in the tree, and check if any have to be removed. */
490 childItem = TreeView_GetChild(hwndTV, hItem);
491 while (childItem) {
492 HTREEITEM nextItem = TreeView_GetNextSibling(hwndTV, childItem);
493 if (RefreshTreeItem(hwndTV, childItem) == FALSE) {
494 SendMessageW(hwndTV, TVM_DELETEITEM, 0, (LPARAM)childItem);
496 childItem = nextItem;
499 return TRUE;
502 BOOL RefreshTreeView(HWND hwndTV)
504 HTREEITEM hItem;
505 HTREEITEM hSelectedItem;
506 HCURSOR hcursorOld;
508 WINE_TRACE("\n");
509 hSelectedItem = TreeView_GetSelection(hwndTV);
510 hcursorOld = SetCursor(LoadCursor(NULL, IDC_WAIT));
511 SendMessageW(hwndTV, WM_SETREDRAW, FALSE, 0);
513 hItem = TreeView_GetChild(hwndTV, TreeView_GetRoot(hwndTV));
514 while (hItem) {
515 RefreshTreeItem(hwndTV, hItem);
516 hItem = TreeView_GetNextSibling(hwndTV, hItem);
519 SendMessageW(hwndTV, WM_SETREDRAW, TRUE, 0);
520 InvalidateRect(hwndTV, NULL, FALSE);
521 SetCursor(hcursorOld);
523 /* We reselect the currently selected node, this will prompt a refresh of the listview. */
524 SendMessageW(hwndTV, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hSelectedItem);
525 return TRUE;
528 HTREEITEM InsertNode(HWND hwndTV, HTREEITEM hItem, LPWSTR name)
530 WCHAR buf[MAX_NEW_KEY_LEN];
531 HTREEITEM hNewItem = 0;
532 TVITEMEXW item;
534 if (!hItem) hItem = TreeView_GetSelection(hwndTV);
535 if (!hItem) return FALSE;
536 if (TreeView_GetItemState(hwndTV, hItem, TVIS_EXPANDEDONCE)) {
537 hNewItem = AddEntryToTree(hwndTV, hItem, name, 0, 0);
538 } else {
539 item.mask = TVIF_CHILDREN | TVIF_HANDLE;
540 item.hItem = hItem;
541 if (!TreeView_GetItemW(hwndTV, &item)) return FALSE;
542 item.cChildren = 1;
543 if (!TreeView_SetItemW(hwndTV, &item)) return FALSE;
545 SendMessageW(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)hItem );
546 if (!hNewItem) {
547 for(hNewItem = TreeView_GetChild(hwndTV, hItem); hNewItem; hNewItem = TreeView_GetNextSibling(hwndTV, hNewItem)) {
548 item.mask = TVIF_HANDLE | TVIF_TEXT;
549 item.hItem = hNewItem;
550 item.pszText = buf;
551 item.cchTextMax = COUNT_OF(buf);
552 if (!TreeView_GetItemW(hwndTV, &item)) continue;
553 if (lstrcmpW(name, item.pszText) == 0) break;
556 if (hNewItem)
557 SendMessageW(hwndTV, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hNewItem);
559 return hNewItem;
562 HWND StartKeyRename(HWND hwndTV)
564 HTREEITEM hItem;
566 if(!(hItem = TreeView_GetSelection(hwndTV))) return 0;
567 return TreeView_EditLabel(hwndTV, hItem);
570 static BOOL InitTreeViewItems(HWND hwndTV, LPWSTR pHostName)
572 TVINSERTSTRUCTW tvins;
573 HTREEITEM hRoot;
574 static WCHAR hkcr[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0},
575 hkcu[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0},
576 hklm[] = {'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0},
577 hku[] = {'H','K','E','Y','_','U','S','E','R','S',0},
578 hkcc[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0},
579 hkdd[] = {'H','K','E','Y','_','D','Y','N','_','D','A','T','A',0};
581 tvins.u.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
582 /* Set the text of the item. */
583 tvins.u.item.pszText = pHostName;
584 tvins.u.item.cchTextMax = lstrlenW(pHostName);
585 /* Assume the item is not a parent item, so give it an image. */
586 tvins.u.item.iImage = Image_Root;
587 tvins.u.item.iSelectedImage = Image_Root;
588 tvins.u.item.cChildren = 5;
589 /* Save the heading level in the item's application-defined data area. */
590 tvins.u.item.lParam = (LPARAM)NULL;
591 tvins.hInsertAfter = (HTREEITEM)TVI_FIRST;
592 tvins.hParent = TVI_ROOT;
593 /* Add the item to the tree view control. */
594 if (!(hRoot = TreeView_InsertItemW(hwndTV, &tvins))) return FALSE;
596 if (!AddEntryToTree(hwndTV, hRoot, hkcr, HKEY_CLASSES_ROOT, 1)) return FALSE;
597 if (!AddEntryToTree(hwndTV, hRoot, hkcu, HKEY_CURRENT_USER, 1)) return FALSE;
598 if (!AddEntryToTree(hwndTV, hRoot, hklm, HKEY_LOCAL_MACHINE, 1)) return FALSE;
599 if (!AddEntryToTree(hwndTV, hRoot, hku, HKEY_USERS, 1)) return FALSE;
600 if (!AddEntryToTree(hwndTV, hRoot, hkcc, HKEY_CURRENT_CONFIG, 1)) return FALSE;
601 if (!AddEntryToTree(hwndTV, hRoot, hkdd, HKEY_DYN_DATA, 1)) return FALSE;
603 /* expand and select host name */
604 SendMessageW(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)hRoot );
605 SendMessageW(hwndTV, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hRoot);
606 return TRUE;
611 * InitTreeViewImageLists - creates an image list, adds three bitmaps
612 * to it, and associates the image list with a tree view control.
613 * Returns TRUE if successful, or FALSE otherwise.
614 * hwndTV - handle to the tree view control.
616 static BOOL InitTreeViewImageLists(HWND hwndTV)
618 HIMAGELIST himl; /* handle to image list */
619 HICON hico; /* handle to icon */
621 /* Create the image list. */
622 if ((himl = ImageList_Create(CX_ICON, CY_ICON,
623 ILC_MASK, 0, NUM_ICONS)) == NULL)
624 return FALSE;
626 /* Add the open file, closed file, and document bitmaps. */
627 hico = LoadIconW(hInst, MAKEINTRESOURCEW(IDI_OPEN_FILE));
628 Image_Open = ImageList_AddIcon(himl, hico);
630 hico = LoadIconW(hInst, MAKEINTRESOURCEW(IDI_CLOSED_FILE));
631 Image_Closed = ImageList_AddIcon(himl, hico);
633 hico = LoadIconW(hInst, MAKEINTRESOURCEW(IDI_ROOT));
634 Image_Root = ImageList_AddIcon(himl, hico);
636 /* Fail if not all of the images were added. */
637 if (ImageList_GetImageCount(himl) < NUM_ICONS)
639 return FALSE;
642 /* Associate the image list with the tree view control. */
643 SendMessageW(hwndTV, TVM_SETIMAGELIST, TVSIL_NORMAL, (LPARAM)himl);
645 return TRUE;
648 BOOL UpdateExpandingTree(HWND hwndTV, HTREEITEM hItem, int state)
650 DWORD dwCount, dwIndex, dwMaxSubKeyLen;
651 HKEY hRoot, hNewKey, hKey;
652 LPWSTR keyPath;
653 LPWSTR Name;
654 LONG errCode;
655 HCURSOR hcursorOld;
657 static int expanding;
658 if (expanding) return FALSE;
659 if (state & TVIS_EXPANDEDONCE ) {
660 return TRUE;
662 expanding = TRUE;
663 hcursorOld = SetCursor(LoadCursor(NULL, IDC_WAIT));
664 SendMessageW(hwndTV, WM_SETREDRAW, FALSE, 0);
666 keyPath = GetItemPathW(hwndTV, hItem, &hRoot);
667 if (!keyPath) goto done;
669 if (*keyPath) {
670 errCode = RegOpenKeyExW(hRoot, keyPath, 0, KEY_READ, &hNewKey);
671 if (errCode != ERROR_SUCCESS) goto done;
672 } else {
673 hNewKey = hRoot;
676 errCode = RegQueryInfoKeyW(hNewKey, 0, 0, 0, &dwCount, &dwMaxSubKeyLen, 0, 0, 0, 0, 0, 0);
677 if (errCode != ERROR_SUCCESS) goto done;
678 dwMaxSubKeyLen++; /* account for the \0 terminator */
679 Name = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(WCHAR));
680 if (!Name) goto done;
682 for (dwIndex = 0; dwIndex < dwCount; dwIndex++) {
683 DWORD cName = dwMaxSubKeyLen, dwSubCount;
685 errCode = RegEnumKeyExW(hNewKey, dwIndex, Name, &cName, 0, 0, 0, 0);
686 if (errCode != ERROR_SUCCESS) continue;
687 errCode = RegOpenKeyExW(hNewKey, Name, 0, KEY_QUERY_VALUE, &hKey);
688 if (errCode == ERROR_SUCCESS) {
689 errCode = RegQueryInfoKeyW(hKey, 0, 0, 0, &dwSubCount, 0, 0, 0, 0, 0, 0, 0);
690 RegCloseKey(hKey);
692 if (errCode != ERROR_SUCCESS) dwSubCount = 0;
693 AddEntryToTree(hwndTV, hItem, Name, NULL, dwSubCount);
695 RegCloseKey(hNewKey);
696 HeapFree(GetProcessHeap(), 0, Name);
698 done:
699 TreeView_SetItemState(hwndTV, hItem, TVIS_EXPANDEDONCE, TVIS_EXPANDEDONCE);
700 SendMessageW(hwndTV, WM_SETREDRAW, TRUE, 0);
701 SetCursor(hcursorOld);
702 expanding = FALSE;
703 HeapFree(GetProcessHeap(), 0, keyPath);
705 return TRUE;
708 BOOL OnTreeExpanding(HWND hwndTV, NMTREEVIEW* pnmtv)
710 return UpdateExpandingTree(hwndTV, pnmtv->itemNew.hItem, pnmtv->itemNew.state);
715 * CreateTreeView - creates a tree view control.
716 * Returns the handle to the new control if successful, or NULL otherwise.
717 * hwndParent - handle to the control's parent window.
719 HWND CreateTreeView(HWND hwndParent, LPWSTR pHostName, UINT id)
721 RECT rcClient;
722 HWND hwndTV;
723 WCHAR TreeView[] = {'T','r','e','e',' ','V','i','e','w',0};
725 /* Get the dimensions of the parent window's client area, and create the tree view control. */
726 GetClientRect(hwndParent, &rcClient);
727 hwndTV = CreateWindowExW(WS_EX_CLIENTEDGE, WC_TREEVIEWW, TreeView,
728 WS_VISIBLE | WS_CHILD | WS_TABSTOP | TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT,
729 0, 0, rcClient.right, rcClient.bottom,
730 hwndParent, (HMENU)ULongToHandle(id), hInst, NULL);
731 /* Initialize the image list, and add items to the control. */
732 if (!InitTreeViewImageLists(hwndTV) || !InitTreeViewItems(hwndTV, pHostName)) {
733 DestroyWindow(hwndTV);
734 return NULL;
736 return hwndTV;