4 * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
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.
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.
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
21 #define WIN32_LEAN_AND_MEAN /* Exclude rarely-used stuff from Windows headers */
23 #define NONAMELESSUNION
24 #define NONAMELESSSTRUCT
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. */
44 static LPTSTR pathBuffer
;
50 static BOOL
get_item_path(HWND hwndTV
, HTREEITEM hItem
, HKEY
* phKey
, LPTSTR
* pKeyPath
, int* pPathLen
, int* pMaxLen
)
56 item
.mask
= TVIF_PARAM
;
58 if (!TreeView_GetItem(hwndTV
, &item
)) return FALSE
;
61 /* found root key with valid key value */
62 *phKey
= (HKEY
)item
.lParam
;
66 if(!get_item_path(hwndTV
, TreeView_GetParent(hwndTV
, hItem
), phKey
, pKeyPath
, pPathLen
, pMaxLen
)) return FALSE
;
68 (*pKeyPath
)[*pPathLen
] = _T('\\');
73 item
.mask
= TVIF_TEXT
;
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) {
83 newStr
= HeapReAlloc(GetProcessHeap(), 0, *pKeyPath
, *pMaxLen
* 2);
84 if (!newStr
) return FALSE
;
92 LPCTSTR
GetItemPath(HWND hwndTV
, HTREEITEM hItem
, HKEY
* phRootKey
)
94 int pathLen
= 0, maxLen
;
97 if (!pathBuffer
) pathBuffer
= HeapAlloc(GetProcessHeap(), 0, 1024);
98 if (!pathBuffer
) return NULL
;
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
)) {
110 BOOL
DeleteNode(HWND hwndTV
, HTREEITEM hItem
)
112 if (!hItem
) hItem
= TreeView_GetSelection(hwndTV
);
113 if (!hItem
) return FALSE
;
114 return TreeView_DeleteItem(hwndTV
, hItem
);
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
)
121 TVINSERTSTRUCT tvins
;
124 if (RegQueryInfoKey(hKey
, 0, 0, 0, &dwChildren
, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS
) {
129 tvi
.mask
= TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
| TVIF_PARAM
;
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
);
142 BOOL
RefreshTreeItem(HWND hwndTV
, HTREEITEM hItem
)
144 HKEY hRoot
, hKey
, hSubKey
;
147 DWORD dwCount
, dwIndex
, dwMaxSubKeyLen
;
150 LPTSTR pszNodes
= NULL
;
151 BOOL bSuccess
= FALSE
;
155 KeyPath
= GetItemPath(hwndTV
, hItem
, &hRoot
);
158 if (RegOpenKeyEx(hRoot
, KeyPath
, 0, KEY_READ
, &hKey
) != ERROR_SUCCESS
) {
165 if (RegQueryInfoKey(hKey
, 0, 0, 0, &dwCount
, &dwMaxSubKeyLen
, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS
) {
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
)) {
177 /* We don't have to bother with the rest if it's not expanded. */
178 if (TreeView_GetItemState(hwndTV
, hItem
, TVIS_EXPANDED
) == 0) {
184 dwMaxSubKeyLen
++; /* account for the \0 terminator */
185 if (!(Name
= HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen
* sizeof(TCHAR
)))) {
188 tvItem
.cchTextMax
= dwMaxSubKeyLen
;
189 /*if (!(tvItem.pszText = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(TCHAR)))) {
193 /* Get all of the tree node siblings in one contiguous block of memory */
195 DWORD dwPhysicalSize
= 0;
196 DWORD dwActualSize
= 0;
197 DWORD dwNewPhysicalSize
;
199 DWORD dwStep
= 10000;
201 for (childItem
= TreeView_GetChild(hwndTV
, hItem
); childItem
;
202 childItem
= TreeView_GetNextSibling(hwndTV
, childItem
)) {
204 if (dwActualSize
+ dwMaxSubKeyLen
+ 1 > dwPhysicalSize
)
206 dwNewPhysicalSize
= dwActualSize
+ dwMaxSubKeyLen
+ 1 + dwStep
;
209 pszNewNodes
= (LPTSTR
) HeapReAlloc(GetProcessHeap(), 0, pszNodes
, dwNewPhysicalSize
* sizeof(TCHAR
));
211 pszNewNodes
= (LPTSTR
) HeapAlloc(GetProcessHeap(), 0, dwNewPhysicalSize
* sizeof(TCHAR
));
215 dwPhysicalSize
= dwNewPhysicalSize
;
216 pszNodes
= pszNewNodes
;
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
))
226 dwActualSize
+= _tcslen(&pszNodes
[dwActualSize
]) + 1;
230 pszNodes
[dwActualSize
] = '\0';
233 /* Now go through all the children in the registry, and check if any have to be added. */
235 for (dwIndex
= 0; dwIndex
< dwCount
; dwIndex
++) {
236 DWORD cName
= dwMaxSubKeyLen
, dwSubCount
;
240 if (RegEnumKeyEx(hKey
, dwIndex
, Name
, &cName
, 0, 0, 0, NULL
) != ERROR_SUCCESS
) {
244 /* Check if the node is already in there. */
246 for (s
= pszNodes
; *s
; s
+= _tcslen(s
) + 1) {
247 if (!_tcscmp(s
, Name
)) {
254 if (found
== FALSE
) {
255 /* Find the number of children of the node. */
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
) {
261 RegCloseKey(hSubKey
);
264 AddEntryToTree(hwndTV
, hItem
, Name
, NULL
, dwSubCount
);
271 SendMessage(hwndTV
, TVM_SORTCHILDREN
, 0, (LPARAM
) hItem
);
273 /* Now go through all the children in the tree, and check if any have to be removed. */
274 childItem
= TreeView_GetChild(hwndTV
, hItem
);
276 HTREEITEM nextItem
= TreeView_GetNextSibling(hwndTV
, childItem
);
277 if (RefreshTreeItem(hwndTV
, childItem
) == FALSE
) {
278 TreeView_DeleteItem(hwndTV
, childItem
);
280 childItem
= nextItem
;
286 HeapFree(GetProcessHeap(), 0, pszNodes
);
288 HeapFree(GetProcessHeap(), 0, Name
);
292 BOOL
RefreshTreeView(HWND hwndTV
)
295 HTREEITEM hSelectedItem
;
298 hSelectedItem
= TreeView_GetSelection(hwndTV
);
299 hcursorOld
= SetCursor(LoadCursor(NULL
, IDC_WAIT
));
300 SendMessage(hwndTV
, WM_SETREDRAW
, FALSE
, 0);
302 hItem
= TreeView_GetChild(hwndTV
, TreeView_GetRoot(hwndTV
));
304 RefreshTreeItem(hwndTV
, hItem
);
305 hItem
= TreeView_GetNextSibling(hwndTV
, hItem
);
308 SendMessage(hwndTV
, WM_SETREDRAW
, TRUE
, 0);
309 SetCursor(hcursorOld
);
311 /* We reselect the currently selected node, this will prompt a refresh of the listview. */
312 TreeView_SelectItem(hwndTV
, hSelectedItem
);
316 HTREEITEM
InsertNode(HWND hwndTV
, HTREEITEM hItem
, LPTSTR name
)
318 TCHAR buf
[MAX_NEW_KEY_LEN
];
319 HTREEITEM hNewItem
= 0;
322 /* Default to the current selection */
325 hItem
= TreeView_GetSelection(hwndTV
);
330 memset(&item
, 0, sizeof(item
));
332 item
.mask
= TVIF_CHILDREN
| TVIF_HANDLE
| TVIF_STATE
;
333 if (!TreeView_GetItem(hwndTV
, &item
))
336 if ((item
.state
& TVIS_EXPANDEDONCE
) && (item
.cChildren
> 0))
338 hNewItem
= AddEntryToTree(hwndTV
, hItem
, name
, 0, 0);
339 SendMessage(hwndTV
, TVM_SORTCHILDREN
, 0, (LPARAM
) hItem
);
343 item
.mask
= TVIF_CHILDREN
| TVIF_HANDLE
;
346 if (!TreeView_SetItem(hwndTV
, &item
))
350 TreeView_Expand(hwndTV
, hItem
, TVE_EXPAND
);
353 for(hNewItem
= TreeView_GetChild(hwndTV
, hItem
); hNewItem
; hNewItem
= TreeView_GetNextSibling(hwndTV
, hNewItem
))
355 item
.mask
= TVIF_HANDLE
| TVIF_TEXT
;
356 item
.hItem
= hNewItem
;
358 item
.cchTextMax
= COUNT_OF(buf
);
359 if (!TreeView_GetItem(hwndTV
, &item
)) continue;
360 if (lstrcmp(name
, item
.pszText
) == 0) break;
363 if (hNewItem
) TreeView_SelectItem(hwndTV
, hNewItem
);
368 HWND
StartKeyRename(HWND hwndTV
)
372 if(!(hItem
= TreeView_GetSelection(hwndTV
))) return 0;
373 return TreeView_EditLabel(hwndTV
, hItem
);
376 static BOOL
InitTreeViewItems(HWND hwndTV
, LPTSTR pHostName
)
379 TVINSERTSTRUCT tvins
;
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
;
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
;
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
;
404 if (GetVersion() & 0x80000000)
406 /* Win9x specific key */
407 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_DYN_DATA"), HKEY_DYN_DATA
, 1)) return FALSE
;
410 /* expand and select host name */
411 TreeView_Expand(hwndTV
, hRoot
, TVE_EXPAND
);
412 TreeView_Select(hwndTV
, hRoot
, TVGN_CARET
);
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.
423 static BOOL
InitTreeViewImageLists(HWND hwndTV
)
425 HIMAGELIST himl
; /* handle to image list */
426 HICON hico
; /* handle to icon */
428 /* Create the image list. */
429 if ((himl
= ImageList_Create(CX_ICON
, CY_ICON
,
430 ILC_MASK
, 0, NUM_ICONS
)) == NULL
)
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
);
437 hico
= LoadIcon(hInst
, MAKEINTRESOURCE(IDI_CLOSED_FILE
));
438 Image_Closed
= ImageList_AddIcon(himl
, hico
);
440 hico
= LoadIcon(hInst
, MAKEINTRESOURCE(IDI_ROOT
));
441 Image_Root
= ImageList_AddIcon(himl
, hico
);
443 /* Fail if not all of the images were added. */
444 if (ImageList_GetImageCount(himl
) < NUM_ICONS
)
449 /* Associate the image list with the tree view control. */
450 TreeView_SetImageList(hwndTV
, himl
, TVSIL_NORMAL
);
455 BOOL
OnTreeExpanding(HWND hwndTV
, NMTREEVIEW
* pnmtv
)
457 DWORD dwCount
, dwIndex
, dwMaxSubKeyLen
;
458 HKEY hRoot
, hNewKey
, hKey
;
464 static int expanding
;
465 if (expanding
) return FALSE
;
466 if (pnmtv
->itemNew
.state
& TVIS_EXPANDEDONCE
) {
470 hcursorOld
= SetCursor(LoadCursor(NULL
, IDC_WAIT
));
471 SendMessage(hwndTV
, WM_SETREDRAW
, FALSE
, 0);
473 keyPath
= GetItemPath(hwndTV
, pnmtv
->itemNew
.hItem
, &hRoot
);
474 if (!keyPath
) goto done
;
477 errCode
= RegOpenKeyEx(hRoot
, keyPath
, 0, KEY_READ
, &hNewKey
);
478 if (errCode
!= ERROR_SUCCESS
) goto done
;
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
;
489 for (dwIndex
= 0; dwIndex
< dwCount
; dwIndex
++) {
490 DWORD cName
= dwMaxSubKeyLen
, dwSubCount
;
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);
499 if (errCode
!= ERROR_SUCCESS
) dwSubCount
= 0;
500 AddEntryToTree(hwndTV
, pnmtv
->itemNew
.hItem
, Name
, NULL
, dwSubCount
);
503 SendMessage(hwndTV
, TVM_SORTCHILDREN
, 0, (LPARAM
)pnmtv
->itemNew
.hItem
);
505 RegCloseKey(hNewKey
);
506 HeapFree(GetProcessHeap(), 0, Name
);
509 SendMessage(hwndTV
, WM_SETREDRAW
, TRUE
, 0);
510 SetCursor(hcursorOld
);
517 BOOL
CreateNewKey(HWND hwndTV
, HTREEITEM hItem
)
519 TCHAR szNewKeyFormat
[128];
526 BOOL bSuccess
= FALSE
;
531 pszKeyPath
= GetItemPath(g_pChildWnd
->hTreeWnd
, hItem
, &hRootKey
);
532 if (RegOpenKey(hRootKey
, pszKeyPath
, &hKey
) != ERROR_SUCCESS
)
535 if (LoadString(hInst
, IDS_NEW_KEY
, szNewKeyFormat
, sizeof(szNewKeyFormat
) / sizeof(szNewKeyFormat
[0])) <= 0)
538 /* Need to create a new key with a unique name */
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
))
545 RegCloseKey(hNewKey
);
551 /* Insert the new key */
552 hNewItem
= InsertNode(hwndTV
, hItem
, szNewKey
);
556 /* The new key's name is probably not appropriate yet */
557 TreeView_EditLabel(hwndTV
, hNewItem
);
565 RegCloseKey(hNewKey
);
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.
575 HWND
CreateTreeView(HWND hwndParent
, LPTSTR pHostName
, HMENU id
)
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
);
594 void DestroyTreeView() {
596 HeapFree(GetProcessHeap(), 0, pathBuffer
);
599 BOOL
SelectNode(HWND hwndTV
, LPCTSTR keyPath
)
601 HTREEITEM hRoot
, hItem
;
602 HTREEITEM hChildItem
;
603 TCHAR szPathPart
[128];
608 /* Total no-good hack */
609 if (!_tcsncmp(keyPath
, _T("My Computer\\"), 12))
612 hRoot
= TreeView_GetRoot(hwndTV
);
617 s
= _tcschr(keyPath
, '\\');
618 lstrcpyn(szPathPart
, keyPath
, s
? s
- keyPath
+ 1 : _tcslen(keyPath
) + 1);
620 /* Special case for root to expand root key abbreviations */
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"));
637 for (hChildItem
= TreeView_GetChild(hwndTV
, hItem
); hChildItem
;
638 hChildItem
= TreeView_GetNextSibling(hwndTV
, hChildItem
))
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]);
646 TreeView_GetItem(hwndTV
, &tvi
);
648 if (!_tcscmp(szBuffer
, szPathPart
))
655 if (tvi
.cChildren
> 0)
657 if (!TreeView_Expand(hwndTV
, hChildItem
, TVE_EXPAND
))
661 keyPath
= s
? s
+ 1 : _T("");
665 TreeView_SelectItem(hwndTV
, hItem
);
666 TreeView_EnsureVisible(hwndTV
, hItem
);