winecfg: Winecfg is a program, not a library. So tweak the license and warranty messa...
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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     SendMessage(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)hItem );
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                 SendMessage(hwndTV, TVM_GETITEM, 0, (LPARAM) &tvi);
149                 if (!_tcsicmp(tvi.pszText, lpItemName)) {
150                      SendMessage(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)hItem );
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     TVINSERTSTRUCT tvins;
179
180     if (hKey) {
181         if (RegQueryInfoKey(hKey, 0, 0, 0, &dwChildren, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
182             dwChildren = 0;
183         }
184     }
185
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);
196 }
197
198 static BOOL match_string(LPCTSTR sstring1, LPCTSTR sstring2, int mode)
199 {
200     if (mode & SEARCH_WHOLE)
201         return !stricmp(sstring1, sstring2);
202     else
203         return NULL != StrStrI(sstring1, sstring2);
204 }
205
206 static BOOL match_item(HWND hwndTV, HTREEITEM hItem, LPCTSTR sstring, int mode, int *row)
207 {
208     TVITEM item;
209     TCHAR keyname[KEY_MAX_LEN];
210     item.mask = TVIF_TEXT;
211     item.hItem = hItem;
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)) {
216         *row = -1;
217         return TRUE;
218     }
219
220     if (mode & (SEARCH_VALUES | SEARCH_CONTENT)) {
221         int i, adjust;
222         TCHAR valName[KEY_MAX_LEN], *KeyPath;
223         HKEY hKey, hRoot;
224         DWORD lenName;
225         
226         KeyPath = GetItemPath(hwndTV, hItem, &hRoot);
227
228         if (!KeyPath || !hRoot)
229              return FALSE;
230
231         if (RegOpenKeyEx(hRoot, KeyPath, 0, KEY_READ, &hKey) != ERROR_SUCCESS) {
232             HeapFree(GetProcessHeap(), 0, KeyPath);
233             return FALSE;
234         }
235             
236         HeapFree(GetProcessHeap(), 0, KeyPath);
237         lenName = KEY_MAX_LEN;
238         adjust = 0;
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)
242             adjust = 1;
243         
244         i = (*row)-adjust;
245         if (i < 0) i = 0;
246         while(1) {
247             DWORD lenValue = 0, type = 0;
248             lenName = KEY_MAX_LEN;
249             
250             if (ERROR_SUCCESS != RegEnumValue(hKey, 
251                 i, valName, &lenName, NULL, &type, NULL, &lenValue))
252                 break;
253             
254             if (mode & SEARCH_VALUES) {
255                 if (match_string(valName, sstring, mode)) {
256                     RegCloseKey(hKey);
257                     *row = i+adjust;
258                     return TRUE;
259                 }
260             }
261             
262             if ((mode & SEARCH_CONTENT) && (type == REG_EXPAND_SZ || type == REG_SZ)) {
263                 LPTSTR buffer;
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);
268                     RegCloseKey(hKey);
269                     *row = i+adjust;
270                     return TRUE;
271                 }
272                 HeapFree(GetProcessHeap(), 0, buffer);
273             }
274                             
275             i++;
276         }
277         RegCloseKey(hKey);
278     }        
279     return FALSE;
280 }
281
282 HTREEITEM FindNext(HWND hwndTV, HTREEITEM hItem, LPCTSTR sstring, int mode, int *row)
283 {
284     HTREEITEM hTry, hLast;
285     
286     hLast = hItem;
287     (*row)++;
288     if (match_item(hwndTV, hLast, sstring, mode & ~SEARCH_KEYS, row)) {
289         return hLast;
290     }
291     *row = 0;
292     
293     while(hLast) {
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));
298         }
299         hTry = TreeView_GetChild(hwndTV, hLast);
300         if (hTry) {
301             if (match_item(hwndTV, hTry, sstring, mode, row))
302                 return hTry;
303             hLast = hTry;
304             continue;
305         }
306         /* no more children, maybe there are any siblings? */
307         hTry = TreeView_GetNextSibling(hwndTV, hLast);
308         if (hTry) {
309             if (match_item(hwndTV, hTry, sstring, mode, row))
310                 return hTry;
311             hLast = hTry;
312             continue;
313         }
314         /* no more siblings, look at the next siblings in parent(s) */
315         hLast = TreeView_GetParent(hwndTV, hLast);
316         if (!hLast)
317             return NULL;
318         while (hLast && (hTry = TreeView_GetNextSibling(hwndTV, hLast)) == NULL) {
319             hLast = TreeView_GetParent(hwndTV, hLast);
320         }
321         if (match_item(hwndTV, hTry, sstring, mode, row))
322             return hTry;
323         hLast = hTry;
324     }
325     return NULL;
326 }
327
328 static BOOL RefreshTreeItem(HWND hwndTV, HTREEITEM hItem)
329 {
330     HKEY hRoot, hKey, hSubKey;
331     HTREEITEM childItem;
332     LPTSTR KeyPath;
333     DWORD dwCount, dwIndex, dwMaxSubKeyLen;
334     LPSTR Name;
335     TVITEM tvItem;
336     
337     hRoot = NULL;
338     KeyPath = GetItemPath(hwndTV, hItem, &hRoot);
339
340     if (!KeyPath || !hRoot)
341         return FALSE;
342
343     if (*KeyPath) {
344         if (RegOpenKeyEx(hRoot, KeyPath, 0, KEY_READ, &hKey) != ERROR_SUCCESS) {
345             WINE_TRACE("RegOpenKeyEx failed, \"%s\" was probably removed.\n", KeyPath);
346             return FALSE;
347         }
348     } else {
349         hKey = hRoot;
350     }
351     HeapFree(GetProcessHeap(), 0, KeyPath);
352
353     if (RegQueryInfoKey(hKey, 0, 0, 0, &dwCount, &dwMaxSubKeyLen, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
354         return FALSE;
355     }
356
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)) {
362         return FALSE;
363     }
364
365     /* We don't have to bother with the rest if it's not expanded. */
366     if (TreeView_GetItemState(hwndTV, hItem, TVIS_EXPANDED) == 0) {
367         RegCloseKey(hKey);
368         return TRUE;
369     }
370
371     dwMaxSubKeyLen++; /* account for the \0 terminator */
372     if (!(Name = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(TCHAR)))) {
373         return FALSE;
374     }
375     tvItem.cchTextMax = dwMaxSubKeyLen;
376     if (!(tvItem.pszText = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(TCHAR)))) {
377         return FALSE;
378     }
379
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;
383         BOOL found;
384
385         found = FALSE;
386         if (RegEnumKeyEx(hKey, dwIndex, Name, &cName, 0, 0, 0, NULL) != ERROR_SUCCESS) {
387             continue;
388         }
389
390         /* Find the number of children of the node. */
391         dwSubCount = 0;
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) {
394                 dwSubCount = 0;
395             }
396             RegCloseKey(hSubKey);
397         }
398
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)) {
405                 return FALSE;
406             }
407
408             if (!stricmp(tvItem.pszText, Name)) {
409                 found = TRUE;
410                 break;
411             }
412         }
413
414         if (found == FALSE) {
415             WINE_TRACE("New subkey %s\n", Name);
416             AddEntryToTree(hwndTV, hItem, Name, NULL, dwSubCount);
417         }
418     }
419     HeapFree(GetProcessHeap(), 0, Name);
420     HeapFree(GetProcessHeap(), 0, tvItem.pszText);
421     RegCloseKey(hKey);
422
423     /* Now go through all the children in the tree, and check if any have to be removed. */
424     childItem = TreeView_GetChild(hwndTV, hItem);
425     while (childItem) {
426         HTREEITEM nextItem = TreeView_GetNextSibling(hwndTV, childItem);
427         if (RefreshTreeItem(hwndTV, childItem) == FALSE) {
428             SendMessage(hwndTV, TVM_DELETEITEM, 0, (LPARAM)childItem);
429         }
430         childItem = nextItem;
431     }
432
433     return TRUE;
434 }
435
436 BOOL RefreshTreeView(HWND hwndTV)
437 {
438     HTREEITEM hItem;
439     HTREEITEM hSelectedItem;
440     HCURSOR hcursorOld;
441
442     WINE_TRACE("\n");
443     hSelectedItem = TreeView_GetSelection(hwndTV);
444     hcursorOld = SetCursor(LoadCursor(NULL, IDC_WAIT));
445     SendMessage(hwndTV, WM_SETREDRAW, FALSE, 0);
446
447     hItem = TreeView_GetChild(hwndTV, TreeView_GetRoot(hwndTV));
448     while (hItem) {
449         RefreshTreeItem(hwndTV, hItem);
450         hItem = TreeView_GetNextSibling(hwndTV, hItem);
451     }
452
453     SendMessage(hwndTV, WM_SETREDRAW, TRUE, 0);
454     InvalidateRect(hwndTV, NULL, FALSE);
455     SetCursor(hcursorOld);
456     
457     /* We reselect the currently selected node, this will prompt a refresh of the listview. */
458     SendMessage(hwndTV, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hSelectedItem);
459     return TRUE;
460 }
461
462 HTREEITEM InsertNode(HWND hwndTV, HTREEITEM hItem, LPTSTR name)
463 {
464     TCHAR buf[MAX_NEW_KEY_LEN];
465     HTREEITEM hNewItem = 0;
466     TVITEMEX item;
467
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);
472     } else {
473         item.mask = TVIF_CHILDREN | TVIF_HANDLE;
474         item.hItem = hItem;
475         if (!TreeView_GetItem(hwndTV, &item)) return FALSE;
476         item.cChildren = 1;
477         if (!TreeView_SetItem(hwndTV, &item)) return FALSE;
478     }
479     SendMessage(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)hItem );
480     if (!hNewItem) {
481         for(hNewItem = TreeView_GetChild(hwndTV, hItem); hNewItem; hNewItem = TreeView_GetNextSibling(hwndTV, hNewItem)) {
482             item.mask = TVIF_HANDLE | TVIF_TEXT;
483             item.hItem = hNewItem;
484             item.pszText = buf;
485             item.cchTextMax = COUNT_OF(buf);
486             if (!TreeView_GetItem(hwndTV, &item)) continue;
487             if (lstrcmp(name, item.pszText) == 0) break;
488         }       
489     }
490     if (hNewItem)
491         SendMessage(hwndTV, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hNewItem);
492
493     return hNewItem;
494 }
495
496 HWND StartKeyRename(HWND hwndTV)
497 {
498     HTREEITEM hItem;
499
500     if(!(hItem = TreeView_GetSelection(hwndTV))) return 0;
501     return TreeView_EditLabel(hwndTV, hItem);
502 }
503
504 static BOOL InitTreeViewItems(HWND hwndTV, LPTSTR pHostName)
505 {
506     TVINSERTSTRUCT tvins;
507     HTREEITEM hRoot;
508     static TCHAR hkcr[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0},
509                  hkcu[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0},
510                  hklm[] = {'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0},
511                  hku[]  = {'H','K','E','Y','_','U','S','E','R','S',0},
512                  hkcc[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0},
513                  hkdd[] = {'H','K','E','Y','_','D','Y','N','_','D','A','T','A',0};
514
515     tvins.u.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
516     /* Set the text of the item.  */
517     tvins.u.item.pszText = pHostName;
518     tvins.u.item.cchTextMax = lstrlen(pHostName);
519     /* Assume the item is not a parent item, so give it an image.  */
520     tvins.u.item.iImage = Image_Root;
521     tvins.u.item.iSelectedImage = Image_Root;
522     tvins.u.item.cChildren = 5;
523     /* Save the heading level in the item's application-defined data area.  */
524     tvins.u.item.lParam = (LPARAM)NULL;
525     tvins.hInsertAfter = (HTREEITEM)TVI_FIRST;
526     tvins.hParent = TVI_ROOT;
527     /* Add the item to the tree view control.  */
528     if (!(hRoot = TreeView_InsertItem(hwndTV, &tvins))) return FALSE;
529
530     if (!AddEntryToTree(hwndTV, hRoot, hkcr, HKEY_CLASSES_ROOT, 1)) return FALSE;
531     if (!AddEntryToTree(hwndTV, hRoot, hkcu, HKEY_CURRENT_USER, 1)) return FALSE;
532     if (!AddEntryToTree(hwndTV, hRoot, hklm, HKEY_LOCAL_MACHINE, 1)) return FALSE;
533     if (!AddEntryToTree(hwndTV, hRoot, hku, HKEY_USERS, 1)) return FALSE;
534     if (!AddEntryToTree(hwndTV, hRoot, hkcc, HKEY_CURRENT_CONFIG, 1)) return FALSE;
535     if (!AddEntryToTree(hwndTV, hRoot, hkdd, HKEY_DYN_DATA, 1)) return FALSE;
536
537     /* expand and select host name */
538     SendMessage(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)hRoot );
539     SendMessage(hwndTV, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hRoot);
540     return TRUE;
541 }
542
543
544 /*
545  * InitTreeViewImageLists - creates an image list, adds three bitmaps
546  * to it, and associates the image list with a tree view control.
547  * Returns TRUE if successful, or FALSE otherwise.
548  * hwndTV - handle to the tree view control.
549  */
550 static BOOL InitTreeViewImageLists(HWND hwndTV)
551 {
552     HIMAGELIST himl;  /* handle to image list  */
553     HICON hico;       /* handle to icon  */
554
555     /* Create the image list.  */
556     if ((himl = ImageList_Create(CX_ICON, CY_ICON,
557                                  ILC_MASK, 0, NUM_ICONS)) == NULL)
558         return FALSE;
559
560     /* Add the open file, closed file, and document bitmaps.  */
561     hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_OPEN_FILE));
562     Image_Open = ImageList_AddIcon(himl, hico);
563
564     hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_CLOSED_FILE));
565     Image_Closed = ImageList_AddIcon(himl, hico);
566
567     hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_ROOT));
568     Image_Root = ImageList_AddIcon(himl, hico);
569
570     /* Fail if not all of the images were added.  */
571     if (ImageList_GetImageCount(himl) < NUM_ICONS)
572     {
573       return FALSE;
574     }
575
576     /* Associate the image list with the tree view control.  */
577     SendMessage(hwndTV, TVM_SETIMAGELIST, TVSIL_NORMAL, (LPARAM)himl);
578
579     return TRUE;
580 }
581
582 BOOL UpdateExpandingTree(HWND hwndTV, HTREEITEM hItem, int state)
583 {
584     DWORD dwCount, dwIndex, dwMaxSubKeyLen;
585     HKEY hRoot, hNewKey, hKey;
586     LPTSTR keyPath;
587     LPTSTR Name;
588     LONG errCode;
589     HCURSOR hcursorOld;
590
591     static int expanding;
592     if (expanding) return FALSE;
593     if (state & TVIS_EXPANDEDONCE ) {
594         return TRUE;
595     }
596     expanding = TRUE;
597     hcursorOld = SetCursor(LoadCursor(NULL, IDC_WAIT));
598     SendMessage(hwndTV, WM_SETREDRAW, FALSE, 0);
599
600     keyPath = GetItemPath(hwndTV, hItem, &hRoot);
601     if (!keyPath) goto done;
602
603     if (*keyPath) {
604         errCode = RegOpenKeyEx(hRoot, keyPath, 0, KEY_READ, &hNewKey);
605         if (errCode != ERROR_SUCCESS) goto done;
606     } else {
607         hNewKey = hRoot;
608     }
609
610     errCode = RegQueryInfoKey(hNewKey, 0, 0, 0, &dwCount, &dwMaxSubKeyLen, 0, 0, 0, 0, 0, 0);
611     if (errCode != ERROR_SUCCESS) goto done;
612     dwMaxSubKeyLen++; /* account for the \0 terminator */
613     Name = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(TCHAR));
614     if (!Name) goto done;
615
616     for (dwIndex = 0; dwIndex < dwCount; dwIndex++) {
617         DWORD cName = dwMaxSubKeyLen, dwSubCount;
618
619         errCode = RegEnumKeyEx(hNewKey, dwIndex, Name, &cName, 0, 0, 0, 0);
620         if (errCode != ERROR_SUCCESS) continue;
621         errCode = RegOpenKeyEx(hNewKey, Name, 0, KEY_QUERY_VALUE, &hKey);
622         if (errCode == ERROR_SUCCESS) {
623             errCode = RegQueryInfoKey(hKey, 0, 0, 0, &dwSubCount, 0, 0, 0, 0, 0, 0, 0);
624             RegCloseKey(hKey);
625         }
626         if (errCode != ERROR_SUCCESS) dwSubCount = 0;
627         AddEntryToTree(hwndTV, hItem, Name, NULL, dwSubCount);
628     }
629     RegCloseKey(hNewKey);
630     HeapFree(GetProcessHeap(), 0, Name);
631
632 done:
633     TreeView_SetItemState(hwndTV, hItem, TVIS_EXPANDEDONCE, TVIS_EXPANDEDONCE);
634     SendMessage(hwndTV, WM_SETREDRAW, TRUE, 0);
635     SetCursor(hcursorOld);
636     expanding = FALSE;
637     HeapFree(GetProcessHeap(), 0, keyPath);
638
639     return TRUE;
640 }
641
642 BOOL OnTreeExpanding(HWND hwndTV, NMTREEVIEW* pnmtv)
643 {
644     return UpdateExpandingTree(hwndTV, pnmtv->itemNew.hItem, pnmtv->itemNew.state);
645 }
646
647
648 /*
649  * CreateTreeView - creates a tree view control.
650  * Returns the handle to the new control if successful, or NULL otherwise.
651  * hwndParent - handle to the control's parent window.
652  */
653 HWND CreateTreeView(HWND hwndParent, LPTSTR pHostName, UINT id)
654 {
655     RECT rcClient;
656     HWND hwndTV;
657
658     /* Get the dimensions of the parent window's client area, and create the tree view control.  */
659     GetClientRect(hwndParent, &rcClient);
660     hwndTV = CreateWindowEx(WS_EX_CLIENTEDGE, WC_TREEVIEW, _T("Tree View"),
661                             WS_VISIBLE | WS_CHILD | WS_TABSTOP | TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT,
662                             0, 0, rcClient.right, rcClient.bottom,
663                             hwndParent, (HMENU)ULongToHandle(id), hInst, NULL);
664     /* Initialize the image list, and add items to the control.  */
665     if (!InitTreeViewImageLists(hwndTV) || !InitTreeViewItems(hwndTV, pHostName)) {
666         DestroyWindow(hwndTV);
667         return NULL;
668     }
669     return hwndTV;
670 }