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 /* Add an entry to the tree. Only give hKey for root nodes (HKEY_ constants) */
111 static HTREEITEM
AddEntryToTree(HWND hwndTV
, HTREEITEM hParent
, LPTSTR label
, HKEY hKey
, DWORD dwChildren
)
114 TVINSERTSTRUCT tvins
;
117 if (RegQueryInfoKey(hKey
, 0, 0, 0, &dwChildren
, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS
) {
122 tvi
.mask
= TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
| TVIF_PARAM
;
124 tvi
.cchTextMax
= lstrlen(tvi
.pszText
);
125 tvi
.iImage
= Image_Closed
;
126 tvi
.iSelectedImage
= Image_Open
;
127 tvi
.cChildren
= dwChildren
;
128 tvi
.lParam
= (LPARAM
)hKey
;
129 tvins
.DUMMYUNIONNAME
.item
= tvi
;
130 tvins
.hInsertAfter
= (HTREEITEM
)(hKey
? TVI_LAST
: TVI_FIRST
);
131 tvins
.hParent
= hParent
;
132 return TreeView_InsertItem(hwndTV
, &tvins
);
135 static BOOL
RefreshTreeItem(HWND hwndTV
, HTREEITEM hItem
)
137 HKEY hRoot
, hKey
, hSubKey
;
140 DWORD dwCount
, dwIndex
, dwMaxSubKeyLen
;
143 LPTSTR pszNodes
= NULL
;
144 BOOL bSuccess
= FALSE
;
148 KeyPath
= GetItemPath(hwndTV
, hItem
, &hRoot
);
151 if (RegOpenKeyEx(hRoot
, KeyPath
, 0, KEY_READ
, &hKey
) != ERROR_SUCCESS
) {
158 if (RegQueryInfoKey(hKey
, 0, 0, 0, &dwCount
, &dwMaxSubKeyLen
, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS
) {
162 /* Set the number of children again */
163 tvItem
.mask
= TVIF_CHILDREN
;
164 tvItem
.hItem
= hItem
;
165 tvItem
.cChildren
= dwCount
;
166 if (!TreeView_SetItem(hwndTV
, &tvItem
)) {
170 /* We don't have to bother with the rest if it's not expanded. */
171 if (TreeView_GetItemState(hwndTV
, hItem
, TVIS_EXPANDED
) == 0) {
177 dwMaxSubKeyLen
++; /* account for the \0 terminator */
178 if (!(Name
= HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen
* sizeof(TCHAR
)))) {
181 tvItem
.cchTextMax
= dwMaxSubKeyLen
;
182 /*if (!(tvItem.pszText = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(TCHAR)))) {
186 /* Get all of the tree node siblings in one contiguous block of memory */
188 DWORD dwPhysicalSize
= 0;
189 DWORD dwActualSize
= 0;
190 DWORD dwNewPhysicalSize
;
192 DWORD dwStep
= 10000;
194 for (childItem
= TreeView_GetChild(hwndTV
, hItem
); childItem
;
195 childItem
= TreeView_GetNextSibling(hwndTV
, childItem
)) {
197 if (dwActualSize
+ dwMaxSubKeyLen
+ 1 > dwPhysicalSize
)
199 dwNewPhysicalSize
= dwActualSize
+ dwMaxSubKeyLen
+ 1 + dwStep
;
202 pszNewNodes
= (LPTSTR
) HeapReAlloc(GetProcessHeap(), 0, pszNodes
, dwNewPhysicalSize
* sizeof(TCHAR
));
204 pszNewNodes
= (LPTSTR
) HeapAlloc(GetProcessHeap(), 0, dwNewPhysicalSize
* sizeof(TCHAR
));
208 dwPhysicalSize
= dwNewPhysicalSize
;
209 pszNodes
= pszNewNodes
;
212 tvItem
.mask
= TVIF_TEXT
;
213 tvItem
.hItem
= childItem
;
214 tvItem
.pszText
= &pszNodes
[dwActualSize
];
215 tvItem
.cchTextMax
= dwPhysicalSize
- dwActualSize
;
216 if (!TreeView_GetItem(hwndTV
, &tvItem
))
219 dwActualSize
+= _tcslen(&pszNodes
[dwActualSize
]) + 1;
223 pszNodes
[dwActualSize
] = '\0';
226 /* Now go through all the children in the registry, and check if any have to be added. */
228 for (dwIndex
= 0; dwIndex
< dwCount
; dwIndex
++) {
229 DWORD cName
= dwMaxSubKeyLen
, dwSubCount
;
233 if (RegEnumKeyEx(hKey
, dwIndex
, Name
, &cName
, 0, 0, 0, NULL
) != ERROR_SUCCESS
) {
237 /* Check if the node is already in there. */
239 for (s
= pszNodes
; *s
; s
+= _tcslen(s
) + 1) {
240 if (!_tcscmp(s
, Name
)) {
247 if (found
== FALSE
) {
248 /* Find the number of children of the node. */
250 if (RegOpenKeyEx(hKey
, Name
, 0, KEY_QUERY_VALUE
, &hSubKey
) == ERROR_SUCCESS
) {
251 if (RegQueryInfoKey(hSubKey
, 0, 0, 0, &dwSubCount
, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS
) {
254 RegCloseKey(hSubKey
);
257 AddEntryToTree(hwndTV
, hItem
, Name
, NULL
, dwSubCount
);
264 SendMessage(hwndTV
, TVM_SORTCHILDREN
, 0, (LPARAM
) hItem
);
266 /* Now go through all the children in the tree, and check if any have to be removed. */
267 childItem
= TreeView_GetChild(hwndTV
, hItem
);
269 HTREEITEM nextItem
= TreeView_GetNextSibling(hwndTV
, childItem
);
270 if (RefreshTreeItem(hwndTV
, childItem
) == FALSE
) {
271 TreeView_DeleteItem(hwndTV
, childItem
);
273 childItem
= nextItem
;
279 HeapFree(GetProcessHeap(), 0, pszNodes
);
281 HeapFree(GetProcessHeap(), 0, Name
);
285 BOOL
RefreshTreeView(HWND hwndTV
)
288 HTREEITEM hSelectedItem
;
291 hSelectedItem
= TreeView_GetSelection(hwndTV
);
292 hcursorOld
= SetCursor(LoadCursor(NULL
, IDC_WAIT
));
293 SendMessage(hwndTV
, WM_SETREDRAW
, FALSE
, 0);
295 hItem
= TreeView_GetChild(hwndTV
, TreeView_GetRoot(hwndTV
));
297 RefreshTreeItem(hwndTV
, hItem
);
298 hItem
= TreeView_GetNextSibling(hwndTV
, hItem
);
301 SendMessage(hwndTV
, WM_SETREDRAW
, TRUE
, 0);
302 SetCursor(hcursorOld
);
304 /* We reselect the currently selected node, this will prompt a refresh of the listview. */
305 TreeView_SelectItem(hwndTV
, hSelectedItem
);
309 HTREEITEM
InsertNode(HWND hwndTV
, HTREEITEM hItem
, LPTSTR name
)
311 TCHAR buf
[MAX_NEW_KEY_LEN
];
312 HTREEITEM hNewItem
= 0;
315 if (!hItem
) hItem
= TreeView_GetSelection(hwndTV
);
316 if (!hItem
) return FALSE
;
317 if (TreeView_GetItemState(hwndTV
, hItem
, TVIS_EXPANDEDONCE
)) {
318 hNewItem
= AddEntryToTree(hwndTV
, hItem
, name
, 0, 0);
319 SendMessage(hwndTV
, TVM_SORTCHILDREN
, 0, (LPARAM
) hItem
);
321 item
.mask
= TVIF_CHILDREN
| TVIF_HANDLE
;
323 if (!TreeView_GetItem(hwndTV
, &item
)) return FALSE
;
325 if (!TreeView_SetItem(hwndTV
, &item
)) return FALSE
;
327 TreeView_Expand(hwndTV
, hItem
, TVE_EXPAND
);
329 for(hNewItem
= TreeView_GetChild(hwndTV
, hItem
); hNewItem
; hNewItem
= TreeView_GetNextSibling(hwndTV
, hNewItem
)) {
330 item
.mask
= TVIF_HANDLE
| TVIF_TEXT
;
331 item
.hItem
= hNewItem
;
333 item
.cchTextMax
= COUNT_OF(buf
);
334 if (!TreeView_GetItem(hwndTV
, &item
)) continue;
335 if (lstrcmp(name
, item
.pszText
) == 0) break;
338 if (hNewItem
) TreeView_SelectItem(hwndTV
, hNewItem
);
343 HWND
StartKeyRename(HWND hwndTV
)
347 if(!(hItem
= TreeView_GetSelection(hwndTV
))) return 0;
348 return TreeView_EditLabel(hwndTV
, hItem
);
351 static BOOL
InitTreeViewItems(HWND hwndTV
, LPTSTR pHostName
)
354 TVINSERTSTRUCT tvins
;
357 tvi
.mask
= TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
| TVIF_PARAM
;
358 /* Set the text of the item. */
359 tvi
.pszText
= pHostName
;
360 tvi
.cchTextMax
= lstrlen(tvi
.pszText
);
361 /* Assume the item is not a parent item, so give it an image. */
362 tvi
.iImage
= Image_Root
;
363 tvi
.iSelectedImage
= Image_Root
;
365 /* Save the heading level in the item's application-defined data area. */
366 tvi
.lParam
= (LPARAM
)NULL
;
367 tvins
.DUMMYUNIONNAME
.item
= tvi
;
368 tvins
.hInsertAfter
= (HTREEITEM
)TVI_FIRST
;
369 tvins
.hParent
= TVI_ROOT
;
370 /* Add the item to the tree view control. */
371 if (!(hRoot
= TreeView_InsertItem(hwndTV
, &tvins
))) return FALSE
;
373 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_CLASSES_ROOT"), HKEY_CLASSES_ROOT
, 1)) return FALSE
;
374 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_CURRENT_USER"), HKEY_CURRENT_USER
, 1)) return FALSE
;
375 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_LOCAL_MACHINE"), HKEY_LOCAL_MACHINE
, 1)) return FALSE
;
376 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_USERS"), HKEY_USERS
, 1)) return FALSE
;
377 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_CURRENT_CONFIG"), HKEY_CURRENT_CONFIG
, 1)) return FALSE
;
379 if (GetVersion() & 0x80000000)
381 /* Win9x specific key */
382 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_DYN_DATA"), HKEY_DYN_DATA
, 1)) return FALSE
;
385 /* expand and select host name */
386 TreeView_Expand(hwndTV
, hRoot
, TVE_EXPAND
);
387 TreeView_Select(hwndTV
, hRoot
, TVGN_CARET
);
393 * InitTreeViewImageLists - creates an image list, adds three bitmaps
394 * to it, and associates the image list with a tree view control.
395 * Returns TRUE if successful, or FALSE otherwise.
396 * hwndTV - handle to the tree view control.
398 static BOOL
InitTreeViewImageLists(HWND hwndTV
)
400 HIMAGELIST himl
; /* handle to image list */
401 HICON hico
; /* handle to icon */
403 /* Create the image list. */
404 if ((himl
= ImageList_Create(CX_ICON
, CY_ICON
,
405 ILC_MASK
, 0, NUM_ICONS
)) == NULL
)
408 /* Add the open file, closed file, and document bitmaps. */
409 hico
= LoadIcon(hInst
, MAKEINTRESOURCE(IDI_OPEN_FILE
));
410 Image_Open
= ImageList_AddIcon(himl
, hico
);
412 hico
= LoadIcon(hInst
, MAKEINTRESOURCE(IDI_CLOSED_FILE
));
413 Image_Closed
= ImageList_AddIcon(himl
, hico
);
415 hico
= LoadIcon(hInst
, MAKEINTRESOURCE(IDI_ROOT
));
416 Image_Root
= ImageList_AddIcon(himl
, hico
);
418 /* Fail if not all of the images were added. */
419 if (ImageList_GetImageCount(himl
) < NUM_ICONS
)
424 /* Associate the image list with the tree view control. */
425 TreeView_SetImageList(hwndTV
, himl
, TVSIL_NORMAL
);
430 BOOL
OnTreeExpanding(HWND hwndTV
, NMTREEVIEW
* pnmtv
)
432 DWORD dwCount
, dwIndex
, dwMaxSubKeyLen
;
433 HKEY hRoot
, hNewKey
, hKey
;
439 static int expanding
;
440 if (expanding
) return FALSE
;
441 if (pnmtv
->itemNew
.state
& TVIS_EXPANDEDONCE
) {
445 hcursorOld
= SetCursor(LoadCursor(NULL
, IDC_WAIT
));
446 SendMessage(hwndTV
, WM_SETREDRAW
, FALSE
, 0);
448 keyPath
= GetItemPath(hwndTV
, pnmtv
->itemNew
.hItem
, &hRoot
);
449 if (!keyPath
) goto done
;
452 errCode
= RegOpenKeyEx(hRoot
, keyPath
, 0, KEY_READ
, &hNewKey
);
453 if (errCode
!= ERROR_SUCCESS
) goto done
;
458 errCode
= RegQueryInfoKey(hNewKey
, 0, 0, 0, &dwCount
, &dwMaxSubKeyLen
, 0, 0, 0, 0, 0, 0);
459 if (errCode
!= ERROR_SUCCESS
) goto done
;
460 dwMaxSubKeyLen
++; /* account for the \0 terminator */
461 Name
= HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen
* sizeof(TCHAR
));
462 if (!Name
) goto done
;
464 for (dwIndex
= 0; dwIndex
< dwCount
; dwIndex
++) {
465 DWORD cName
= dwMaxSubKeyLen
, dwSubCount
;
467 errCode
= RegEnumKeyEx(hNewKey
, dwIndex
, Name
, &cName
, 0, 0, 0, 0);
468 if (errCode
!= ERROR_SUCCESS
) continue;
469 errCode
= RegOpenKeyEx(hNewKey
, Name
, 0, KEY_QUERY_VALUE
, &hKey
);
470 if (errCode
== ERROR_SUCCESS
) {
471 errCode
= RegQueryInfoKey(hKey
, 0, 0, 0, &dwSubCount
, 0, 0, 0, 0, 0, 0, 0);
474 if (errCode
!= ERROR_SUCCESS
) dwSubCount
= 0;
475 AddEntryToTree(hwndTV
, pnmtv
->itemNew
.hItem
, Name
, NULL
, dwSubCount
);
478 SendMessage(hwndTV
, TVM_SORTCHILDREN
, 0, (LPARAM
)pnmtv
->itemNew
.hItem
);
480 RegCloseKey(hNewKey
);
481 HeapFree(GetProcessHeap(), 0, Name
);
484 SendMessage(hwndTV
, WM_SETREDRAW
, TRUE
, 0);
485 SetCursor(hcursorOld
);
492 BOOL
CreateNewKey(HWND hwndTV
, HTREEITEM hItem
)
494 TCHAR szNewKeyFormat
[128];
501 BOOL bSuccess
= FALSE
;
506 pszKeyPath
= GetItemPath(g_pChildWnd
->hTreeWnd
, hItem
, &hRootKey
);
507 if (RegOpenKey(hRootKey
, pszKeyPath
, &hKey
) != ERROR_SUCCESS
)
510 if (LoadString(hInst
, IDS_NEW_KEY
, szNewKeyFormat
, sizeof(szNewKeyFormat
) / sizeof(szNewKeyFormat
[0])) <= 0)
515 _sntprintf(szNewKey
, sizeof(szNewKey
) / sizeof(szNewKey
[0]), szNewKeyFormat
, iIndex
++);
516 lResult
= RegCreateKeyEx(hKey
, szNewKey
, 0, NULL
, 0, KEY_ALL_ACCESS
, NULL
, &hNewKey
, &dwDisposition
);
517 if (hNewKey
&& (dwDisposition
== REG_OPENED_EXISTING_KEY
))
519 RegCloseKey(hNewKey
);
525 hNewItem
= AddEntryToTree(hwndTV
, hItem
, szNewKey
, NULL
, 0);
528 SendMessage(hwndTV
, TVM_SORTCHILDREN
, 0, (LPARAM
) hItem
);
529 TreeView_EditLabel(hwndTV
, hNewItem
);
537 RegCloseKey(hNewKey
);
543 * CreateTreeView - creates a tree view control.
544 * Returns the handle to the new control if successful, or NULL otherwise.
545 * hwndParent - handle to the control's parent window.
547 HWND
CreateTreeView(HWND hwndParent
, LPTSTR pHostName
, int id
)
552 /* Get the dimensions of the parent window's client area, and create the tree view control. */
553 GetClientRect(hwndParent
, &rcClient
);
554 hwndTV
= CreateWindowEx(WS_EX_CLIENTEDGE
, WC_TREEVIEW
, NULL
,
555 WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
| TVS_HASLINES
| TVS_HASBUTTONS
| TVS_LINESATROOT
| TVS_EDITLABELS
,
556 0, 0, rcClient
.right
, rcClient
.bottom
,
557 hwndParent
, (HMENU
)id
, hInst
, NULL
);
558 /* Initialize the image list, and add items to the control. */
559 if (!InitTreeViewImageLists(hwndTV
) || !InitTreeViewItems(hwndTV
, pHostName
)) {
560 DestroyWindow(hwndTV
);
566 void DestroyTreeView() {
568 HeapFree(GetProcessHeap(), 0, pathBuffer
);
571 BOOL
SelectNode(HWND hwndTV
, LPCTSTR keyPath
)
573 HTREEITEM hRoot
, hItem
;
574 HTREEITEM hChildItem
;
575 TCHAR szPathPart
[128];
580 hRoot
= TreeView_GetRoot(hwndTV
);
585 s
= _tcschr(keyPath
, '\\');
586 lstrcpyn(szPathPart
, keyPath
, s
? s
- keyPath
+ 1 : _tcslen(keyPath
) + 1);
588 /* Special case for root to expand root key abbreviations */
591 if (!_tcscmp(szPathPart
, TEXT("HKCR")))
592 _tcscpy(szPathPart
, TEXT("HKEY_CLASSES_ROOT"));
593 else if (!_tcscmp(szPathPart
, TEXT("HKCU")))
594 _tcscpy(szPathPart
, TEXT("HKEY_CURRENT_USER"));
595 else if (!_tcscmp(szPathPart
, TEXT("HKLM")))
596 _tcscpy(szPathPart
, TEXT("HKEY_LOCAL_MACHINE"));
597 else if (!_tcscmp(szPathPart
, TEXT("HKU")))
598 _tcscpy(szPathPart
, TEXT("HKEY_USERS"));
599 else if (!_tcscmp(szPathPart
, TEXT("HKCC")))
600 _tcscpy(szPathPart
, TEXT("HKEY_CURRENT_CONFIG"));
601 else if (!_tcscmp(szPathPart
, TEXT("HKDD")))
602 _tcscpy(szPathPart
, TEXT("HKEY_DYN_DATA"));
605 for (hChildItem
= TreeView_GetChild(hwndTV
, hItem
); hChildItem
;
606 hChildItem
= TreeView_GetNextSibling(hwndTV
, hChildItem
))
608 memset(&tvi
, 0, sizeof(tvi
));
609 tvi
.hItem
= hChildItem
;
610 tvi
.mask
= TVIF_TEXT
| TVIF_CHILDREN
;
611 tvi
.pszText
= szBuffer
;
612 tvi
.cchTextMax
= sizeof(szBuffer
) / sizeof(szBuffer
[0]);
614 TreeView_GetItem(hwndTV
, &tvi
);
616 if (!_tcscmp(szBuffer
, szPathPart
))
623 if (tvi
.cChildren
> 0)
625 if (!TreeView_Expand(hwndTV
, hChildItem
, TVE_EXPAND
))
629 keyPath
= s
? s
+ 1 : _T("");
633 TreeView_SelectItem(hwndTV
, hItem
);
634 TreeView_EnsureVisible(hwndTV
, hItem
);