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