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