- Revert 49927 "Update to trunk" as it breaks KsStudio (again)
[reactos.git] / 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 static int Image_Open = 0;
28 static int Image_Closed = 0;
29 static int Image_Root = 0;
30
31 static LPTSTR pathBuffer;
32
33 #define NUM_ICONS 3
34
35 static BOOL get_item_path(HWND hwndTV, HTREEITEM hItem, HKEY* phKey, LPTSTR* pKeyPath, int* pPathLen, int* pMaxLen)
36 {
37 TVITEM item;
38 size_t maxLen, len;
39 LPTSTR newStr;
40
41 item.mask = TVIF_PARAM;
42 item.hItem = hItem;
43 if (!TreeView_GetItem(hwndTV, &item)) return FALSE;
44
45 if (item.lParam) {
46 /* found root key with valid key value */
47 *phKey = (HKEY)item.lParam;
48 return TRUE;
49 }
50
51 if(!get_item_path(hwndTV, TreeView_GetParent(hwndTV, hItem), phKey, pKeyPath, pPathLen, pMaxLen)) return FALSE;
52 if (*pPathLen) {
53 (*pKeyPath)[*pPathLen] = _T('\\');
54 ++(*pPathLen);
55 }
56
57 do {
58 item.mask = TVIF_TEXT;
59 item.hItem = hItem;
60 item.pszText = *pKeyPath + *pPathLen;
61 maxLen = *pMaxLen - *pPathLen;
62 item.cchTextMax = (int) maxLen;
63 if (!TreeView_GetItem(hwndTV, &item)) return FALSE;
64 len = _tcslen(item.pszText);
65 if (len < maxLen - 1) {
66 *pPathLen += (int) len;
67 break;
68 }
69 newStr = HeapReAlloc(GetProcessHeap(), 0, *pKeyPath, *pMaxLen * 2);
70 if (!newStr) return FALSE;
71 *pKeyPath = newStr;
72 *pMaxLen *= 2;
73 } while(TRUE);
74
75 return TRUE;
76 }
77
78 LPCTSTR GetItemPath(HWND hwndTV, HTREEITEM hItem, HKEY* phRootKey)
79 {
80 int pathLen = 0, maxLen;
81
82 *phRootKey = NULL;
83 if (!pathBuffer) pathBuffer = HeapAlloc(GetProcessHeap(), 0, 1024);
84 if (!pathBuffer) return NULL;
85 *pathBuffer = 0;
86 maxLen = (int) HeapSize(GetProcessHeap(), 0, pathBuffer);
87 if (maxLen == -1) return NULL;
88 if (!hItem) hItem = TreeView_GetSelection(hwndTV);
89 if (!hItem) return NULL;
90 if (!get_item_path(hwndTV, hItem, phRootKey, &pathBuffer, &pathLen, &maxLen)) {
91 return NULL;
92 }
93 return pathBuffer;
94 }
95
96 BOOL DeleteNode(HWND hwndTV, HTREEITEM hItem)
97 {
98 if (!hItem) hItem = TreeView_GetSelection(hwndTV);
99 if (!hItem) return FALSE;
100 return TreeView_DeleteItem(hwndTV, hItem);
101 }
102
103 /* Add an entry to the tree. Only give hKey for root nodes (HKEY_ constants) */
104 static HTREEITEM AddEntryToTree(HWND hwndTV, HTREEITEM hParent, LPTSTR label, HKEY hKey, DWORD dwChildren)
105 {
106 TVITEM tvi;
107 TVINSERTSTRUCT tvins;
108
109 if (hKey) {
110 if (RegQueryInfoKey(hKey, 0, 0, 0, &dwChildren, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
111 dwChildren = 0;
112 }
113 }
114
115 tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
116 tvi.pszText = label;
117 tvi.cchTextMax = lstrlen(tvi.pszText);
118 tvi.iImage = Image_Closed;
119 tvi.iSelectedImage = Image_Open;
120 tvi.cChildren = dwChildren;
121 tvi.lParam = (LPARAM)hKey;
122 tvins.item = tvi;
123 tvins.hInsertAfter = (HTREEITEM)(hKey ? TVI_LAST : TVI_FIRST);
124 tvins.hParent = hParent;
125 return TreeView_InsertItem(hwndTV, &tvins);
126 }
127
128 BOOL RefreshTreeItem(HWND hwndTV, HTREEITEM hItem)
129 {
130 HKEY hRoot, hKey, hSubKey;
131 HTREEITEM childItem;
132 LPCTSTR KeyPath;
133 DWORD dwCount, dwIndex, dwMaxSubKeyLen;
134 LPTSTR Name = NULL;
135 TVITEM tvItem;
136 LPTSTR pszNodes = NULL;
137 BOOL bSuccess = FALSE;
138 LPTSTR s;
139 BOOL bAddedAny;
140
141 KeyPath = GetItemPath(hwndTV, hItem, &hRoot);
142
143 if (*KeyPath) {
144 if (RegOpenKeyEx(hRoot, KeyPath, 0, KEY_READ, &hKey) != ERROR_SUCCESS) {
145 goto done;
146 }
147 } else {
148 hKey = hRoot;
149 }
150
151 if (RegQueryInfoKey(hKey, 0, 0, 0, &dwCount, &dwMaxSubKeyLen, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
152 goto done;
153 }
154
155 /* Set the number of children again */
156 tvItem.mask = TVIF_CHILDREN;
157 tvItem.hItem = hItem;
158 tvItem.cChildren = dwCount;
159 if (!TreeView_SetItem(hwndTV, &tvItem)) {
160 goto done;
161 }
162
163 /* We don't have to bother with the rest if it's not expanded. */
164 if (TreeView_GetItemState(hwndTV, hItem, TVIS_EXPANDED) == 0) {
165 RegCloseKey(hKey);
166 bSuccess = TRUE;
167 goto done;
168 }
169
170 dwMaxSubKeyLen++; /* account for the \0 terminator */
171 if (!(Name = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(TCHAR)))) {
172 goto done;
173 }
174 tvItem.cchTextMax = dwMaxSubKeyLen;
175 /*if (!(tvItem.pszText = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(TCHAR)))) {
176 goto done;
177 }*/
178
179 /* Get all of the tree node siblings in one contiguous block of memory */
180 {
181 DWORD dwPhysicalSize = 0;
182 DWORD dwActualSize = 0;
183 DWORD dwNewPhysicalSize;
184 LPTSTR pszNewNodes;
185 DWORD dwStep = 10000;
186
187 for (childItem = TreeView_GetChild(hwndTV, hItem); childItem;
188 childItem = TreeView_GetNextSibling(hwndTV, childItem)) {
189
190 if (dwActualSize + dwMaxSubKeyLen + 1 > dwPhysicalSize)
191 {
192 dwNewPhysicalSize = dwActualSize + dwMaxSubKeyLen + 1 + dwStep;
193
194 if (pszNodes)
195 pszNewNodes = (LPTSTR) HeapReAlloc(GetProcessHeap(), 0, pszNodes, dwNewPhysicalSize * sizeof(TCHAR));
196 else
197 pszNewNodes = (LPTSTR) HeapAlloc(GetProcessHeap(), 0, dwNewPhysicalSize * sizeof(TCHAR));
198 if (!pszNewNodes)
199 goto done;
200
201 dwPhysicalSize = dwNewPhysicalSize;
202 pszNodes = pszNewNodes;
203 }
204
205 tvItem.mask = TVIF_TEXT;
206 tvItem.hItem = childItem;
207 tvItem.pszText = &pszNodes[dwActualSize];
208 tvItem.cchTextMax = dwPhysicalSize - dwActualSize;
209 if (!TreeView_GetItem(hwndTV, &tvItem))
210 goto done;
211
212 dwActualSize += (DWORD) _tcslen(&pszNodes[dwActualSize]) + 1;
213 }
214
215 if (pszNodes)
216 pszNodes[dwActualSize] = '\0';
217 }
218
219 /* Now go through all the children in the tree, and check if any have to be removed. */
220 childItem = TreeView_GetChild(hwndTV, hItem);
221 while (childItem) {
222 HTREEITEM nextItem = TreeView_GetNextSibling(hwndTV, childItem);
223 if (RefreshTreeItem(hwndTV, childItem) == FALSE) {
224 (void)TreeView_DeleteItem(hwndTV, childItem);
225 }
226 childItem = nextItem;
227 }
228
229 /* Now go through all the children in the registry, and check if any have to be added. */
230 bAddedAny = FALSE;
231 for (dwIndex = 0; dwIndex < dwCount; dwIndex++) {
232 DWORD cName = dwMaxSubKeyLen, dwSubCount;
233 BOOL found;
234
235 found = FALSE;
236 if (RegEnumKeyEx(hKey, dwIndex, Name, &cName, 0, 0, 0, NULL) != ERROR_SUCCESS) {
237 continue;
238 }
239
240 /* Check if the node is already in there. */
241 if (pszNodes) {
242 for (s = pszNodes; *s; s += _tcslen(s) + 1) {
243 if (!_tcscmp(s, Name)) {
244 found = TRUE;
245 break;
246 }
247 }
248 }
249
250 if (found == FALSE) {
251 /* Find the number of children of the node. */
252 dwSubCount = 0;
253 if (RegOpenKeyEx(hKey, Name, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
254 if (RegQueryInfoKey(hSubKey, 0, 0, 0, &dwSubCount, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
255 dwSubCount = 0;
256 }
257 RegCloseKey(hSubKey);
258 }
259
260 AddEntryToTree(hwndTV, hItem, Name, NULL, dwSubCount);
261 bAddedAny = TRUE;
262 }
263 }
264 RegCloseKey(hKey);
265
266 if (bAddedAny)
267 SendMessage(hwndTV, TVM_SORTCHILDREN, 0, (LPARAM) hItem);
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 (void)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 (void)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) (void)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 (void)TreeView_Expand(hwndTV, hRoot, TVE_EXPAND);
399 (void)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(GetSystemMetrics(SM_CXSMICON),
417 GetSystemMetrics(SM_CYSMICON),
418 ILC_MASK | ILC_COLOR32,
419 0,
420 NUM_ICONS)) == NULL)
421 {
422 return FALSE;
423 }
424
425 /* Add the open file, closed file, and document bitmaps. */
426 hico = LoadImage(hInst,
427 MAKEINTRESOURCE(IDI_OPEN_FILE),
428 IMAGE_ICON,
429 GetSystemMetrics(SM_CXSMICON),
430 GetSystemMetrics(SM_CYSMICON),
431 0);
432 if (hico)
433 {
434 Image_Open = ImageList_AddIcon(himl, hico);
435 DestroyIcon(hico);
436 }
437
438 hico = LoadImage(hInst,
439 MAKEINTRESOURCE(IDI_CLOSED_FILE),
440 IMAGE_ICON,
441 GetSystemMetrics(SM_CXSMICON),
442 GetSystemMetrics(SM_CYSMICON),
443 0);
444 if (hico)
445 {
446 Image_Closed = ImageList_AddIcon(himl, hico);
447 DestroyIcon(hico);
448 }
449
450 hico = LoadImage(hInst,
451 MAKEINTRESOURCE(IDI_ROOT),
452 IMAGE_ICON,
453 GetSystemMetrics(SM_CXSMICON),
454 GetSystemMetrics(SM_CYSMICON),
455 0);
456 if (hico)
457 {
458 Image_Root = ImageList_AddIcon(himl, hico);
459 DestroyIcon(hico);
460 }
461
462 /* Fail if not all of the images were added. */
463 if (ImageList_GetImageCount(himl) < NUM_ICONS)
464 {
465 ImageList_Destroy(himl);
466 return FALSE;
467 }
468
469 /* Associate the image list with the tree view control. */
470 (void)TreeView_SetImageList(hwndTV, himl, TVSIL_NORMAL);
471
472 return TRUE;
473 }
474
475 BOOL OnTreeExpanding(HWND hwndTV, NMTREEVIEW* pnmtv)
476 {
477 DWORD dwCount, dwIndex, dwMaxSubKeyLen;
478 HKEY hRoot, hNewKey, hKey;
479 LPCTSTR keyPath;
480 LPTSTR Name;
481 LONG errCode;
482 HCURSOR hcursorOld;
483
484 static int expanding;
485 if (expanding) return FALSE;
486 if (pnmtv->itemNew.state & TVIS_EXPANDEDONCE ) {
487 return TRUE;
488 }
489 expanding = TRUE;
490 hcursorOld = SetCursor(LoadCursor(NULL, IDC_WAIT));
491 SendMessage(hwndTV, WM_SETREDRAW, FALSE, 0);
492
493 keyPath = GetItemPath(hwndTV, pnmtv->itemNew.hItem, &hRoot);
494 if (!keyPath) goto done;
495
496 if (*keyPath) {
497 errCode = RegOpenKeyEx(hRoot, keyPath, 0, KEY_READ, &hNewKey);
498 if (errCode != ERROR_SUCCESS) goto done;
499 } else {
500 hNewKey = hRoot;
501 }
502
503 errCode = RegQueryInfoKey(hNewKey, 0, 0, 0, &dwCount, &dwMaxSubKeyLen, 0, 0, 0, 0, 0, 0);
504 if (errCode != ERROR_SUCCESS) goto done;
505 dwMaxSubKeyLen++; /* account for the \0 terminator */
506 Name = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(TCHAR));
507 if (!Name) goto done;
508
509 for (dwIndex = 0; dwIndex < dwCount; dwIndex++) {
510 DWORD cName = dwMaxSubKeyLen, dwSubCount;
511
512 errCode = RegEnumKeyEx(hNewKey, dwIndex, Name, &cName, 0, 0, 0, 0);
513 if (errCode != ERROR_SUCCESS) continue;
514 errCode = RegOpenKeyEx(hNewKey, Name, 0, KEY_QUERY_VALUE, &hKey);
515 if (errCode == ERROR_SUCCESS) {
516 errCode = RegQueryInfoKey(hKey, 0, 0, 0, &dwSubCount, 0, 0, 0, 0, 0, 0, 0);
517 RegCloseKey(hKey);
518 }
519 if (errCode != ERROR_SUCCESS) dwSubCount = 0;
520 AddEntryToTree(hwndTV, pnmtv->itemNew.hItem, Name, NULL, dwSubCount);
521 }
522
523 SendMessage(hwndTV, TVM_SORTCHILDREN, 0, (LPARAM)pnmtv->itemNew.hItem);
524
525 RegCloseKey(hNewKey);
526 HeapFree(GetProcessHeap(), 0, Name);
527
528 done:
529 SendMessage(hwndTV, WM_SETREDRAW, TRUE, 0);
530 SetCursor(hcursorOld);
531 expanding = FALSE;
532
533 return TRUE;
534 }
535
536
537 BOOL CreateNewKey(HWND hwndTV, HTREEITEM hItem)
538 {
539 TCHAR szNewKeyFormat[128];
540 TCHAR szNewKey[128];
541 LPCTSTR pszKeyPath;
542 int iIndex = 1;
543 LONG nResult;
544 HKEY hRootKey = NULL, hKey = NULL, hNewKey = NULL;
545 BOOL bSuccess = FALSE;
546 DWORD dwDisposition;
547 HTREEITEM hNewItem;
548
549 pszKeyPath = GetItemPath(hwndTV, hItem, &hRootKey);
550 if (pszKeyPath[0] == TEXT('\0'))
551 hKey = hRootKey;
552 else if (RegOpenKey(hRootKey, pszKeyPath, &hKey) != ERROR_SUCCESS)
553 goto done;
554
555 if (LoadString(hInst, IDS_NEW_KEY, szNewKeyFormat, sizeof(szNewKeyFormat) / sizeof(szNewKeyFormat[0])) <= 0)
556 goto done;
557
558 /* Need to create a new key with a unique name */
559 do
560 {
561 wsprintf(szNewKey, szNewKeyFormat, iIndex++);
562 nResult = RegCreateKeyEx(hKey, szNewKey, 0, NULL, 0, KEY_WRITE, NULL, &hNewKey, &dwDisposition);
563 if (hNewKey && dwDisposition == REG_OPENED_EXISTING_KEY)
564 {
565 RegCloseKey(hNewKey);
566 hNewKey = NULL;
567 }
568 else if (!hNewKey)
569 {
570 TCHAR sz[256];
571 wsprintf(sz, TEXT("Cannot create new key!\n\nError Code: %d"), nResult);
572 MessageBox(hFrameWnd, sz, NULL, MB_ICONERROR);
573 goto done;
574 }
575 }
576 while(!hNewKey);
577
578 /* Insert the new key */
579 hNewItem = InsertNode(hwndTV, hItem, szNewKey);
580 if (!hNewItem)
581 goto done;
582
583 /* The new key's name is probably not appropriate yet */
584 (void)TreeView_EditLabel(hwndTV, hNewItem);
585
586 bSuccess = TRUE;
587
588 done:
589 if (hKey != hRootKey && hKey)
590 RegCloseKey(hKey);
591 if (hNewKey)
592 RegCloseKey(hNewKey);
593 return bSuccess;
594 }
595
596
597 /*
598 * CreateTreeView - creates a tree view control.
599 * Returns the handle to the new control if successful, or NULL otherwise.
600 * hwndParent - handle to the control's parent window.
601 */
602 HWND CreateTreeView(HWND hwndParent, LPTSTR pHostName, HMENU id)
603 {
604 RECT rcClient;
605 HWND hwndTV;
606
607 /* Get the dimensions of the parent window's client area, and create the tree view control. */
608 GetClientRect(hwndParent, &rcClient);
609 hwndTV = CreateWindowEx(WS_EX_CLIENTEDGE, WC_TREEVIEW, NULL,
610 WS_VISIBLE | WS_CHILD | WS_TABSTOP | TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT | TVS_EDITLABELS,
611 0, 0, rcClient.right, rcClient.bottom,
612 hwndParent, id, hInst, NULL);
613 /* Initialize the image list, and add items to the control. */
614 if (!InitTreeViewImageLists(hwndTV) || !InitTreeViewItems(hwndTV, pHostName)) {
615 DestroyWindow(hwndTV);
616 return NULL;
617 }
618 return hwndTV;
619 }
620
621 void DestroyTreeView() {
622 if (pathBuffer)
623 HeapFree(GetProcessHeap(), 0, pathBuffer);
624 }
625
626 BOOL SelectNode(HWND hwndTV, LPCTSTR keyPath)
627 {
628 HTREEITEM hRoot, hItem;
629 HTREEITEM hChildItem;
630 TCHAR szPathPart[128];
631 TCHAR szBuffer[128];
632 LPCTSTR s;
633 TVITEM tvi;
634
635 /* Total no-good hack */
636 if (!_tcsncmp(keyPath, _T("My Computer\\"), 12))
637 keyPath += 12;
638
639 hRoot = TreeView_GetRoot(hwndTV);
640 hItem = hRoot;
641
642 while(keyPath[0])
643 {
644 s = _tcschr(keyPath, TEXT('\\'));
645 lstrcpyn(szPathPart, keyPath, s ? s - keyPath + 1 : _tcslen(keyPath) + 1);
646
647 /* Special case for root to expand root key abbreviations */
648 if (hItem == hRoot)
649 {
650 if (!_tcscmp(szPathPart, TEXT("HKCR")))
651 _tcscpy(szPathPart, TEXT("HKEY_CLASSES_ROOT"));
652 else if (!_tcscmp(szPathPart, TEXT("HKCU")))
653 _tcscpy(szPathPart, TEXT("HKEY_CURRENT_USER"));
654 else if (!_tcscmp(szPathPart, TEXT("HKLM")))
655 _tcscpy(szPathPart, TEXT("HKEY_LOCAL_MACHINE"));
656 else if (!_tcscmp(szPathPart, TEXT("HKU")))
657 _tcscpy(szPathPart, TEXT("HKEY_USERS"));
658 else if (!_tcscmp(szPathPart, TEXT("HKCC")))
659 _tcscpy(szPathPart, TEXT("HKEY_CURRENT_CONFIG"));
660 else if (!_tcscmp(szPathPart, TEXT("HKDD")))
661 _tcscpy(szPathPart, TEXT("HKEY_DYN_DATA"));
662 }
663
664 for (hChildItem = TreeView_GetChild(hwndTV, hItem); hChildItem;
665 hChildItem = TreeView_GetNextSibling(hwndTV, hChildItem))
666 {
667 memset(&tvi, 0, sizeof(tvi));
668 tvi.hItem = hChildItem;
669 tvi.mask = TVIF_TEXT | TVIF_CHILDREN;
670 tvi.pszText = szBuffer;
671 tvi.cchTextMax = sizeof(szBuffer) / sizeof(szBuffer[0]);
672
673 (void)TreeView_GetItem(hwndTV, &tvi);
674
675 if (!_tcscmp(szBuffer, szPathPart))
676 break;
677 }
678
679 if (!hChildItem)
680 return FALSE;
681
682 if (tvi.cChildren > 0)
683 {
684 if (!TreeView_Expand(hwndTV, hChildItem, TVE_EXPAND))
685 return FALSE;
686 }
687
688 keyPath = s ? s + 1 : _T("");
689 hItem = hChildItem;
690 }
691
692 (void)TreeView_SelectItem(hwndTV, hItem);
693 (void)TreeView_EnsureVisible(hwndTV, hItem);
694
695 return TRUE;
696 }
697
698