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