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