Match PSDK STATUS_* definitions.
[wine] / programs / regedit / treeview.c
1 /*
2  * Regedit treeview
3  *
4  * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #define WIN32_LEAN_AND_MEAN     /* Exclude rarely-used stuff from Windows headers */
22
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>
32
33 #include "main.h"
34 #include "regproc.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(regedit);
37
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;
45
46 #define CX_ICON    16
47 #define CY_ICON    16
48 #define NUM_ICONS    3
49
50 static BOOL UpdateExpandingTree(HWND hwndTV, HTREEITEM hItem, int state);
51
52 static BOOL get_item_path(HWND hwndTV, HTREEITEM hItem, HKEY* phKey, LPTSTR* pKeyPath, int* pPathLen, int* pMaxLen)
53 {
54     TVITEM item;
55     int maxLen, len;
56     LPTSTR newStr;
57
58     item.mask = TVIF_PARAM;
59     item.hItem = hItem;
60     if (!TreeView_GetItem(hwndTV, &item)) return FALSE;
61
62     if (item.lParam) {
63         /* found root key with valid key value */
64         *phKey = (HKEY)item.lParam;
65         return TRUE;
66     }
67
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);
72     }
73
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;
84         }
85         newStr = HeapReAlloc(GetProcessHeap(), 0, *pKeyPath, *pMaxLen * 2);
86         if (!newStr) return FALSE;
87         *pKeyPath = newStr;
88         *pMaxLen *= 2;
89     } while(TRUE);
90
91     return TRUE;
92 }
93
94 LPTSTR GetItemPath(HWND hwndTV, HTREEITEM hItem, HKEY* phRootKey)
95 {
96     int pathLen = 0, maxLen;
97     TCHAR *pathBuffer;
98
99     pathBuffer = HeapAlloc(GetProcessHeap(), 0, 1024);
100     if (!pathBuffer) return NULL;
101     *pathBuffer = 0;
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;
107     return pathBuffer;
108 }
109
110 static LPTSTR get_path_component(LPCTSTR *lplpKeyName) {
111      LPCTSTR lpPos = *lplpKeyName;
112      LPTSTR lpResult = NULL;
113      int len;
114      if (!lpPos)
115          return NULL;
116      while(*lpPos && *lpPos != '\\')
117          lpPos++;
118      if (*lpPos && lpPos == *lplpKeyName)
119          return NULL;
120      len = (lpPos+1-(*lplpKeyName)) * sizeof(TCHAR);
121      lpResult = HeapAlloc(GetProcessHeap(), 0, len);
122      if (!lpResult) /* that would be very odd */
123          return NULL;
124      memcpy(lpResult, *lplpKeyName, len-1);
125      lpResult[len-1] = '\0';
126      *lplpKeyName = *lpPos ? lpPos+1 : NULL;
127      return lpResult;
128 }
129
130 HTREEITEM FindPathInTree(HWND hwndTV, LPCTSTR lpKeyName) {
131     TVITEMEX tvi;
132     TCHAR buf[261]; /* tree view has 260 character limitation on item name */
133     HTREEITEM hItem, hOldItem;
134
135     buf[260] = '\0';
136     hItem = TreeView_GetRoot(hwndTV);
137     TreeView_Expand(hwndTV, hItem, TVE_EXPAND);
138     hItem = TreeView_GetChild(hwndTV, hItem);
139     hOldItem = hItem;
140     while(1) {
141         LPTSTR lpItemName = get_path_component(&lpKeyName);
142         if (lpItemName) {
143             while(hItem) {
144                 tvi.mask = TVIF_TEXT | TVIF_HANDLE;
145                 tvi.hItem = hItem;
146                 tvi.pszText = buf;
147                 tvi.cchTextMax = 260;
148                 TreeView_GetItem(hwndTV, &tvi);
149                 if (!_tcsicmp(tvi.pszText, lpItemName)) {
150                      TreeView_Expand(hwndTV, hItem, TVE_EXPAND);
151                      if (!lpKeyName)
152                          return hItem;
153                      hOldItem = hItem;
154                      hItem = TreeView_GetChild(hwndTV, hItem);
155                      break;
156                 }
157                 hItem = TreeView_GetNextSibling(hwndTV, hItem);
158             }
159             HeapFree(GetProcessHeap(), 0, lpItemName);
160             if (!hItem)
161                 return hOldItem;
162         }
163         else
164             return hItem;
165     }
166 }
167
168 BOOL DeleteNode(HWND hwndTV, HTREEITEM hItem)
169 {
170     if (!hItem) hItem = TreeView_GetSelection(hwndTV);
171     if (!hItem) return FALSE;
172     return TreeView_DeleteItem(hwndTV, hItem);
173 }
174
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)
177 {
178     TVITEM tvi;
179     TVINSERTSTRUCT tvins;
180
181     if (hKey) {
182         if (RegQueryInfoKey(hKey, 0, 0, 0, &dwChildren, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
183             dwChildren = 0;
184         }
185     }
186
187     tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
188     tvi.pszText = label;
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;
194     tvins.u.item = tvi;
195     tvins.hInsertAfter = (HTREEITEM)(hKey ? TVI_LAST : TVI_SORT);
196     tvins.hParent = hParent;
197     return TreeView_InsertItem(hwndTV, &tvins);
198 }
199
200 static BOOL match_string(LPCTSTR sstring1, LPCTSTR sstring2, int mode)
201 {
202     if (mode & SEARCH_WHOLE)
203         return !stricmp(sstring1, sstring2);
204     else
205         return NULL != StrStrI(sstring1, sstring2);
206 }
207
208 static BOOL match_item(HWND hwndTV, HTREEITEM hItem, LPCTSTR sstring, int mode, int *row)
209 {
210     TVITEM item;
211     TCHAR keyname[KEY_MAX_LEN];
212     item.mask = TVIF_TEXT;
213     item.hItem = hItem;
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)) {
218         *row = -1;
219         return TRUE;
220     }
221
222     if (mode & (SEARCH_VALUES | SEARCH_CONTENT)) {
223         int i, adjust;
224         TCHAR valName[KEY_MAX_LEN], *KeyPath;
225         HKEY hKey, hRoot;
226         DWORD lenName;
227         
228         KeyPath = GetItemPath(hwndTV, hItem, &hRoot);
229
230         if (!KeyPath || !hRoot)
231              return FALSE;
232
233         if (RegOpenKeyEx(hRoot, KeyPath, 0, KEY_READ, &hKey) != ERROR_SUCCESS) {
234             HeapFree(GetProcessHeap(), 0, KeyPath);
235             return FALSE;
236         }
237             
238         HeapFree(GetProcessHeap(), 0, KeyPath);
239         lenName = KEY_MAX_LEN;
240         adjust = 0;
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)
244             adjust = 1;
245         
246         i = (*row)-adjust;
247         if (i < 0) i = 0;
248         while(1) {
249             DWORD lenValue = 0, type = 0;
250             lenName = KEY_MAX_LEN;
251             
252             if (ERROR_SUCCESS != RegEnumValue(hKey, 
253                 i, valName, &lenName, NULL, &type, NULL, &lenValue))
254                 break;
255             
256             if (mode & SEARCH_VALUES) {
257                 if (match_string(valName, sstring, mode)) {
258                     RegCloseKey(hKey);
259                     *row = i+adjust;
260                     return TRUE;
261                 }
262             }
263             
264             if ((mode & SEARCH_CONTENT) && (type == REG_EXPAND_SZ || type == REG_SZ)) {
265                 LPTSTR buffer;
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);
270                     RegCloseKey(hKey);
271                     *row = i+adjust;
272                     return TRUE;
273                 }
274                 HeapFree(GetProcessHeap(), 0, buffer);
275             }
276                             
277             i++;
278         }
279         RegCloseKey(hKey);
280     }        
281     return FALSE;
282 }
283
284 HTREEITEM FindNext(HWND hwndTV, HTREEITEM hItem, LPCTSTR sstring, int mode, int *row)
285 {
286     HTREEITEM hTry, hLast;
287     
288     hLast = hItem;
289     (*row)++;
290     if (match_item(hwndTV, hLast, sstring, mode & ~SEARCH_KEYS, row)) {
291         return hLast;
292     }
293     *row = 0;
294     
295     while(hLast) {
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));
300         }
301         hTry = TreeView_GetChild(hwndTV, hLast);
302         if (hTry) {
303             if (match_item(hwndTV, hTry, sstring, mode, row))
304                 return hTry;
305             hLast = hTry;
306             continue;
307         }
308         /* no more children, maybe there are any siblings? */
309         hTry = TreeView_GetNextSibling(hwndTV, hLast);
310         if (hTry) {
311             if (match_item(hwndTV, hTry, sstring, mode, row))
312                 return hTry;
313             hLast = hTry;
314             continue;
315         }
316         /* no more siblings, look at the next siblings in parent(s) */
317         hLast = TreeView_GetParent(hwndTV, hLast);
318         if (!hLast)
319             return NULL;
320         while (hLast && (hTry = TreeView_GetNextSibling(hwndTV, hLast)) == NULL) {
321             hLast = TreeView_GetParent(hwndTV, hLast);
322         }
323         if (match_item(hwndTV, hTry, sstring, mode, row))
324             return hTry;
325         hLast = hTry;
326     }
327     return NULL;
328 }
329
330 static BOOL RefreshTreeItem(HWND hwndTV, HTREEITEM hItem)
331 {
332     HKEY hRoot, hKey, hSubKey;
333     HTREEITEM childItem;
334     LPTSTR KeyPath;
335     DWORD dwCount, dwIndex, dwMaxSubKeyLen;
336     LPSTR Name;
337     TVITEM tvItem;
338     
339     hRoot = NULL;
340     KeyPath = GetItemPath(hwndTV, hItem, &hRoot);
341
342     if (!KeyPath || !hRoot)
343         return FALSE;
344
345     if (*KeyPath) {
346         if (RegOpenKeyEx(hRoot, KeyPath, 0, KEY_READ, &hKey) != ERROR_SUCCESS) {
347             WINE_TRACE("RegOpenKeyEx failed, \"%s\" was probably removed.\n", KeyPath);
348             return FALSE;
349         }
350     } else {
351         hKey = hRoot;
352     }
353     HeapFree(GetProcessHeap(), 0, KeyPath);
354
355     if (RegQueryInfoKey(hKey, 0, 0, 0, &dwCount, &dwMaxSubKeyLen, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
356         return FALSE;
357     }
358
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)) {
364         return FALSE;
365     }
366
367     /* We don't have to bother with the rest if it's not expanded. */
368     if (TreeView_GetItemState(hwndTV, hItem, TVIS_EXPANDED) == 0) {
369         RegCloseKey(hKey);
370         return TRUE;
371     }
372
373     dwMaxSubKeyLen++; /* account for the \0 terminator */
374     if (!(Name = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(TCHAR)))) {
375         return FALSE;
376     }
377     tvItem.cchTextMax = dwMaxSubKeyLen;
378     if (!(tvItem.pszText = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(TCHAR)))) {
379         return FALSE;
380     }
381
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;
385         BOOL found;
386
387         found = FALSE;
388         if (RegEnumKeyEx(hKey, dwIndex, Name, &cName, 0, 0, 0, NULL) != ERROR_SUCCESS) {
389             continue;
390         }
391
392         /* Find the number of children of the node. */
393         dwSubCount = 0;
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) {
396                 dwSubCount = 0;
397             }
398             RegCloseKey(hSubKey);
399         }
400
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)) {
407                 return FALSE;
408             }
409
410             if (!stricmp(tvItem.pszText, Name)) {
411                 found = TRUE;
412                 break;
413             }
414         }
415
416         if (found == FALSE) {
417             WINE_TRACE("New subkey %s\n", Name);
418             AddEntryToTree(hwndTV, hItem, Name, NULL, dwSubCount);
419         }
420     }
421     HeapFree(GetProcessHeap(), 0, Name);
422     HeapFree(GetProcessHeap(), 0, tvItem.pszText);
423     RegCloseKey(hKey);
424
425     /* Now go through all the children in the tree, and check if any have to be removed. */
426     childItem = TreeView_GetChild(hwndTV, hItem);
427     while (childItem) {
428         HTREEITEM nextItem = TreeView_GetNextSibling(hwndTV, childItem);
429         if (RefreshTreeItem(hwndTV, childItem) == FALSE) {
430             TreeView_DeleteItem(hwndTV, childItem);
431         }
432         childItem = nextItem;
433     }
434
435     return TRUE;
436 }
437
438 BOOL RefreshTreeView(HWND hwndTV)
439 {
440     HTREEITEM hItem;
441     HTREEITEM hSelectedItem;
442     HCURSOR hcursorOld;
443
444     WINE_TRACE("\n");
445     hSelectedItem = TreeView_GetSelection(hwndTV);
446     hcursorOld = SetCursor(LoadCursor(NULL, IDC_WAIT));
447     SendMessage(hwndTV, WM_SETREDRAW, FALSE, 0);
448
449     hItem = TreeView_GetChild(hwndTV, TreeView_GetRoot(hwndTV));
450     while (hItem) {
451         RefreshTreeItem(hwndTV, hItem);
452         hItem = TreeView_GetNextSibling(hwndTV, hItem);
453     }
454
455     SendMessage(hwndTV, WM_SETREDRAW, TRUE, 0);
456     InvalidateRect(hwndTV, NULL, FALSE);
457     SetCursor(hcursorOld);
458     
459     /* We reselect the currently selected node, this will prompt a refresh of the listview. */
460     TreeView_SelectItem(hwndTV, hSelectedItem);
461     return TRUE;
462 }
463
464 HTREEITEM InsertNode(HWND hwndTV, HTREEITEM hItem, LPTSTR name)
465 {
466     TCHAR buf[MAX_NEW_KEY_LEN];
467     HTREEITEM hNewItem = 0;
468     TVITEMEX item;
469
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);
474     } else {
475         item.mask = TVIF_CHILDREN | TVIF_HANDLE;
476         item.hItem = hItem;
477         if (!TreeView_GetItem(hwndTV, &item)) return FALSE;
478         item.cChildren = 1;
479         if (!TreeView_SetItem(hwndTV, &item)) return FALSE;
480     }
481     TreeView_Expand(hwndTV, hItem, TVE_EXPAND);
482     if (!hNewItem) {
483         for(hNewItem = TreeView_GetChild(hwndTV, hItem); hNewItem; hNewItem = TreeView_GetNextSibling(hwndTV, hNewItem)) {
484             item.mask = TVIF_HANDLE | TVIF_TEXT;
485             item.hItem = hNewItem;
486             item.pszText = buf;
487             item.cchTextMax = COUNT_OF(buf);
488             if (!TreeView_GetItem(hwndTV, &item)) continue;
489             if (lstrcmp(name, item.pszText) == 0) break;
490         }       
491     }
492     if (hNewItem) TreeView_SelectItem(hwndTV, hNewItem);
493
494     return hNewItem;
495 }
496
497 HWND StartKeyRename(HWND hwndTV)
498 {
499     HTREEITEM hItem;
500
501     if(!(hItem = TreeView_GetSelection(hwndTV))) return 0;
502     return TreeView_EditLabel(hwndTV, hItem);
503 }
504
505 static BOOL InitTreeViewItems(HWND hwndTV, LPTSTR pHostName)
506 {
507     TVITEM tvi;
508     TVINSERTSTRUCT tvins;
509     HTREEITEM hRoot;
510
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;
518     tvi.cChildren = 5;
519     /* Save the heading level in the item's application-defined data area.  */
520     tvi.lParam = (LPARAM)NULL;
521     tvins.u.item = tvi;
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;
526
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;
533     
534     /* expand and select host name */
535     TreeView_Expand(hwndTV, hRoot, TVE_EXPAND);
536     TreeView_Select(hwndTV, hRoot, TVGN_CARET);
537     return TRUE;
538 }
539
540
541 /*
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.
546  */
547 static BOOL InitTreeViewImageLists(HWND hwndTV)
548 {
549     HIMAGELIST himl;  /* handle to image list  */
550     HICON hico;       /* handle to icon  */
551
552     /* Create the image list.  */
553     if ((himl = ImageList_Create(CX_ICON, CY_ICON,
554                                  ILC_MASK, 0, NUM_ICONS)) == NULL)
555         return FALSE;
556
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);
560
561     hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_CLOSED_FILE));
562     Image_Closed = ImageList_AddIcon(himl, hico);
563
564     hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_ROOT));
565     Image_Root = ImageList_AddIcon(himl, hico);
566
567     /* Fail if not all of the images were added.  */
568     if (ImageList_GetImageCount(himl) < NUM_ICONS)
569     {
570       return FALSE;
571     }
572
573     /* Associate the image list with the tree view control.  */
574     TreeView_SetImageList(hwndTV, himl, TVSIL_NORMAL);
575
576     return TRUE;
577 }
578
579 BOOL UpdateExpandingTree(HWND hwndTV, HTREEITEM hItem, int state)
580 {
581     DWORD dwCount, dwIndex, dwMaxSubKeyLen;
582     HKEY hRoot, hNewKey, hKey;
583     LPTSTR keyPath;
584     LPTSTR Name;
585     LONG errCode;
586     HCURSOR hcursorOld;
587
588     static int expanding;
589     if (expanding) return FALSE;
590     if (state & TVIS_EXPANDEDONCE ) {
591         return TRUE;
592     }
593     expanding = TRUE;
594     hcursorOld = SetCursor(LoadCursor(NULL, IDC_WAIT));
595     SendMessage(hwndTV, WM_SETREDRAW, FALSE, 0);
596
597     keyPath = GetItemPath(hwndTV, hItem, &hRoot);
598     if (!keyPath) goto done;
599
600     if (*keyPath) {
601         errCode = RegOpenKeyEx(hRoot, keyPath, 0, KEY_READ, &hNewKey);
602         if (errCode != ERROR_SUCCESS) goto done;
603     } else {
604         hNewKey = hRoot;
605     }
606
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;
612
613     for (dwIndex = 0; dwIndex < dwCount; dwIndex++) {
614         DWORD cName = dwMaxSubKeyLen, dwSubCount;
615
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);
621             RegCloseKey(hKey);
622         }
623         if (errCode != ERROR_SUCCESS) dwSubCount = 0;
624         AddEntryToTree(hwndTV, hItem, Name, NULL, dwSubCount);
625     }
626     RegCloseKey(hNewKey);
627     HeapFree(GetProcessHeap(), 0, Name);
628
629 done:
630     TreeView_SetItemState(hwndTV, hItem, TVIS_EXPANDEDONCE, TVIS_EXPANDEDONCE);
631     SendMessage(hwndTV, WM_SETREDRAW, TRUE, 0);
632     SetCursor(hcursorOld);
633     expanding = FALSE;
634     if (keyPath)
635         HeapFree(GetProcessHeap(), 0, keyPath);
636
637     return TRUE;
638 }
639
640 BOOL OnTreeExpanding(HWND hwndTV, NMTREEVIEW* pnmtv)
641 {
642     return UpdateExpandingTree(hwndTV, pnmtv->itemNew.hItem, pnmtv->itemNew.state);
643 }
644
645
646 /*
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.
650  */
651 HWND CreateTreeView(HWND hwndParent, LPTSTR pHostName, int id)
652 {
653     RECT rcClient;
654     HWND hwndTV;
655
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);
665         return NULL;
666     }
667     return hwndTV;
668 }