6c9455b924cbd9832ef7acd63aeffac73fc45926
[reactos.git] / reactos / subsys / system / 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 <process.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <tchar.h>
33
34 #include "main.h"
35
36 /* Global variables and constants */
37 /* Image_Open, Image_Closed, and Image_Root - integer variables for indexes of the images. */
38 /* CX_ICON and CY_ICON - width and height of an icon. */
39 /* NUM_ICON - number of icons to add to the image list. */
40 int Image_Open;
41 int Image_Closed;
42 int Image_Root;
43
44 static LPTSTR pathBuffer;
45
46 #define CX_ICON 16
47 #define CY_ICON 16
48 #define NUM_ICONS 3
49
50 static BOOL get_item_path(HWND hwndTV, HTREEITEM hItem, HKEY* phKey, LPTSTR* pKeyPath, int* pPathLen, int* pMaxLen)
51 {
52 TVITEM item;
53 int maxLen, len;
54 LPTSTR newStr;
55
56 item.mask = TVIF_PARAM;
57 item.hItem = hItem;
58 if (!TreeView_GetItem(hwndTV, &item)) return FALSE;
59
60 if (item.lParam) {
61 /* found root key with valid key value */
62 *phKey = (HKEY)item.lParam;
63 return TRUE;
64 }
65
66 if(!get_item_path(hwndTV, TreeView_GetParent(hwndTV, hItem), phKey, pKeyPath, pPathLen, pMaxLen)) return FALSE;
67 if (*pPathLen) {
68 (*pKeyPath)[*pPathLen] = _T('\\');
69 ++(*pPathLen);
70 }
71
72 do {
73 item.mask = TVIF_TEXT;
74 item.hItem = hItem;
75 item.pszText = *pKeyPath + *pPathLen;
76 item.cchTextMax = maxLen = *pMaxLen - *pPathLen;
77 if (!TreeView_GetItem(hwndTV, &item)) return FALSE;
78 len = _tcslen(item.pszText);
79 if (len < maxLen - 1) {
80 *pPathLen += len;
81 break;
82 }
83 newStr = HeapReAlloc(GetProcessHeap(), 0, *pKeyPath, *pMaxLen * 2);
84 if (!newStr) return FALSE;
85 *pKeyPath = newStr;
86 *pMaxLen *= 2;
87 } while(TRUE);
88
89 return TRUE;
90 }
91
92 LPCTSTR GetItemPath(HWND hwndTV, HTREEITEM hItem, HKEY* phRootKey)
93 {
94 int pathLen = 0, maxLen;
95
96 *phRootKey = NULL;
97 if (!pathBuffer) pathBuffer = HeapAlloc(GetProcessHeap(), 0, 1024);
98 if (!pathBuffer) return NULL;
99 *pathBuffer = 0;
100 maxLen = HeapSize(GetProcessHeap(), 0, pathBuffer);
101 if (maxLen == -1) return NULL;
102 if (!hItem) hItem = TreeView_GetSelection(hwndTV);
103 if (!hItem) return NULL;
104 if (!get_item_path(hwndTV, hItem, phRootKey, &pathBuffer, &pathLen, &maxLen)) return NULL;
105 return pathBuffer;
106 }
107
108 /* Add an entry to the tree. Only give hKey for root nodes (HKEY_ constants) */
109 static HTREEITEM AddEntryToTree(HWND hwndTV, HTREEITEM hParent, LPTSTR label, HKEY hKey, DWORD dwChildren)
110 {
111 TVITEM tvi;
112 TVINSERTSTRUCT tvins;
113
114 if (hKey) {
115 if (RegQueryInfoKey(hKey, 0, 0, 0, &dwChildren, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
116 dwChildren = 0;
117 }
118 }
119
120 tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
121 tvi.pszText = label;
122 tvi.cchTextMax = lstrlen(tvi.pszText);
123 tvi.iImage = Image_Closed;
124 tvi.iSelectedImage = Image_Open;
125 tvi.cChildren = dwChildren;
126 tvi.lParam = (LPARAM)hKey;
127 tvins.u.item = tvi;
128 tvins.hInsertAfter = (HTREEITEM)(hKey ? TVI_LAST : TVI_FIRST);
129 tvins.hParent = hParent;
130 return TreeView_InsertItem(hwndTV, &tvins);
131 }
132
133 static BOOL RefreshTreeItem(HWND hwndTV, HTREEITEM hItem)
134 {
135 HKEY hRoot, hKey, hSubKey;
136 HTREEITEM childItem;
137 LPCTSTR KeyPath;
138 DWORD dwCount, dwIndex, dwMaxSubKeyLen;
139 LPTSTR Name = NULL;
140 TVITEM tvItem;
141 LPTSTR pszNodes = NULL;
142 BOOL bSuccess = FALSE;
143 LPTSTR s;
144 BOOL bAddedAny;
145
146 KeyPath = GetItemPath(hwndTV, hItem, &hRoot);
147
148 if (*KeyPath) {
149 if (RegOpenKeyEx(hRoot, KeyPath, 0, KEY_READ, &hKey) != ERROR_SUCCESS) {
150 goto done;
151 }
152 } else {
153 hKey = hRoot;
154 }
155
156 if (RegQueryInfoKey(hKey, 0, 0, 0, &dwCount, &dwMaxSubKeyLen, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
157 goto done;
158 }
159
160 /* Set the number of children again */
161 tvItem.mask = TVIF_CHILDREN;
162 tvItem.hItem = hItem;
163 tvItem.cChildren = dwCount;
164 if (!TreeView_SetItem(hwndTV, &tvItem)) {
165 goto done;
166 }
167
168 /* We don't have to bother with the rest if it's not expanded. */
169 if (TreeView_GetItemState(hwndTV, hItem, TVIS_EXPANDED) == 0) {
170 RegCloseKey(hKey);
171 bSuccess = TRUE;
172 goto done;
173 }
174
175 dwMaxSubKeyLen++; /* account for the \0 terminator */
176 if (!(Name = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(TCHAR)))) {
177 goto done;
178 }
179 tvItem.cchTextMax = dwMaxSubKeyLen;
180 if (!(tvItem.pszText = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(TCHAR)))) {
181 goto done;
182 }
183
184 /* Get all of the tree node siblings in one contiguous block of memory */
185 {
186 DWORD dwPhysicalSize = 0;
187 DWORD dwActualSize = 0;
188 DWORD dwNewPhysicalSize;
189 LPTSTR pszNewNodes;
190 DWORD dwStep = 10000;
191
192 for (childItem = TreeView_GetChild(hwndTV, hItem); childItem;
193 childItem = TreeView_GetNextSibling(hwndTV, childItem)) {
194
195 if (dwActualSize + dwMaxSubKeyLen + 1 > dwPhysicalSize)
196 {
197 dwNewPhysicalSize = dwActualSize + dwMaxSubKeyLen + 1 + dwStep;
198
199 if (pszNodes)
200 pszNewNodes = (LPTSTR) HeapReAlloc(GetProcessHeap(), 0, pszNodes, dwNewPhysicalSize * sizeof(TCHAR));
201 else
202 pszNewNodes = (LPTSTR) HeapAlloc(GetProcessHeap(), 0, dwNewPhysicalSize * sizeof(TCHAR));
203 if (!pszNewNodes)
204 goto done;
205
206 dwPhysicalSize = dwNewPhysicalSize;
207 pszNodes = pszNewNodes;
208 }
209
210 tvItem.mask = TVIF_TEXT;
211 tvItem.hItem = childItem;
212 tvItem.pszText = &pszNodes[dwActualSize];
213 tvItem.cchTextMax = dwPhysicalSize - dwActualSize;
214 if (!TreeView_GetItem(hwndTV, &tvItem))
215 goto done;
216
217 dwActualSize += _tcslen(&pszNodes[dwActualSize]) + 1;
218 }
219
220 if (pszNodes)
221 pszNodes[dwActualSize] = '\0';
222 }
223
224 /* Now go through all the children in the registry, and check if any have to be added. */
225 bAddedAny = FALSE;
226 for (dwIndex = 0; dwIndex < dwCount; dwIndex++) {
227 DWORD cName = dwMaxSubKeyLen, dwSubCount;
228 BOOL found;
229
230 found = FALSE;
231 if (RegEnumKeyEx(hKey, dwIndex, Name, &cName, 0, 0, 0, NULL) != ERROR_SUCCESS) {
232 continue;
233 }
234
235 /* Check if the node is already in there. */
236 if (pszNodes) {
237 for (s = pszNodes; *s; s += _tcslen(s) + 1) {
238 if (!_tcscmp(s, Name)) {
239 found = TRUE;
240 break;
241 }
242 }
243 }
244
245 if (found == FALSE) {
246 /* Find the number of children of the node. */
247 dwSubCount = 0;
248 if (RegOpenKeyEx(hKey, Name, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
249 if (RegQueryInfoKey(hSubKey, 0, 0, 0, &dwSubCount, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
250 dwSubCount = 0;
251 }
252 RegCloseKey(hSubKey);
253 }
254
255 AddEntryToTree(hwndTV, hItem, Name, NULL, dwSubCount);
256 bAddedAny = TRUE;
257 }
258 }
259 RegCloseKey(hKey);
260
261 if (bAddedAny)
262 SendMessage(hwndTV, TVM_SORTCHILDREN, 0, (LPARAM) hItem);
263
264 /* Now go through all the children in the tree, and check if any have to be removed. */
265 childItem = TreeView_GetChild(hwndTV, hItem);
266 while (childItem) {
267 HTREEITEM nextItem = TreeView_GetNextSibling(hwndTV, childItem);
268 if (RefreshTreeItem(hwndTV, childItem) == FALSE) {
269 TreeView_DeleteItem(hwndTV, childItem);
270 }
271 childItem = nextItem;
272 }
273 bSuccess = TRUE;
274
275 done:
276 if (pszNodes)
277 HeapFree(GetProcessHeap(), 0, pszNodes);
278 if (Name)
279 HeapFree(GetProcessHeap(), 0, Name);
280 return bSuccess;
281 }
282
283 BOOL RefreshTreeView(HWND hwndTV)
284 {
285 HTREEITEM hItem;
286 HTREEITEM hSelectedItem;
287 HCURSOR hcursorOld;
288
289 hSelectedItem = TreeView_GetSelection(hwndTV);
290 hcursorOld = SetCursor(LoadCursor(NULL, IDC_WAIT));
291 SendMessage(hwndTV, WM_SETREDRAW, FALSE, 0);
292
293 hItem = TreeView_GetChild(hwndTV, TreeView_GetRoot(hwndTV));
294 while (hItem) {
295 RefreshTreeItem(hwndTV, hItem);
296 hItem = TreeView_GetNextSibling(hwndTV, hItem);
297 }
298
299 SendMessage(hwndTV, WM_SETREDRAW, TRUE, 0);
300 SetCursor(hcursorOld);
301
302 /* We reselect the currently selected node, this will prompt a refresh of the listview. */
303 TreeView_SelectItem(hwndTV, hSelectedItem);
304 return TRUE;
305 }
306
307 HTREEITEM InsertNode(HWND hwndTV, HTREEITEM hItem, LPTSTR name)
308 {
309 TCHAR buf[MAX_NEW_KEY_LEN];
310 HTREEITEM hNewItem = 0;
311 TVITEMEX item;
312
313 if (!hItem) hItem = TreeView_GetSelection(hwndTV);
314 if (!hItem) return FALSE;
315 if (TreeView_GetItemState(hwndTV, hItem, TVIS_EXPANDEDONCE)) {
316 hNewItem = AddEntryToTree(hwndTV, hItem, name, 0, 0);
317 SendMessage(hwndTV, TVM_SORTCHILDREN, 0, (LPARAM) hItem);
318 } else {
319 item.mask = TVIF_CHILDREN | TVIF_HANDLE;
320 item.hItem = hItem;
321 if (!TreeView_GetItem(hwndTV, &item)) return FALSE;
322 item.cChildren = 1;
323 if (!TreeView_SetItem(hwndTV, &item)) return FALSE;
324 }
325 TreeView_Expand(hwndTV, hItem, TVE_EXPAND);
326 if (!hNewItem) {
327 for(hNewItem = TreeView_GetChild(hwndTV, hItem); hNewItem; hNewItem = TreeView_GetNextSibling(hwndTV, hNewItem)) {
328 item.mask = TVIF_HANDLE | TVIF_TEXT;
329 item.hItem = hNewItem;
330 item.pszText = buf;
331 item.cchTextMax = COUNT_OF(buf);
332 if (!TreeView_GetItem(hwndTV, &item)) continue;
333 if (lstrcmp(name, item.pszText) == 0) break;
334 }
335 }
336 if (hNewItem) TreeView_SelectItem(hwndTV, hNewItem);
337
338 return hNewItem;
339 }
340
341 HWND StartKeyRename(HWND hwndTV)
342 {
343 HTREEITEM hItem;
344
345 if(!(hItem = TreeView_GetSelection(hwndTV))) return 0;
346 return TreeView_EditLabel(hwndTV, hItem);
347 }
348
349 static BOOL InitTreeViewItems(HWND hwndTV, LPTSTR pHostName)
350 {
351 TVITEM tvi;
352 TVINSERTSTRUCT tvins;
353 HTREEITEM hRoot;
354
355 tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
356 /* Set the text of the item. */
357 tvi.pszText = pHostName;
358 tvi.cchTextMax = lstrlen(tvi.pszText);
359 /* Assume the item is not a parent item, so give it an image. */
360 tvi.iImage = Image_Root;
361 tvi.iSelectedImage = Image_Root;
362 tvi.cChildren = 5;
363 /* Save the heading level in the item's application-defined data area. */
364 tvi.lParam = (LPARAM)NULL;
365 tvins.u.item = tvi;
366 tvins.hInsertAfter = (HTREEITEM)TVI_FIRST;
367 tvins.hParent = TVI_ROOT;
368 /* Add the item to the tree view control. */
369 if (!(hRoot = TreeView_InsertItem(hwndTV, &tvins))) return FALSE;
370
371 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_CLASSES_ROOT"), HKEY_CLASSES_ROOT, 1)) return FALSE;
372 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_CURRENT_USER"), HKEY_CURRENT_USER, 1)) return FALSE;
373 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_LOCAL_MACHINE"), HKEY_LOCAL_MACHINE, 1)) return FALSE;
374 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_USERS"), HKEY_USERS, 1)) return FALSE;
375 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_CURRENT_CONFIG"), HKEY_CURRENT_CONFIG, 1)) return FALSE;
376
377 if (GetVersion() & 0x80000000)
378 {
379 /* Win9x specific key */
380 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_DYN_DATA"), HKEY_DYN_DATA, 1)) return FALSE;
381 }
382
383 /* expand and select host name */
384 TreeView_Expand(hwndTV, hRoot, TVE_EXPAND);
385 TreeView_Select(hwndTV, hRoot, TVGN_CARET);
386 return TRUE;
387 }
388
389
390 /*
391 * InitTreeViewImageLists - creates an image list, adds three bitmaps
392 * to it, and associates the image list with a tree view control.
393 * Returns TRUE if successful, or FALSE otherwise.
394 * hwndTV - handle to the tree view control.
395 */
396 static BOOL InitTreeViewImageLists(HWND hwndTV)
397 {
398 HIMAGELIST himl; /* handle to image list */
399 HICON hico; /* handle to icon */
400
401 /* Create the image list. */
402 if ((himl = ImageList_Create(CX_ICON, CY_ICON,
403 ILC_MASK, 0, NUM_ICONS)) == NULL)
404 return FALSE;
405
406 /* Add the open file, closed file, and document bitmaps. */
407 hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_OPEN_FILE));
408 Image_Open = ImageList_AddIcon(himl, hico);
409
410 hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_CLOSED_FILE));
411 Image_Closed = ImageList_AddIcon(himl, hico);
412
413 hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_ROOT));
414 Image_Root = ImageList_AddIcon(himl, hico);
415
416 /* Fail if not all of the images were added. */
417 if (ImageList_GetImageCount(himl) < NUM_ICONS)
418 {
419 return FALSE;
420 }
421
422 /* Associate the image list with the tree view control. */
423 TreeView_SetImageList(hwndTV, himl, TVSIL_NORMAL);
424
425 return TRUE;
426 }
427
428 BOOL OnTreeExpanding(HWND hwndTV, NMTREEVIEW* pnmtv)
429 {
430 DWORD dwCount, dwIndex, dwMaxSubKeyLen;
431 HKEY hRoot, hNewKey, hKey;
432 LPCTSTR keyPath;
433 LPTSTR Name;
434 LONG errCode;
435 HCURSOR hcursorOld;
436
437 static int expanding;
438 if (expanding) return FALSE;
439 if (pnmtv->itemNew.state & TVIS_EXPANDEDONCE ) {
440 return TRUE;
441 }
442 expanding = TRUE;
443 hcursorOld = SetCursor(LoadCursor(NULL, IDC_WAIT));
444 SendMessage(hwndTV, WM_SETREDRAW, FALSE, 0);
445
446 keyPath = GetItemPath(hwndTV, pnmtv->itemNew.hItem, &hRoot);
447 if (!keyPath) goto done;
448
449 if (*keyPath) {
450 errCode = RegOpenKeyEx(hRoot, keyPath, 0, KEY_READ, &hNewKey);
451 if (errCode != ERROR_SUCCESS) goto done;
452 } else {
453 hNewKey = hRoot;
454 }
455
456 errCode = RegQueryInfoKey(hNewKey, 0, 0, 0, &dwCount, &dwMaxSubKeyLen, 0, 0, 0, 0, 0, 0);
457 if (errCode != ERROR_SUCCESS) goto done;
458 dwMaxSubKeyLen++; /* account for the \0 terminator */
459 Name = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(TCHAR));
460 if (!Name) goto done;
461
462 for (dwIndex = 0; dwIndex < dwCount; dwIndex++) {
463 DWORD cName = dwMaxSubKeyLen, dwSubCount;
464
465 errCode = RegEnumKeyEx(hNewKey, dwIndex, Name, &cName, 0, 0, 0, 0);
466 if (errCode != ERROR_SUCCESS) continue;
467 errCode = RegOpenKeyEx(hNewKey, Name, 0, KEY_QUERY_VALUE, &hKey);
468 if (errCode == ERROR_SUCCESS) {
469 errCode = RegQueryInfoKey(hKey, 0, 0, 0, &dwSubCount, 0, 0, 0, 0, 0, 0, 0);
470 RegCloseKey(hKey);
471 }
472 if (errCode != ERROR_SUCCESS) dwSubCount = 0;
473 AddEntryToTree(hwndTV, pnmtv->itemNew.hItem, Name, NULL, dwSubCount);
474 }
475
476 SendMessage(hwndTV, TVM_SORTCHILDREN, 0, (LPARAM)pnmtv->itemNew.hItem);
477
478 RegCloseKey(hNewKey);
479 HeapFree(GetProcessHeap(), 0, Name);
480
481 done:
482 SendMessage(hwndTV, WM_SETREDRAW, TRUE, 0);
483 SetCursor(hcursorOld);
484 expanding = FALSE;
485
486 return TRUE;
487 }
488
489
490 BOOL CreateNewKey(HWND hwndTV, HTREEITEM hItem)
491 {
492 TCHAR szNewKeyFormat[128];
493 TCHAR szNewKey[128];
494 LPCTSTR pszKeyPath;
495 int iIndex = 1;
496 HKEY hRootKey;
497 HKEY hKey = NULL;
498 HKEY hNewKey = NULL;
499 BOOL bSuccess = FALSE;
500 LONG lResult;
501 DWORD dwDisposition;
502 HTREEITEM hNewItem;
503
504 pszKeyPath = GetItemPath(g_pChildWnd->hTreeWnd, hItem, &hRootKey);
505 if (RegOpenKey(hRootKey, pszKeyPath, &hKey) != ERROR_SUCCESS)
506 goto done;
507
508 if (LoadString(hInst, IDS_NEW_KEY, szNewKeyFormat, sizeof(szNewKeyFormat) / sizeof(szNewKeyFormat[0])) <= 0)
509 goto done;
510
511 do
512 {
513 _sntprintf(szNewKey, sizeof(szNewKey) / sizeof(szNewKey[0]), szNewKeyFormat, iIndex++);
514 lResult = RegCreateKeyEx(hKey, szNewKey, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hNewKey, &dwDisposition);
515 if (hNewKey && (dwDisposition == REG_OPENED_EXISTING_KEY))
516 {
517 RegCloseKey(hNewKey);
518 hNewKey = NULL;
519 }
520 }
521 while(!hNewKey);
522
523 hNewItem = AddEntryToTree(hwndTV, hItem, szNewKey, NULL, 0);
524 if (!hNewItem)
525 goto done;
526 SendMessage(hwndTV, TVM_SORTCHILDREN, 0, (LPARAM) hItem);
527 TreeView_EditLabel(hwndTV, hNewItem);
528
529 bSuccess = TRUE;
530
531 done:
532 if (hKey)
533 RegCloseKey(hKey);
534 if (hNewKey)
535 RegCloseKey(hNewKey);
536 return bSuccess;
537 }
538
539
540 /*
541 * CreateTreeView - creates a tree view control.
542 * Returns the handle to the new control if successful, or NULL otherwise.
543 * hwndParent - handle to the control's parent window.
544 */
545 HWND CreateTreeView(HWND hwndParent, LPTSTR pHostName, int id)
546 {
547 RECT rcClient;
548 HWND hwndTV;
549
550 /* Get the dimensions of the parent window's client area, and create the tree view control. */
551 GetClientRect(hwndParent, &rcClient);
552 hwndTV = CreateWindowEx(WS_EX_CLIENTEDGE, WC_TREEVIEW, NULL,
553 WS_VISIBLE | WS_CHILD | WS_TABSTOP | TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT | TVS_EDITLABELS,
554 0, 0, rcClient.right, rcClient.bottom,
555 hwndParent, (HMENU)id, hInst, NULL);
556 /* Initialize the image list, and add items to the control. */
557 if (!InitTreeViewImageLists(hwndTV) || !InitTreeViewItems(hwndTV, pHostName)) {
558 DestroyWindow(hwndTV);
559 return NULL;
560 }
561 return hwndTV;
562 }
563
564 BOOL SelectNode(HWND hwndTV, LPCTSTR keyPath)
565 {
566 HTREEITEM hRoot, hItem;
567 HTREEITEM hChildItem;
568 TCHAR szPathPart[128];
569 TCHAR szBuffer[128];
570 LPCTSTR s;
571 TVITEM tvi;
572
573 hRoot = TreeView_GetRoot(hwndTV);
574 hItem = hRoot;
575
576 while(keyPath[0])
577 {
578 s = _tcschr(keyPath, '\\');
579 lstrcpyn(szPathPart, keyPath, s ? s - keyPath + 1 : _tcslen(keyPath) + 1);
580
581 /* Special case for root to expand root key abbreviations */
582 if (hItem == hRoot)
583 {
584 if (!_tcscmp(szPathPart, TEXT("HKCR")))
585 _tcscpy(szPathPart, TEXT("HKEY_CLASSES_ROOT"));
586 else if (!_tcscmp(szPathPart, TEXT("HKCU")))
587 _tcscpy(szPathPart, TEXT("HKEY_CURRENT_USER"));
588 else if (!_tcscmp(szPathPart, TEXT("HKLM")))
589 _tcscpy(szPathPart, TEXT("HKEY_LOCAL_MACHINE"));
590 else if (!_tcscmp(szPathPart, TEXT("HKU")))
591 _tcscpy(szPathPart, TEXT("HKEY_USERS"));
592 else if (!_tcscmp(szPathPart, TEXT("HKCC")))
593 _tcscpy(szPathPart, TEXT("HKEY_CURRENT_CONFIG"));
594 else if (!_tcscmp(szPathPart, TEXT("HKDD")))
595 _tcscpy(szPathPart, TEXT("HKEY_DYN_DATA"));
596 }
597
598 for (hChildItem = TreeView_GetChild(hwndTV, hItem); hChildItem;
599 hChildItem = TreeView_GetNextSibling(hwndTV, hChildItem))
600 {
601 memset(&tvi, 0, sizeof(tvi));
602 tvi.hItem = hChildItem;
603 tvi.mask = TVIF_TEXT | TVIF_CHILDREN;
604 tvi.pszText = szBuffer;
605 tvi.cchTextMax = sizeof(szBuffer) / sizeof(szBuffer[0]);
606
607 TreeView_GetItem(hwndTV, &tvi);
608
609 if (!_tcscmp(szBuffer, szPathPart))
610 break;
611 }
612
613 if (!hChildItem)
614 return FALSE;
615
616 if (tvi.cChildren > 0)
617 {
618 if (!TreeView_Expand(hwndTV, hChildItem, TVE_EXPAND))
619 return FALSE;
620 }
621
622 keyPath = s ? s + 1 : _T("");
623 hItem = hChildItem;
624 }
625
626 TreeView_SelectItem(hwndTV, hItem);
627 TreeView_EnsureVisible(hwndTV, hItem);
628
629 return TRUE;
630 }
631