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
;
127 #if defined(__MINGW32__)
132 tvins
.hInsertAfter
= (HTREEITEM
)(hKey
? TVI_LAST
: TVI_FIRST
);
133 tvins
.hParent
= hParent
;
134 return TreeView_InsertItem(hwndTV
, &tvins
);
137 static BOOL
RefreshTreeItem(HWND hwndTV
, HTREEITEM hItem
)
139 HKEY hRoot
, hKey
, hSubKey
;
142 DWORD dwCount
, dwIndex
, dwMaxSubKeyLen
;
145 LPTSTR pszNodes
= NULL
;
146 BOOL bSuccess
= FALSE
;
150 KeyPath
= GetItemPath(hwndTV
, hItem
, &hRoot
);
153 if (RegOpenKeyEx(hRoot
, KeyPath
, 0, KEY_READ
, &hKey
) != ERROR_SUCCESS
) {
160 if (RegQueryInfoKey(hKey
, 0, 0, 0, &dwCount
, &dwMaxSubKeyLen
, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS
) {
164 /* Set the number of children again */
165 tvItem
.mask
= TVIF_CHILDREN
;
166 tvItem
.hItem
= hItem
;
167 tvItem
.cChildren
= dwCount
;
168 if (!TreeView_SetItem(hwndTV
, &tvItem
)) {
172 /* We don't have to bother with the rest if it's not expanded. */
173 if (TreeView_GetItemState(hwndTV
, hItem
, TVIS_EXPANDED
) == 0) {
179 dwMaxSubKeyLen
++; /* account for the \0 terminator */
180 if (!(Name
= HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen
* sizeof(TCHAR
)))) {
183 tvItem
.cchTextMax
= dwMaxSubKeyLen
;
184 if (!(tvItem
.pszText
= HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen
* sizeof(TCHAR
)))) {
188 /* Get all of the tree node siblings in one contiguous block of memory */
190 DWORD dwPhysicalSize
= 0;
191 DWORD dwActualSize
= 0;
192 DWORD dwNewPhysicalSize
;
194 DWORD dwStep
= 10000;
196 for (childItem
= TreeView_GetChild(hwndTV
, hItem
); childItem
;
197 childItem
= TreeView_GetNextSibling(hwndTV
, childItem
)) {
199 if (dwActualSize
+ dwMaxSubKeyLen
+ 1 > dwPhysicalSize
)
201 dwNewPhysicalSize
= dwActualSize
+ dwMaxSubKeyLen
+ 1 + dwStep
;
204 pszNewNodes
= (LPTSTR
) HeapReAlloc(GetProcessHeap(), 0, pszNodes
, dwNewPhysicalSize
* sizeof(TCHAR
));
206 pszNewNodes
= (LPTSTR
) HeapAlloc(GetProcessHeap(), 0, dwNewPhysicalSize
* sizeof(TCHAR
));
210 dwPhysicalSize
= dwNewPhysicalSize
;
211 pszNodes
= pszNewNodes
;
214 tvItem
.mask
= TVIF_TEXT
;
215 tvItem
.hItem
= childItem
;
216 tvItem
.pszText
= &pszNodes
[dwActualSize
];
217 tvItem
.cchTextMax
= dwPhysicalSize
- dwActualSize
;
218 if (!TreeView_GetItem(hwndTV
, &tvItem
))
221 dwActualSize
+= _tcslen(&pszNodes
[dwActualSize
]) + 1;
225 pszNodes
[dwActualSize
] = '\0';
228 /* Now go through all the children in the registry, and check if any have to be added. */
230 for (dwIndex
= 0; dwIndex
< dwCount
; dwIndex
++) {
231 DWORD cName
= dwMaxSubKeyLen
, dwSubCount
;
235 if (RegEnumKeyEx(hKey
, dwIndex
, Name
, &cName
, 0, 0, 0, NULL
) != ERROR_SUCCESS
) {
239 /* Check if the node is already in there. */
241 for (s
= pszNodes
; *s
; s
+= _tcslen(s
) + 1) {
242 if (!_tcscmp(s
, Name
)) {
249 if (found
== FALSE
) {
250 /* Find the number of children of the node. */
252 if (RegOpenKeyEx(hKey
, Name
, 0, KEY_QUERY_VALUE
, &hSubKey
) == ERROR_SUCCESS
) {
253 if (RegQueryInfoKey(hSubKey
, 0, 0, 0, &dwSubCount
, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS
) {
256 RegCloseKey(hSubKey
);
259 AddEntryToTree(hwndTV
, hItem
, Name
, NULL
, dwSubCount
);
266 SendMessage(hwndTV
, TVM_SORTCHILDREN
, 0, (LPARAM
) hItem
);
268 /* Now go through all the children in the tree, and check if any have to be removed. */
269 childItem
= TreeView_GetChild(hwndTV
, hItem
);
271 HTREEITEM nextItem
= TreeView_GetNextSibling(hwndTV
, childItem
);
272 if (RefreshTreeItem(hwndTV
, childItem
) == FALSE
) {
273 TreeView_DeleteItem(hwndTV
, childItem
);
275 childItem
= nextItem
;
281 HeapFree(GetProcessHeap(), 0, pszNodes
);
283 HeapFree(GetProcessHeap(), 0, Name
);
287 BOOL
RefreshTreeView(HWND hwndTV
)
290 HTREEITEM hSelectedItem
;
293 hSelectedItem
= TreeView_GetSelection(hwndTV
);
294 hcursorOld
= SetCursor(LoadCursor(NULL
, IDC_WAIT
));
295 SendMessage(hwndTV
, WM_SETREDRAW
, FALSE
, 0);
297 hItem
= TreeView_GetChild(hwndTV
, TreeView_GetRoot(hwndTV
));
299 RefreshTreeItem(hwndTV
, hItem
);
300 hItem
= TreeView_GetNextSibling(hwndTV
, hItem
);
303 SendMessage(hwndTV
, WM_SETREDRAW
, TRUE
, 0);
304 SetCursor(hcursorOld
);
306 /* We reselect the currently selected node, this will prompt a refresh of the listview. */
307 TreeView_SelectItem(hwndTV
, hSelectedItem
);
311 HTREEITEM
InsertNode(HWND hwndTV
, HTREEITEM hItem
, LPTSTR name
)
313 TCHAR buf
[MAX_NEW_KEY_LEN
];
314 HTREEITEM hNewItem
= 0;
317 if (!hItem
) hItem
= TreeView_GetSelection(hwndTV
);
318 if (!hItem
) return FALSE
;
319 if (TreeView_GetItemState(hwndTV
, hItem
, TVIS_EXPANDEDONCE
)) {
320 hNewItem
= AddEntryToTree(hwndTV
, hItem
, name
, 0, 0);
321 SendMessage(hwndTV
, TVM_SORTCHILDREN
, 0, (LPARAM
) hItem
);
323 item
.mask
= TVIF_CHILDREN
| TVIF_HANDLE
;
325 if (!TreeView_GetItem(hwndTV
, &item
)) return FALSE
;
327 if (!TreeView_SetItem(hwndTV
, &item
)) return FALSE
;
329 TreeView_Expand(hwndTV
, hItem
, TVE_EXPAND
);
331 for(hNewItem
= TreeView_GetChild(hwndTV
, hItem
); hNewItem
; hNewItem
= TreeView_GetNextSibling(hwndTV
, hNewItem
)) {
332 item
.mask
= TVIF_HANDLE
| TVIF_TEXT
;
333 item
.hItem
= hNewItem
;
335 item
.cchTextMax
= COUNT_OF(buf
);
336 if (!TreeView_GetItem(hwndTV
, &item
)) continue;
337 if (lstrcmp(name
, item
.pszText
) == 0) break;
340 if (hNewItem
) TreeView_SelectItem(hwndTV
, hNewItem
);
345 HWND
StartKeyRename(HWND hwndTV
)
349 if(!(hItem
= TreeView_GetSelection(hwndTV
))) return 0;
350 return TreeView_EditLabel(hwndTV
, hItem
);
353 static BOOL
InitTreeViewItems(HWND hwndTV
, LPTSTR pHostName
)
356 TVINSERTSTRUCT tvins
;
359 tvi
.mask
= TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
| TVIF_PARAM
;
360 /* Set the text of the item. */
361 tvi
.pszText
= pHostName
;
362 tvi
.cchTextMax
= lstrlen(tvi
.pszText
);
363 /* Assume the item is not a parent item, so give it an image. */
364 tvi
.iImage
= Image_Root
;
365 tvi
.iSelectedImage
= Image_Root
;
367 /* Save the heading level in the item's application-defined data area. */
368 tvi
.lParam
= (LPARAM
)NULL
;
369 #if defined(__MINGW32__)
374 tvins
.hInsertAfter
= (HTREEITEM
)TVI_FIRST
;
375 tvins
.hParent
= TVI_ROOT
;
376 /* Add the item to the tree view control. */
377 if (!(hRoot
= TreeView_InsertItem(hwndTV
, &tvins
))) return FALSE
;
379 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_CLASSES_ROOT"), HKEY_CLASSES_ROOT
, 1)) return FALSE
;
380 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_CURRENT_USER"), HKEY_CURRENT_USER
, 1)) return FALSE
;
381 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_LOCAL_MACHINE"), HKEY_LOCAL_MACHINE
, 1)) return FALSE
;
382 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_USERS"), HKEY_USERS
, 1)) return FALSE
;
383 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_CURRENT_CONFIG"), HKEY_CURRENT_CONFIG
, 1)) return FALSE
;
385 if (GetVersion() & 0x80000000)
387 /* Win9x specific key */
388 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_DYN_DATA"), HKEY_DYN_DATA
, 1)) return FALSE
;
391 /* expand and select host name */
392 TreeView_Expand(hwndTV
, hRoot
, TVE_EXPAND
);
393 TreeView_Select(hwndTV
, hRoot
, TVGN_CARET
);
399 * InitTreeViewImageLists - creates an image list, adds three bitmaps
400 * to it, and associates the image list with a tree view control.
401 * Returns TRUE if successful, or FALSE otherwise.
402 * hwndTV - handle to the tree view control.
404 static BOOL
InitTreeViewImageLists(HWND hwndTV
)
406 HIMAGELIST himl
; /* handle to image list */
407 HICON hico
; /* handle to icon */
409 /* Create the image list. */
410 if ((himl
= ImageList_Create(CX_ICON
, CY_ICON
,
411 ILC_MASK
, 0, NUM_ICONS
)) == NULL
)
414 /* Add the open file, closed file, and document bitmaps. */
415 hico
= LoadIcon(hInst
, MAKEINTRESOURCE(IDI_OPEN_FILE
));
416 Image_Open
= ImageList_AddIcon(himl
, hico
);
418 hico
= LoadIcon(hInst
, MAKEINTRESOURCE(IDI_CLOSED_FILE
));
419 Image_Closed
= ImageList_AddIcon(himl
, hico
);
421 hico
= LoadIcon(hInst
, MAKEINTRESOURCE(IDI_ROOT
));
422 Image_Root
= ImageList_AddIcon(himl
, hico
);
424 /* Fail if not all of the images were added. */
425 if (ImageList_GetImageCount(himl
) < NUM_ICONS
)
430 /* Associate the image list with the tree view control. */
431 TreeView_SetImageList(hwndTV
, himl
, TVSIL_NORMAL
);
436 BOOL
OnTreeExpanding(HWND hwndTV
, NMTREEVIEW
* pnmtv
)
438 DWORD dwCount
, dwIndex
, dwMaxSubKeyLen
;
439 HKEY hRoot
, hNewKey
, hKey
;
445 static int expanding
;
446 if (expanding
) return FALSE
;
447 if (pnmtv
->itemNew
.state
& TVIS_EXPANDEDONCE
) {
451 hcursorOld
= SetCursor(LoadCursor(NULL
, IDC_WAIT
));
452 SendMessage(hwndTV
, WM_SETREDRAW
, FALSE
, 0);
454 keyPath
= GetItemPath(hwndTV
, pnmtv
->itemNew
.hItem
, &hRoot
);
455 if (!keyPath
) goto done
;
458 errCode
= RegOpenKeyEx(hRoot
, keyPath
, 0, KEY_READ
, &hNewKey
);
459 if (errCode
!= ERROR_SUCCESS
) goto done
;
464 errCode
= RegQueryInfoKey(hNewKey
, 0, 0, 0, &dwCount
, &dwMaxSubKeyLen
, 0, 0, 0, 0, 0, 0);
465 if (errCode
!= ERROR_SUCCESS
) goto done
;
466 dwMaxSubKeyLen
++; /* account for the \0 terminator */
467 Name
= HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen
* sizeof(TCHAR
));
468 if (!Name
) goto done
;
470 for (dwIndex
= 0; dwIndex
< dwCount
; dwIndex
++) {
471 DWORD cName
= dwMaxSubKeyLen
, dwSubCount
;
473 errCode
= RegEnumKeyEx(hNewKey
, dwIndex
, Name
, &cName
, 0, 0, 0, 0);
474 if (errCode
!= ERROR_SUCCESS
) continue;
475 errCode
= RegOpenKeyEx(hNewKey
, Name
, 0, KEY_QUERY_VALUE
, &hKey
);
476 if (errCode
== ERROR_SUCCESS
) {
477 errCode
= RegQueryInfoKey(hKey
, 0, 0, 0, &dwSubCount
, 0, 0, 0, 0, 0, 0, 0);
480 if (errCode
!= ERROR_SUCCESS
) dwSubCount
= 0;
481 AddEntryToTree(hwndTV
, pnmtv
->itemNew
.hItem
, Name
, NULL
, dwSubCount
);
484 SendMessage(hwndTV
, TVM_SORTCHILDREN
, 0, (LPARAM
)pnmtv
->itemNew
.hItem
);
486 RegCloseKey(hNewKey
);
487 HeapFree(GetProcessHeap(), 0, Name
);
490 SendMessage(hwndTV
, WM_SETREDRAW
, TRUE
, 0);
491 SetCursor(hcursorOld
);
498 BOOL
CreateNewKey(HWND hwndTV
, HTREEITEM hItem
)
500 TCHAR szNewKeyFormat
[128];
507 BOOL bSuccess
= FALSE
;
512 pszKeyPath
= GetItemPath(g_pChildWnd
->hTreeWnd
, hItem
, &hRootKey
);
513 if (RegOpenKey(hRootKey
, pszKeyPath
, &hKey
) != ERROR_SUCCESS
)
516 if (LoadString(hInst
, IDS_NEW_KEY
, szNewKeyFormat
, sizeof(szNewKeyFormat
) / sizeof(szNewKeyFormat
[0])) <= 0)
521 _sntprintf(szNewKey
, sizeof(szNewKey
) / sizeof(szNewKey
[0]), szNewKeyFormat
, iIndex
++);
522 lResult
= RegCreateKeyEx(hKey
, szNewKey
, 0, NULL
, 0, KEY_ALL_ACCESS
, NULL
, &hNewKey
, &dwDisposition
);
523 if (hNewKey
&& (dwDisposition
== REG_OPENED_EXISTING_KEY
))
525 RegCloseKey(hNewKey
);
531 hNewItem
= AddEntryToTree(hwndTV
, hItem
, szNewKey
, NULL
, 0);
534 SendMessage(hwndTV
, TVM_SORTCHILDREN
, 0, (LPARAM
) hItem
);
535 TreeView_EditLabel(hwndTV
, hNewItem
);
543 RegCloseKey(hNewKey
);
549 * CreateTreeView - creates a tree view control.
550 * Returns the handle to the new control if successful, or NULL otherwise.
551 * hwndParent - handle to the control's parent window.
553 HWND
CreateTreeView(HWND hwndParent
, LPTSTR pHostName
, int id
)
558 /* Get the dimensions of the parent window's client area, and create the tree view control. */
559 GetClientRect(hwndParent
, &rcClient
);
560 hwndTV
= CreateWindowEx(WS_EX_CLIENTEDGE
, WC_TREEVIEW
, NULL
,
561 WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
| TVS_HASLINES
| TVS_HASBUTTONS
| TVS_LINESATROOT
| TVS_EDITLABELS
,
562 0, 0, rcClient
.right
, rcClient
.bottom
,
563 hwndParent
, (HMENU
)id
, hInst
, NULL
);
564 /* Initialize the image list, and add items to the control. */
565 if (!InitTreeViewImageLists(hwndTV
) || !InitTreeViewItems(hwndTV
, pHostName
)) {
566 DestroyWindow(hwndTV
);
572 BOOL
SelectNode(HWND hwndTV
, LPCTSTR keyPath
)
574 HTREEITEM hRoot
, hItem
;
575 HTREEITEM hChildItem
;
576 TCHAR szPathPart
[128];
581 hRoot
= TreeView_GetRoot(hwndTV
);
586 s
= _tcschr(keyPath
, '\\');
587 lstrcpyn(szPathPart
, keyPath
, s
? s
- keyPath
+ 1 : _tcslen(keyPath
) + 1);
589 /* Special case for root to expand root key abbreviations */
592 if (!_tcscmp(szPathPart
, TEXT("HKCR")))
593 _tcscpy(szPathPart
, TEXT("HKEY_CLASSES_ROOT"));
594 else if (!_tcscmp(szPathPart
, TEXT("HKCU")))
595 _tcscpy(szPathPart
, TEXT("HKEY_CURRENT_USER"));
596 else if (!_tcscmp(szPathPart
, TEXT("HKLM")))
597 _tcscpy(szPathPart
, TEXT("HKEY_LOCAL_MACHINE"));
598 else if (!_tcscmp(szPathPart
, TEXT("HKU")))
599 _tcscpy(szPathPart
, TEXT("HKEY_USERS"));
600 else if (!_tcscmp(szPathPart
, TEXT("HKCC")))
601 _tcscpy(szPathPart
, TEXT("HKEY_CURRENT_CONFIG"));
602 else if (!_tcscmp(szPathPart
, TEXT("HKDD")))
603 _tcscpy(szPathPart
, TEXT("HKEY_DYN_DATA"));
606 for (hChildItem
= TreeView_GetChild(hwndTV
, hItem
); hChildItem
;
607 hChildItem
= TreeView_GetNextSibling(hwndTV
, hChildItem
))
609 memset(&tvi
, 0, sizeof(tvi
));
610 tvi
.hItem
= hChildItem
;
611 tvi
.mask
= TVIF_TEXT
| TVIF_CHILDREN
;
612 tvi
.pszText
= szBuffer
;
613 tvi
.cchTextMax
= sizeof(szBuffer
) / sizeof(szBuffer
[0]);
615 TreeView_GetItem(hwndTV
, &tvi
);
617 if (!_tcscmp(szBuffer
, szPathPart
))
624 if (tvi
.cChildren
> 0)
626 if (!TreeView_Expand(hwndTV
, hChildItem
, TVE_EXPAND
))
630 keyPath
= s
? s
+ 1 : _T("");
634 TreeView_SelectItem(hwndTV
, hItem
);
635 TreeView_EnsureVisible(hwndTV
, hItem
);