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