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
23 /* Global variables and constants */
24 /* Image_Open, Image_Closed, and Image_Root - integer variables for indexes of the images. */
25 /* CX_ICON and CY_ICON - width and height of an icon. */
26 /* NUM_ICON - number of icons to add to the image list. */
31 static LPTSTR pathBuffer
;
37 static BOOL
get_item_path(HWND hwndTV
, HTREEITEM hItem
, HKEY
* phKey
, LPTSTR
* pKeyPath
, int* pPathLen
, int* pMaxLen
)
43 item
.mask
= TVIF_PARAM
;
45 if (!TreeView_GetItem(hwndTV
, &item
)) return FALSE
;
48 /* found root key with valid key value */
49 *phKey
= (HKEY
)item
.lParam
;
53 if(!get_item_path(hwndTV
, TreeView_GetParent(hwndTV
, hItem
), phKey
, pKeyPath
, pPathLen
, pMaxLen
)) return FALSE
;
55 (*pKeyPath
)[*pPathLen
] = _T('\\');
60 item
.mask
= TVIF_TEXT
;
62 item
.pszText
= *pKeyPath
+ *pPathLen
;
63 maxLen
= *pMaxLen
- *pPathLen
;
64 item
.cchTextMax
= (int) maxLen
;
65 if (!TreeView_GetItem(hwndTV
, &item
)) return FALSE
;
66 len
= _tcslen(item
.pszText
);
67 if (len
< maxLen
- 1) {
68 *pPathLen
+= (int) len
;
71 newStr
= HeapReAlloc(GetProcessHeap(), 0, *pKeyPath
, *pMaxLen
* 2);
72 if (!newStr
) return FALSE
;
80 LPCTSTR
GetItemPath(HWND hwndTV
, HTREEITEM hItem
, HKEY
* phRootKey
)
82 int pathLen
= 0, maxLen
;
85 if (!pathBuffer
) pathBuffer
= HeapAlloc(GetProcessHeap(), 0, 1024);
86 if (!pathBuffer
) return NULL
;
88 maxLen
= (int) HeapSize(GetProcessHeap(), 0, pathBuffer
);
89 if (maxLen
== -1) return NULL
;
90 if (!hItem
) hItem
= TreeView_GetSelection(hwndTV
);
91 if (!hItem
) return NULL
;
92 if (!get_item_path(hwndTV
, hItem
, phRootKey
, &pathBuffer
, &pathLen
, &maxLen
)) {
98 BOOL
DeleteNode(HWND hwndTV
, HTREEITEM hItem
)
100 if (!hItem
) hItem
= TreeView_GetSelection(hwndTV
);
101 if (!hItem
) return FALSE
;
102 return TreeView_DeleteItem(hwndTV
, hItem
);
105 /* Add an entry to the tree. Only give hKey for root nodes (HKEY_ constants) */
106 static HTREEITEM
AddEntryToTree(HWND hwndTV
, HTREEITEM hParent
, LPTSTR label
, HKEY hKey
, DWORD dwChildren
)
109 TVINSERTSTRUCT tvins
;
112 if (RegQueryInfoKey(hKey
, 0, 0, 0, &dwChildren
, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS
) {
117 tvi
.mask
= TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
| TVIF_PARAM
;
119 tvi
.cchTextMax
= lstrlen(tvi
.pszText
);
120 tvi
.iImage
= Image_Closed
;
121 tvi
.iSelectedImage
= Image_Open
;
122 tvi
.cChildren
= dwChildren
;
123 tvi
.lParam
= (LPARAM
)hKey
;
125 tvins
.hInsertAfter
= (HTREEITEM
)(hKey
? TVI_LAST
: TVI_FIRST
);
126 tvins
.hParent
= hParent
;
127 return TreeView_InsertItem(hwndTV
, &tvins
);
130 BOOL
RefreshTreeItem(HWND hwndTV
, HTREEITEM hItem
)
132 HKEY hRoot
, hKey
, hSubKey
;
135 DWORD dwCount
, dwIndex
, dwMaxSubKeyLen
;
138 LPTSTR pszNodes
= NULL
;
139 BOOL bSuccess
= FALSE
;
143 KeyPath
= GetItemPath(hwndTV
, hItem
, &hRoot
);
146 if (RegOpenKeyEx(hRoot
, KeyPath
, 0, KEY_READ
, &hKey
) != ERROR_SUCCESS
) {
153 if (RegQueryInfoKey(hKey
, 0, 0, 0, &dwCount
, &dwMaxSubKeyLen
, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS
) {
157 /* Set the number of children again */
158 tvItem
.mask
= TVIF_CHILDREN
;
159 tvItem
.hItem
= hItem
;
160 tvItem
.cChildren
= dwCount
;
161 if (!TreeView_SetItem(hwndTV
, &tvItem
)) {
165 /* We don't have to bother with the rest if it's not expanded. */
166 if (TreeView_GetItemState(hwndTV
, hItem
, TVIS_EXPANDED
) == 0) {
172 dwMaxSubKeyLen
++; /* account for the \0 terminator */
173 if (!(Name
= HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen
* sizeof(TCHAR
)))) {
176 tvItem
.cchTextMax
= dwMaxSubKeyLen
;
177 /*if (!(tvItem.pszText = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(TCHAR)))) {
181 /* Get all of the tree node siblings in one contiguous block of memory */
183 DWORD dwPhysicalSize
= 0;
184 DWORD dwActualSize
= 0;
185 DWORD dwNewPhysicalSize
;
187 DWORD dwStep
= 10000;
189 for (childItem
= TreeView_GetChild(hwndTV
, hItem
); childItem
;
190 childItem
= TreeView_GetNextSibling(hwndTV
, childItem
)) {
192 if (dwActualSize
+ dwMaxSubKeyLen
+ 1 > dwPhysicalSize
)
194 dwNewPhysicalSize
= dwActualSize
+ dwMaxSubKeyLen
+ 1 + dwStep
;
197 pszNewNodes
= (LPTSTR
) HeapReAlloc(GetProcessHeap(), 0, pszNodes
, dwNewPhysicalSize
* sizeof(TCHAR
));
199 pszNewNodes
= (LPTSTR
) HeapAlloc(GetProcessHeap(), 0, dwNewPhysicalSize
* sizeof(TCHAR
));
203 dwPhysicalSize
= dwNewPhysicalSize
;
204 pszNodes
= pszNewNodes
;
207 tvItem
.mask
= TVIF_TEXT
;
208 tvItem
.hItem
= childItem
;
209 tvItem
.pszText
= &pszNodes
[dwActualSize
];
210 tvItem
.cchTextMax
= dwPhysicalSize
- dwActualSize
;
211 if (!TreeView_GetItem(hwndTV
, &tvItem
))
214 dwActualSize
+= (DWORD
) _tcslen(&pszNodes
[dwActualSize
]) + 1;
218 pszNodes
[dwActualSize
] = '\0';
221 /* Now go through all the children in the tree, and check if any have to be removed. */
222 childItem
= TreeView_GetChild(hwndTV
, hItem
);
224 HTREEITEM nextItem
= TreeView_GetNextSibling(hwndTV
, childItem
);
225 if (RefreshTreeItem(hwndTV
, childItem
) == FALSE
) {
226 (void)TreeView_DeleteItem(hwndTV
, childItem
);
228 childItem
= nextItem
;
231 /* Now go through all the children in the registry, and check if any have to be added. */
233 for (dwIndex
= 0; dwIndex
< dwCount
; dwIndex
++) {
234 DWORD cName
= dwMaxSubKeyLen
, dwSubCount
;
238 if (RegEnumKeyEx(hKey
, dwIndex
, Name
, &cName
, 0, 0, 0, NULL
) != ERROR_SUCCESS
) {
242 /* Check if the node is already in there. */
244 for (s
= pszNodes
; *s
; s
+= _tcslen(s
) + 1) {
245 if (!_tcscmp(s
, Name
)) {
252 if (found
== FALSE
) {
253 /* Find the number of children of the node. */
255 if (RegOpenKeyEx(hKey
, Name
, 0, KEY_QUERY_VALUE
, &hSubKey
) == ERROR_SUCCESS
) {
256 if (RegQueryInfoKey(hSubKey
, 0, 0, 0, &dwSubCount
, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS
) {
259 RegCloseKey(hSubKey
);
262 AddEntryToTree(hwndTV
, hItem
, Name
, NULL
, dwSubCount
);
269 SendMessage(hwndTV
, TVM_SORTCHILDREN
, 0, (LPARAM
) hItem
);
275 HeapFree(GetProcessHeap(), 0, pszNodes
);
277 HeapFree(GetProcessHeap(), 0, Name
);
281 BOOL
RefreshTreeView(HWND hwndTV
)
284 HTREEITEM hSelectedItem
;
287 hSelectedItem
= TreeView_GetSelection(hwndTV
);
288 hcursorOld
= SetCursor(LoadCursor(NULL
, IDC_WAIT
));
289 SendMessage(hwndTV
, WM_SETREDRAW
, FALSE
, 0);
291 hItem
= TreeView_GetChild(hwndTV
, TreeView_GetRoot(hwndTV
));
293 RefreshTreeItem(hwndTV
, hItem
);
294 hItem
= TreeView_GetNextSibling(hwndTV
, hItem
);
297 SendMessage(hwndTV
, WM_SETREDRAW
, TRUE
, 0);
298 SetCursor(hcursorOld
);
300 /* We reselect the currently selected node, this will prompt a refresh of the listview. */
301 (void)TreeView_SelectItem(hwndTV
, hSelectedItem
);
305 HTREEITEM
InsertNode(HWND hwndTV
, HTREEITEM hItem
, LPTSTR name
)
307 TCHAR buf
[MAX_NEW_KEY_LEN
];
308 HTREEITEM hNewItem
= 0;
311 /* Default to the current selection */
314 hItem
= TreeView_GetSelection(hwndTV
);
319 memset(&item
, 0, sizeof(item
));
321 item
.mask
= TVIF_CHILDREN
| TVIF_HANDLE
| TVIF_STATE
;
322 if (!TreeView_GetItem(hwndTV
, &item
))
325 if ((item
.state
& TVIS_EXPANDEDONCE
) && (item
.cChildren
> 0))
327 hNewItem
= AddEntryToTree(hwndTV
, hItem
, name
, 0, 0);
328 SendMessage(hwndTV
, TVM_SORTCHILDREN
, 0, (LPARAM
) hItem
);
332 item
.mask
= TVIF_CHILDREN
| TVIF_HANDLE
;
335 if (!TreeView_SetItem(hwndTV
, &item
))
339 (void)TreeView_Expand(hwndTV
, hItem
, TVE_EXPAND
);
342 for(hNewItem
= TreeView_GetChild(hwndTV
, hItem
); hNewItem
; hNewItem
= TreeView_GetNextSibling(hwndTV
, hNewItem
))
344 item
.mask
= TVIF_HANDLE
| TVIF_TEXT
;
345 item
.hItem
= hNewItem
;
347 item
.cchTextMax
= COUNT_OF(buf
);
348 if (!TreeView_GetItem(hwndTV
, &item
)) continue;
349 if (lstrcmp(name
, item
.pszText
) == 0) break;
352 if (hNewItem
) (void)TreeView_SelectItem(hwndTV
, hNewItem
);
357 HWND
StartKeyRename(HWND hwndTV
)
361 if(!(hItem
= TreeView_GetSelection(hwndTV
))) return 0;
362 return TreeView_EditLabel(hwndTV
, hItem
);
365 static BOOL
InitTreeViewItems(HWND hwndTV
, LPTSTR pHostName
)
368 TVINSERTSTRUCT tvins
;
371 tvi
.mask
= TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
| TVIF_PARAM
;
372 /* Set the text of the item. */
373 tvi
.pszText
= pHostName
;
374 tvi
.cchTextMax
= lstrlen(tvi
.pszText
);
375 /* Assume the item is not a parent item, so give it an image. */
376 tvi
.iImage
= Image_Root
;
377 tvi
.iSelectedImage
= Image_Root
;
379 /* Save the heading level in the item's application-defined data area. */
380 tvi
.lParam
= (LPARAM
)NULL
;
382 tvins
.hInsertAfter
= (HTREEITEM
)TVI_FIRST
;
383 tvins
.hParent
= TVI_ROOT
;
384 /* Add the item to the tree view control. */
385 if (!(hRoot
= TreeView_InsertItem(hwndTV
, &tvins
))) return FALSE
;
387 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_CLASSES_ROOT"), HKEY_CLASSES_ROOT
, 1)) return FALSE
;
388 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_CURRENT_USER"), HKEY_CURRENT_USER
, 1)) return FALSE
;
389 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_LOCAL_MACHINE"), HKEY_LOCAL_MACHINE
, 1)) return FALSE
;
390 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_USERS"), HKEY_USERS
, 1)) return FALSE
;
391 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_CURRENT_CONFIG"), HKEY_CURRENT_CONFIG
, 1)) return FALSE
;
393 if (GetVersion() & 0x80000000)
395 /* Win9x specific key */
396 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_DYN_DATA"), HKEY_DYN_DATA
, 1)) return FALSE
;
399 /* expand and select host name */
400 (void)TreeView_Expand(hwndTV
, hRoot
, TVE_EXPAND
);
401 (void)TreeView_Select(hwndTV
, hRoot
, TVGN_CARET
);
407 * InitTreeViewImageLists - creates an image list, adds three bitmaps
408 * to it, and associates the image list with a tree view control.
409 * Returns TRUE if successful, or FALSE otherwise.
410 * hwndTV - handle to the tree view control.
412 static BOOL
InitTreeViewImageLists(HWND hwndTV
)
414 HIMAGELIST himl
; /* handle to image list */
415 HICON hico
; /* handle to icon */
417 /* Create the image list. */
418 if ((himl
= ImageList_Create(CX_ICON
, CY_ICON
,
419 ILC_MASK
|ILC_COLOR32
, 0, NUM_ICONS
)) == NULL
)
422 /* Add the open file, closed file, and document bitmaps. */
423 hico
= LoadIcon(hInst
, MAKEINTRESOURCE(IDI_OPEN_FILE
));
424 Image_Open
= ImageList_AddIcon(himl
, hico
);
426 hico
= LoadIcon(hInst
, MAKEINTRESOURCE(IDI_CLOSED_FILE
));
427 Image_Closed
= ImageList_AddIcon(himl
, hico
);
429 hico
= LoadIcon(hInst
, MAKEINTRESOURCE(IDI_ROOT
));
430 Image_Root
= ImageList_AddIcon(himl
, hico
);
432 /* Fail if not all of the images were added. */
433 if (ImageList_GetImageCount(himl
) < NUM_ICONS
)
438 /* Associate the image list with the tree view control. */
439 (void)TreeView_SetImageList(hwndTV
, himl
, TVSIL_NORMAL
);
444 BOOL
OnTreeExpanding(HWND hwndTV
, NMTREEVIEW
* pnmtv
)
446 DWORD dwCount
, dwIndex
, dwMaxSubKeyLen
;
447 HKEY hRoot
, hNewKey
, hKey
;
453 static int expanding
;
454 if (expanding
) return FALSE
;
455 if (pnmtv
->itemNew
.state
& TVIS_EXPANDEDONCE
) {
459 hcursorOld
= SetCursor(LoadCursor(NULL
, IDC_WAIT
));
460 SendMessage(hwndTV
, WM_SETREDRAW
, FALSE
, 0);
462 keyPath
= GetItemPath(hwndTV
, pnmtv
->itemNew
.hItem
, &hRoot
);
463 if (!keyPath
) goto done
;
466 errCode
= RegOpenKeyEx(hRoot
, keyPath
, 0, KEY_READ
, &hNewKey
);
467 if (errCode
!= ERROR_SUCCESS
) goto done
;
472 errCode
= RegQueryInfoKey(hNewKey
, 0, 0, 0, &dwCount
, &dwMaxSubKeyLen
, 0, 0, 0, 0, 0, 0);
473 if (errCode
!= ERROR_SUCCESS
) goto done
;
474 dwMaxSubKeyLen
++; /* account for the \0 terminator */
475 Name
= HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen
* sizeof(TCHAR
));
476 if (!Name
) goto done
;
478 for (dwIndex
= 0; dwIndex
< dwCount
; dwIndex
++) {
479 DWORD cName
= dwMaxSubKeyLen
, dwSubCount
;
481 errCode
= RegEnumKeyEx(hNewKey
, dwIndex
, Name
, &cName
, 0, 0, 0, 0);
482 if (errCode
!= ERROR_SUCCESS
) continue;
483 errCode
= RegOpenKeyEx(hNewKey
, Name
, 0, KEY_QUERY_VALUE
, &hKey
);
484 if (errCode
== ERROR_SUCCESS
) {
485 errCode
= RegQueryInfoKey(hKey
, 0, 0, 0, &dwSubCount
, 0, 0, 0, 0, 0, 0, 0);
488 if (errCode
!= ERROR_SUCCESS
) dwSubCount
= 0;
489 AddEntryToTree(hwndTV
, pnmtv
->itemNew
.hItem
, Name
, NULL
, dwSubCount
);
492 SendMessage(hwndTV
, TVM_SORTCHILDREN
, 0, (LPARAM
)pnmtv
->itemNew
.hItem
);
494 RegCloseKey(hNewKey
);
495 HeapFree(GetProcessHeap(), 0, Name
);
498 SendMessage(hwndTV
, WM_SETREDRAW
, TRUE
, 0);
499 SetCursor(hcursorOld
);
506 BOOL
CreateNewKey(HWND hwndTV
, HTREEITEM hItem
)
508 TCHAR szNewKeyFormat
[128];
515 BOOL bSuccess
= FALSE
;
520 pszKeyPath
= GetItemPath(g_pChildWnd
->hTreeWnd
, hItem
, &hRootKey
);
521 if (RegOpenKey(hRootKey
, pszKeyPath
, &hKey
) != ERROR_SUCCESS
)
524 if (LoadString(hInst
, IDS_NEW_KEY
, szNewKeyFormat
, sizeof(szNewKeyFormat
) / sizeof(szNewKeyFormat
[0])) <= 0)
527 /* Need to create a new key with a unique name */
530 _sntprintf(szNewKey
, sizeof(szNewKey
) / sizeof(szNewKey
[0]), szNewKeyFormat
, iIndex
++);
531 lResult
= RegCreateKeyEx(hKey
, szNewKey
, 0, NULL
, 0, KEY_ALL_ACCESS
, NULL
, &hNewKey
, &dwDisposition
);
532 if (hNewKey
&& (dwDisposition
== REG_OPENED_EXISTING_KEY
))
534 RegCloseKey(hNewKey
);
540 /* Insert the new key */
541 hNewItem
= InsertNode(hwndTV
, hItem
, szNewKey
);
545 /* The new key's name is probably not appropriate yet */
546 (void)TreeView_EditLabel(hwndTV
, hNewItem
);
554 RegCloseKey(hNewKey
);
560 * CreateTreeView - creates a tree view control.
561 * Returns the handle to the new control if successful, or NULL otherwise.
562 * hwndParent - handle to the control's parent window.
564 HWND
CreateTreeView(HWND hwndParent
, LPTSTR pHostName
, HMENU id
)
569 /* Get the dimensions of the parent window's client area, and create the tree view control. */
570 GetClientRect(hwndParent
, &rcClient
);
571 hwndTV
= CreateWindowEx(WS_EX_CLIENTEDGE
, WC_TREEVIEW
, NULL
,
572 WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
| TVS_HASLINES
| TVS_HASBUTTONS
| TVS_LINESATROOT
| TVS_EDITLABELS
,
573 0, 0, rcClient
.right
, rcClient
.bottom
,
574 hwndParent
, id
, hInst
, NULL
);
575 /* Initialize the image list, and add items to the control. */
576 if (!InitTreeViewImageLists(hwndTV
) || !InitTreeViewItems(hwndTV
, pHostName
)) {
577 DestroyWindow(hwndTV
);
583 void DestroyTreeView() {
585 HeapFree(GetProcessHeap(), 0, pathBuffer
);
588 BOOL
SelectNode(HWND hwndTV
, LPCTSTR keyPath
)
590 HTREEITEM hRoot
, hItem
;
591 HTREEITEM hChildItem
;
592 TCHAR szPathPart
[128];
597 /* Total no-good hack */
598 if (!_tcsncmp(keyPath
, _T("My Computer\\"), 12))
601 hRoot
= TreeView_GetRoot(hwndTV
);
606 s
= _tcschr(keyPath
, TEXT('\\'));
607 lstrcpyn(szPathPart
, keyPath
, s
? s
- keyPath
+ 1 : _tcslen(keyPath
) + 1);
609 /* Special case for root to expand root key abbreviations */
612 if (!_tcscmp(szPathPart
, TEXT("HKCR")))
613 _tcscpy(szPathPart
, TEXT("HKEY_CLASSES_ROOT"));
614 else if (!_tcscmp(szPathPart
, TEXT("HKCU")))
615 _tcscpy(szPathPart
, TEXT("HKEY_CURRENT_USER"));
616 else if (!_tcscmp(szPathPart
, TEXT("HKLM")))
617 _tcscpy(szPathPart
, TEXT("HKEY_LOCAL_MACHINE"));
618 else if (!_tcscmp(szPathPart
, TEXT("HKU")))
619 _tcscpy(szPathPart
, TEXT("HKEY_USERS"));
620 else if (!_tcscmp(szPathPart
, TEXT("HKCC")))
621 _tcscpy(szPathPart
, TEXT("HKEY_CURRENT_CONFIG"));
622 else if (!_tcscmp(szPathPart
, TEXT("HKDD")))
623 _tcscpy(szPathPart
, TEXT("HKEY_DYN_DATA"));
626 for (hChildItem
= TreeView_GetChild(hwndTV
, hItem
); hChildItem
;
627 hChildItem
= TreeView_GetNextSibling(hwndTV
, hChildItem
))
629 memset(&tvi
, 0, sizeof(tvi
));
630 tvi
.hItem
= hChildItem
;
631 tvi
.mask
= TVIF_TEXT
| TVIF_CHILDREN
;
632 tvi
.pszText
= szBuffer
;
633 tvi
.cchTextMax
= sizeof(szBuffer
) / sizeof(szBuffer
[0]);
635 (void)TreeView_GetItem(hwndTV
, &tvi
);
637 if (!_tcscmp(szBuffer
, szPathPart
))
644 if (tvi
.cChildren
> 0)
646 if (!TreeView_Expand(hwndTV
, hChildItem
, TVE_EXPAND
))
650 keyPath
= s
? s
+ 1 : _T("");
654 (void)TreeView_SelectItem(hwndTV
, hItem
);
655 (void)TreeView_EnsureVisible(hwndTV
, hItem
);