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
)) return NULL
;
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
)
112 TVINSERTSTRUCT tvins
;
115 if (RegQueryInfoKey(hKey
, 0, 0, 0, &dwChildren
, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS
) {
120 tvi
.mask
= TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
| TVIF_PARAM
;
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
;
128 tvins
.hInsertAfter
= (HTREEITEM
)(hKey
? TVI_LAST
: TVI_FIRST
);
129 tvins
.hParent
= hParent
;
130 return TreeView_InsertItem(hwndTV
, &tvins
);
133 static BOOL
RefreshTreeItem(HWND hwndTV
, HTREEITEM hItem
)
135 HKEY hRoot
, hKey
, hSubKey
;
138 DWORD dwCount
, dwIndex
, dwMaxSubKeyLen
;
141 LPTSTR pszNodes
= NULL
;
142 BOOL bSuccess
= FALSE
;
146 KeyPath
= GetItemPath(hwndTV
, hItem
, &hRoot
);
149 if (RegOpenKeyEx(hRoot
, KeyPath
, 0, KEY_READ
, &hKey
) != ERROR_SUCCESS
) {
156 if (RegQueryInfoKey(hKey
, 0, 0, 0, &dwCount
, &dwMaxSubKeyLen
, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS
) {
160 /* Set the number of children again */
161 tvItem
.mask
= TVIF_CHILDREN
;
162 tvItem
.hItem
= hItem
;
163 tvItem
.cChildren
= dwCount
;
164 if (!TreeView_SetItem(hwndTV
, &tvItem
)) {
168 /* We don't have to bother with the rest if it's not expanded. */
169 if (TreeView_GetItemState(hwndTV
, hItem
, TVIS_EXPANDED
) == 0) {
175 dwMaxSubKeyLen
++; /* account for the \0 terminator */
176 if (!(Name
= HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen
* sizeof(TCHAR
)))) {
179 tvItem
.cchTextMax
= dwMaxSubKeyLen
;
180 if (!(tvItem
.pszText
= HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen
* sizeof(TCHAR
)))) {
184 /* Get all of the tree node siblings in one contiguous block of memory */
186 DWORD dwPhysicalSize
= 0;
187 DWORD dwActualSize
= 0;
188 DWORD dwNewPhysicalSize
;
190 DWORD dwStep
= 10000;
192 for (childItem
= TreeView_GetChild(hwndTV
, hItem
); childItem
;
193 childItem
= TreeView_GetNextSibling(hwndTV
, childItem
)) {
195 if (dwActualSize
+ dwMaxSubKeyLen
+ 1 > dwPhysicalSize
)
197 dwNewPhysicalSize
= dwActualSize
+ dwMaxSubKeyLen
+ 1 + dwStep
;
200 pszNewNodes
= (LPTSTR
) HeapReAlloc(GetProcessHeap(), 0, pszNodes
, dwNewPhysicalSize
* sizeof(TCHAR
));
202 pszNewNodes
= (LPTSTR
) HeapAlloc(GetProcessHeap(), 0, dwNewPhysicalSize
* sizeof(TCHAR
));
206 dwPhysicalSize
= dwNewPhysicalSize
;
207 pszNodes
= pszNewNodes
;
210 tvItem
.mask
= TVIF_TEXT
;
211 tvItem
.hItem
= childItem
;
212 tvItem
.pszText
= &pszNodes
[dwActualSize
];
213 tvItem
.cchTextMax
= dwPhysicalSize
- dwActualSize
;
214 if (!TreeView_GetItem(hwndTV
, &tvItem
))
217 dwActualSize
+= _tcslen(&pszNodes
[dwActualSize
]) + 1;
221 pszNodes
[dwActualSize
] = '\0';
224 /* Now go through all the children in the registry, and check if any have to be added. */
226 for (dwIndex
= 0; dwIndex
< dwCount
; dwIndex
++) {
227 DWORD cName
= dwMaxSubKeyLen
, dwSubCount
;
231 if (RegEnumKeyEx(hKey
, dwIndex
, Name
, &cName
, 0, 0, 0, NULL
) != ERROR_SUCCESS
) {
235 /* Check if the node is already in there. */
237 for (s
= pszNodes
; *s
; s
+= _tcslen(s
) + 1) {
238 if (!_tcscmp(s
, Name
)) {
245 if (found
== FALSE
) {
246 /* Find the number of children of the node. */
248 if (RegOpenKeyEx(hKey
, Name
, 0, KEY_QUERY_VALUE
, &hSubKey
) == ERROR_SUCCESS
) {
249 if (RegQueryInfoKey(hSubKey
, 0, 0, 0, &dwSubCount
, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS
) {
252 RegCloseKey(hSubKey
);
255 AddEntryToTree(hwndTV
, hItem
, Name
, NULL
, dwSubCount
);
262 SendMessage(hwndTV
, TVM_SORTCHILDREN
, 0, (LPARAM
) hItem
);
264 /* Now go through all the children in the tree, and check if any have to be removed. */
265 childItem
= TreeView_GetChild(hwndTV
, hItem
);
267 HTREEITEM nextItem
= TreeView_GetNextSibling(hwndTV
, childItem
);
268 if (RefreshTreeItem(hwndTV
, childItem
) == FALSE
) {
269 TreeView_DeleteItem(hwndTV
, childItem
);
271 childItem
= nextItem
;
277 HeapFree(GetProcessHeap(), 0, pszNodes
);
279 HeapFree(GetProcessHeap(), 0, Name
);
283 BOOL
RefreshTreeView(HWND hwndTV
)
286 HTREEITEM hSelectedItem
;
289 hSelectedItem
= TreeView_GetSelection(hwndTV
);
290 hcursorOld
= SetCursor(LoadCursor(NULL
, IDC_WAIT
));
291 SendMessage(hwndTV
, WM_SETREDRAW
, FALSE
, 0);
293 hItem
= TreeView_GetChild(hwndTV
, TreeView_GetRoot(hwndTV
));
295 RefreshTreeItem(hwndTV
, hItem
);
296 hItem
= TreeView_GetNextSibling(hwndTV
, hItem
);
299 SendMessage(hwndTV
, WM_SETREDRAW
, TRUE
, 0);
300 SetCursor(hcursorOld
);
302 /* We reselect the currently selected node, this will prompt a refresh of the listview. */
303 TreeView_SelectItem(hwndTV
, hSelectedItem
);
307 HTREEITEM
InsertNode(HWND hwndTV
, HTREEITEM hItem
, LPTSTR name
)
309 TCHAR buf
[MAX_NEW_KEY_LEN
];
310 HTREEITEM hNewItem
= 0;
313 if (!hItem
) hItem
= TreeView_GetSelection(hwndTV
);
314 if (!hItem
) return FALSE
;
315 if (TreeView_GetItemState(hwndTV
, hItem
, TVIS_EXPANDEDONCE
)) {
316 hNewItem
= AddEntryToTree(hwndTV
, hItem
, name
, 0, 0);
317 SendMessage(hwndTV
, TVM_SORTCHILDREN
, 0, (LPARAM
) hItem
);
319 item
.mask
= TVIF_CHILDREN
| TVIF_HANDLE
;
321 if (!TreeView_GetItem(hwndTV
, &item
)) return FALSE
;
323 if (!TreeView_SetItem(hwndTV
, &item
)) return FALSE
;
325 TreeView_Expand(hwndTV
, hItem
, TVE_EXPAND
);
327 for(hNewItem
= TreeView_GetChild(hwndTV
, hItem
); hNewItem
; hNewItem
= TreeView_GetNextSibling(hwndTV
, hNewItem
)) {
328 item
.mask
= TVIF_HANDLE
| TVIF_TEXT
;
329 item
.hItem
= hNewItem
;
331 item
.cchTextMax
= COUNT_OF(buf
);
332 if (!TreeView_GetItem(hwndTV
, &item
)) continue;
333 if (lstrcmp(name
, item
.pszText
) == 0) break;
336 if (hNewItem
) TreeView_SelectItem(hwndTV
, hNewItem
);
341 HWND
StartKeyRename(HWND hwndTV
)
345 if(!(hItem
= TreeView_GetSelection(hwndTV
))) return 0;
346 return TreeView_EditLabel(hwndTV
, hItem
);
349 static BOOL
InitTreeViewItems(HWND hwndTV
, LPTSTR pHostName
)
352 TVINSERTSTRUCT tvins
;
355 tvi
.mask
= TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
| TVIF_PARAM
;
356 /* Set the text of the item. */
357 tvi
.pszText
= pHostName
;
358 tvi
.cchTextMax
= lstrlen(tvi
.pszText
);
359 /* Assume the item is not a parent item, so give it an image. */
360 tvi
.iImage
= Image_Root
;
361 tvi
.iSelectedImage
= Image_Root
;
363 /* Save the heading level in the item's application-defined data area. */
364 tvi
.lParam
= (LPARAM
)NULL
;
366 tvins
.hInsertAfter
= (HTREEITEM
)TVI_FIRST
;
367 tvins
.hParent
= TVI_ROOT
;
368 /* Add the item to the tree view control. */
369 if (!(hRoot
= TreeView_InsertItem(hwndTV
, &tvins
))) return FALSE
;
371 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_CLASSES_ROOT"), HKEY_CLASSES_ROOT
, 1)) return FALSE
;
372 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_CURRENT_USER"), HKEY_CURRENT_USER
, 1)) return FALSE
;
373 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_LOCAL_MACHINE"), HKEY_LOCAL_MACHINE
, 1)) return FALSE
;
374 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_USERS"), HKEY_USERS
, 1)) return FALSE
;
375 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_CURRENT_CONFIG"), HKEY_CURRENT_CONFIG
, 1)) return FALSE
;
377 if (GetVersion() & 0x80000000)
379 /* Win9x specific key */
380 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_DYN_DATA"), HKEY_DYN_DATA
, 1)) return FALSE
;
383 /* expand and select host name */
384 TreeView_Expand(hwndTV
, hRoot
, TVE_EXPAND
);
385 TreeView_Select(hwndTV
, hRoot
, TVGN_CARET
);
391 * InitTreeViewImageLists - creates an image list, adds three bitmaps
392 * to it, and associates the image list with a tree view control.
393 * Returns TRUE if successful, or FALSE otherwise.
394 * hwndTV - handle to the tree view control.
396 static BOOL
InitTreeViewImageLists(HWND hwndTV
)
398 HIMAGELIST himl
; /* handle to image list */
399 HICON hico
; /* handle to icon */
401 /* Create the image list. */
402 if ((himl
= ImageList_Create(CX_ICON
, CY_ICON
,
403 ILC_MASK
, 0, NUM_ICONS
)) == NULL
)
406 /* Add the open file, closed file, and document bitmaps. */
407 hico
= LoadIcon(hInst
, MAKEINTRESOURCE(IDI_OPEN_FILE
));
408 Image_Open
= ImageList_AddIcon(himl
, hico
);
410 hico
= LoadIcon(hInst
, MAKEINTRESOURCE(IDI_CLOSED_FILE
));
411 Image_Closed
= ImageList_AddIcon(himl
, hico
);
413 hico
= LoadIcon(hInst
, MAKEINTRESOURCE(IDI_ROOT
));
414 Image_Root
= ImageList_AddIcon(himl
, hico
);
416 /* Fail if not all of the images were added. */
417 if (ImageList_GetImageCount(himl
) < NUM_ICONS
)
422 /* Associate the image list with the tree view control. */
423 TreeView_SetImageList(hwndTV
, himl
, TVSIL_NORMAL
);
428 BOOL
OnTreeExpanding(HWND hwndTV
, NMTREEVIEW
* pnmtv
)
430 DWORD dwCount
, dwIndex
, dwMaxSubKeyLen
;
431 HKEY hRoot
, hNewKey
, hKey
;
437 static int expanding
;
438 if (expanding
) return FALSE
;
439 if (pnmtv
->itemNew
.state
& TVIS_EXPANDEDONCE
) {
443 hcursorOld
= SetCursor(LoadCursor(NULL
, IDC_WAIT
));
444 SendMessage(hwndTV
, WM_SETREDRAW
, FALSE
, 0);
446 keyPath
= GetItemPath(hwndTV
, pnmtv
->itemNew
.hItem
, &hRoot
);
447 if (!keyPath
) goto done
;
450 errCode
= RegOpenKeyEx(hRoot
, keyPath
, 0, KEY_READ
, &hNewKey
);
451 if (errCode
!= ERROR_SUCCESS
) goto done
;
456 errCode
= RegQueryInfoKey(hNewKey
, 0, 0, 0, &dwCount
, &dwMaxSubKeyLen
, 0, 0, 0, 0, 0, 0);
457 if (errCode
!= ERROR_SUCCESS
) goto done
;
458 dwMaxSubKeyLen
++; /* account for the \0 terminator */
459 Name
= HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen
* sizeof(TCHAR
));
460 if (!Name
) goto done
;
462 for (dwIndex
= 0; dwIndex
< dwCount
; dwIndex
++) {
463 DWORD cName
= dwMaxSubKeyLen
, dwSubCount
;
465 errCode
= RegEnumKeyEx(hNewKey
, dwIndex
, Name
, &cName
, 0, 0, 0, 0);
466 if (errCode
!= ERROR_SUCCESS
) continue;
467 errCode
= RegOpenKeyEx(hNewKey
, Name
, 0, KEY_QUERY_VALUE
, &hKey
);
468 if (errCode
== ERROR_SUCCESS
) {
469 errCode
= RegQueryInfoKey(hKey
, 0, 0, 0, &dwSubCount
, 0, 0, 0, 0, 0, 0, 0);
472 if (errCode
!= ERROR_SUCCESS
) dwSubCount
= 0;
473 AddEntryToTree(hwndTV
, pnmtv
->itemNew
.hItem
, Name
, NULL
, dwSubCount
);
476 SendMessage(hwndTV
, TVM_SORTCHILDREN
, 0, (LPARAM
)pnmtv
->itemNew
.hItem
);
478 RegCloseKey(hNewKey
);
479 HeapFree(GetProcessHeap(), 0, Name
);
482 SendMessage(hwndTV
, WM_SETREDRAW
, TRUE
, 0);
483 SetCursor(hcursorOld
);
490 BOOL
CreateNewKey(HWND hwndTV
, HTREEITEM hItem
)
492 TCHAR szNewKeyFormat
[128];
499 BOOL bSuccess
= FALSE
;
504 pszKeyPath
= GetItemPath(g_pChildWnd
->hTreeWnd
, hItem
, &hRootKey
);
505 if (RegOpenKey(hRootKey
, pszKeyPath
, &hKey
) != ERROR_SUCCESS
)
508 if (LoadString(hInst
, IDS_NEW_KEY
, szNewKeyFormat
, sizeof(szNewKeyFormat
) / sizeof(szNewKeyFormat
[0])) <= 0)
513 _sntprintf(szNewKey
, sizeof(szNewKey
) / sizeof(szNewKey
[0]), szNewKeyFormat
, iIndex
++);
514 lResult
= RegCreateKeyEx(hKey
, szNewKey
, 0, NULL
, 0, KEY_ALL_ACCESS
, NULL
, &hNewKey
, &dwDisposition
);
515 if (hNewKey
&& (dwDisposition
== REG_OPENED_EXISTING_KEY
))
517 RegCloseKey(hNewKey
);
523 hNewItem
= AddEntryToTree(hwndTV
, hItem
, szNewKey
, NULL
, 0);
526 SendMessage(hwndTV
, TVM_SORTCHILDREN
, 0, (LPARAM
) hItem
);
527 TreeView_EditLabel(hwndTV
, hNewItem
);
535 RegCloseKey(hNewKey
);
541 * CreateTreeView - creates a tree view control.
542 * Returns the handle to the new control if successful, or NULL otherwise.
543 * hwndParent - handle to the control's parent window.
545 HWND
CreateTreeView(HWND hwndParent
, LPTSTR pHostName
, int id
)
550 /* Get the dimensions of the parent window's client area, and create the tree view control. */
551 GetClientRect(hwndParent
, &rcClient
);
552 hwndTV
= CreateWindowEx(WS_EX_CLIENTEDGE
, WC_TREEVIEW
, NULL
,
553 WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
| TVS_HASLINES
| TVS_HASBUTTONS
| TVS_LINESATROOT
| TVS_EDITLABELS
,
554 0, 0, rcClient
.right
, rcClient
.bottom
,
555 hwndParent
, (HMENU
)id
, hInst
, NULL
);
556 /* Initialize the image list, and add items to the control. */
557 if (!InitTreeViewImageLists(hwndTV
) || !InitTreeViewItems(hwndTV
, pHostName
)) {
558 DestroyWindow(hwndTV
);
564 BOOL
SelectNode(HWND hwndTV
, LPCTSTR keyPath
)
566 HTREEITEM hRoot
, hItem
;
567 HTREEITEM hChildItem
;
568 TCHAR szPathPart
[128];
573 hRoot
= TreeView_GetRoot(hwndTV
);
578 s
= _tcschr(keyPath
, '\\');
579 lstrcpyn(szPathPart
, keyPath
, s
? s
- keyPath
+ 1 : _tcslen(keyPath
) + 1);
581 /* Special case for root to expand root key abbreviations */
584 if (!_tcscmp(szPathPart
, TEXT("HKCR")))
585 _tcscpy(szPathPart
, TEXT("HKEY_CLASSES_ROOT"));
586 else if (!_tcscmp(szPathPart
, TEXT("HKCU")))
587 _tcscpy(szPathPart
, TEXT("HKEY_CURRENT_USER"));
588 else if (!_tcscmp(szPathPart
, TEXT("HKLM")))
589 _tcscpy(szPathPart
, TEXT("HKEY_LOCAL_MACHINE"));
590 else if (!_tcscmp(szPathPart
, TEXT("HKU")))
591 _tcscpy(szPathPart
, TEXT("HKEY_USERS"));
592 else if (!_tcscmp(szPathPart
, TEXT("HKCC")))
593 _tcscpy(szPathPart
, TEXT("HKEY_CURRENT_CONFIG"));
594 else if (!_tcscmp(szPathPart
, TEXT("HKDD")))
595 _tcscpy(szPathPart
, TEXT("HKEY_DYN_DATA"));
598 for (hChildItem
= TreeView_GetChild(hwndTV
, hItem
); hChildItem
;
599 hChildItem
= TreeView_GetNextSibling(hwndTV
, hChildItem
))
601 memset(&tvi
, 0, sizeof(tvi
));
602 tvi
.hItem
= hChildItem
;
603 tvi
.mask
= TVIF_TEXT
;
604 tvi
.pszText
= szBuffer
;
605 tvi
.cchTextMax
= sizeof(szBuffer
) / sizeof(szBuffer
[0]);
607 TreeView_GetItem(hwndTV
, &tvi
);
609 if (!_tcscmp(szBuffer
, szPathPart
))
616 if (!TreeView_Expand(hwndTV
, hChildItem
, TVE_EXPAND
))
619 keyPath
= s
? s
+ 1 : _T("");
623 TreeView_SelectItem(hwndTV
, hItem
);
624 TreeView_EnsureVisible(hwndTV
, hItem
);