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
34 /* Global variables and constants */
35 /* Image_Open, Image_Closed, and Image_Root - integer variables for indexes of the images. */
36 /* CX_BITMAP and CY_BITMAP - width and height of an icon. */
37 /* NUM_BITMAPS - number of bitmaps to add to the image list. */
42 static LPTSTR pathBuffer
;
48 static BOOL
get_item_path(HWND hwndTV
, HTREEITEM hItem
, HKEY
* phKey
, LPTSTR
* pKeyPath
, int* pPathLen
, int* pMaxLen
)
54 item
.mask
= TVIF_PARAM
;
56 if (!TreeView_GetItem(hwndTV
, &item
)) return FALSE
;
59 /* found root key with valid key value */
60 *phKey
= (HKEY
)item
.lParam
;
64 if(!get_item_path(hwndTV
, TreeView_GetParent(hwndTV
, hItem
), phKey
, pKeyPath
, pPathLen
, pMaxLen
)) return FALSE
;
66 (*pKeyPath
)[*pPathLen
] = _T('\\');
71 item
.mask
= TVIF_TEXT
;
73 item
.pszText
= *pKeyPath
+ *pPathLen
;
74 item
.cchTextMax
= maxLen
= *pMaxLen
- *pPathLen
;
75 if (!TreeView_GetItem(hwndTV
, &item
)) return FALSE
;
76 len
= _tcslen(item
.pszText
);
77 if (len
< maxLen
- 1) {
81 newStr
= HeapReAlloc(GetProcessHeap(), 0, *pKeyPath
, *pMaxLen
* 2);
82 if (!newStr
) return FALSE
;
90 LPCTSTR
GetItemPath(HWND hwndTV
, HTREEITEM hItem
, HKEY
* phRootKey
)
92 int pathLen
= 0, maxLen
;
94 if (!pathBuffer
) pathBuffer
= HeapAlloc(GetProcessHeap(), 0, 1024);
95 if (!pathBuffer
) return NULL
;
97 maxLen
= HeapSize(GetProcessHeap(), 0, pathBuffer
);
98 if (maxLen
== (SIZE_T
) - 1) return NULL
;
99 if (!hItem
) hItem
= TreeView_GetSelection(hwndTV
);
100 if (!hItem
) return NULL
;
101 if (!get_item_path(hwndTV
, hItem
, phRootKey
, &pathBuffer
, &pathLen
, &maxLen
)) return NULL
;
105 BOOL
DeleteNode(HWND hwndTV
, HTREEITEM hItem
)
107 if (!hItem
) hItem
= TreeView_GetSelection(hwndTV
);
108 if (!hItem
) return FALSE
;
109 return TreeView_DeleteItem(hwndTV
, hItem
);
112 static HTREEITEM
AddEntryToTree(HWND hwndTV
, HTREEITEM hParent
, LPTSTR label
, HKEY hKey
, DWORD dwChildren
)
115 TVINSERTSTRUCT tvins
;
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_SORT
);
126 tvins
.hParent
= hParent
;
127 return TreeView_InsertItem(hwndTV
, &tvins
);
131 HTREEITEM
InsertNode(HWND hwndTV
, HTREEITEM hItem
, LPTSTR name
)
133 TCHAR buf
[MAX_NEW_KEY_LEN
];
134 HTREEITEM hNewItem
= 0;
137 if (!hItem
) hItem
= TreeView_GetSelection(hwndTV
);
138 if (!hItem
) return FALSE
;
139 if (TreeView_GetItemState(hwndTV
, hItem
, TVIS_EXPANDEDONCE
)) {
140 hNewItem
= AddEntryToTree(hwndTV
, hItem
, name
, 0, 0);
142 item
.mask
= TVIF_CHILDREN
| TVIF_HANDLE
;
144 if (!TreeView_GetItem(hwndTV
, &item
)) return FALSE
;
146 if (!TreeView_SetItem(hwndTV
, &item
)) return FALSE
;
148 TreeView_Expand(hwndTV
, hItem
, TVE_EXPAND
);
150 for(hNewItem
= TreeView_GetChild(hwndTV
, hItem
); hNewItem
; hNewItem
= TreeView_GetNextSibling(hwndTV
, hNewItem
)) {
151 item
.mask
= TVIF_HANDLE
| TVIF_TEXT
;
152 item
.hItem
= hNewItem
;
154 item
.cchTextMax
= COUNT_OF(buf
);
155 if (!TreeView_GetItem(hwndTV
, &item
)) continue;
156 if (lstrcmp(name
, item
.pszText
) == 0) break;
159 if (hNewItem
) TreeView_SelectItem(hwndTV
, hNewItem
);
164 HWND
StartKeyRename(HWND hwndTV
)
168 if(!(hItem
= TreeView_GetSelection(hwndTV
))) return 0;
169 return TreeView_EditLabel(hwndTV
, hItem
);
172 static BOOL
InitTreeViewItems(HWND hwndTV
, LPTSTR pHostName
)
175 TVINSERTSTRUCT tvins
;
178 tvi
.mask
= TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
| TVIF_PARAM
;
179 /* Set the text of the item. */
180 tvi
.pszText
= pHostName
;
181 tvi
.cchTextMax
= lstrlen(tvi
.pszText
);
182 /* Assume the item is not a parent item, so give it an image. */
183 tvi
.iImage
= Image_Root
;
184 tvi
.iSelectedImage
= Image_Root
;
186 /* Save the heading level in the item's application-defined data area. */
187 tvi
.lParam
= (LPARAM
)NULL
;
189 tvins
.hInsertAfter
= (HTREEITEM
)TVI_FIRST
;
190 tvins
.hParent
= TVI_ROOT
;
191 /* Add the item to the tree view control. */
192 if (!(hRoot
= TreeView_InsertItem(hwndTV
, &tvins
))) return FALSE
;
194 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_CLASSES_ROOT"), HKEY_CLASSES_ROOT
, 1)) return FALSE
;
195 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_CURRENT_USER"), HKEY_CURRENT_USER
, 1)) return FALSE
;
196 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_LOCAL_MACHINE"), HKEY_LOCAL_MACHINE
, 1)) return FALSE
;
197 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_USERS"), HKEY_USERS
, 1)) return FALSE
;
198 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_CURRENT_CONFIG"), HKEY_CURRENT_CONFIG
, 1)) return FALSE
;
200 /* expand and select host name */
201 TreeView_Expand(hwndTV
, hRoot
, TVE_EXPAND
);
202 TreeView_Select(hwndTV
, hRoot
, TVGN_CARET
);
208 * InitTreeViewImageLists - creates an image list, adds three bitmaps
209 * to it, and associates the image list with a tree view control.
210 * Returns TRUE if successful, or FALSE otherwise.
211 * hwndTV - handle to the tree view control.
213 static BOOL
InitTreeViewImageLists(HWND hwndTV
)
215 HIMAGELIST himl
; /* handle to image list */
216 HICON hico
; /* handle to icon */
218 /* Create the image list. */
219 if ((himl
= ImageList_Create(CX_ICON
, CY_ICON
,
220 ILC_MASK
, 0, NUM_ICONS
)) == NULL
)
223 /* Add the open file, closed file, and document bitmaps. */
224 hico
= LoadIcon(hInst
, MAKEINTRESOURCE(IDI_OPEN_FILE
));
225 Image_Open
= ImageList_AddIcon(himl
, hico
);
227 hico
= LoadIcon(hInst
, MAKEINTRESOURCE(IDI_CLOSED_FILE
));
228 Image_Closed
= ImageList_AddIcon(himl
, hico
);
230 hico
= LoadIcon(hInst
, MAKEINTRESOURCE(IDI_ROOT
));
231 Image_Root
= ImageList_AddIcon(himl
, hico
);
233 /* Fail if not all of the images were added. */
234 if (ImageList_GetImageCount(himl
) < NUM_ICONS
)
239 /* Associate the image list with the tree view control. */
240 TreeView_SetImageList(hwndTV
, himl
, TVSIL_NORMAL
);
245 BOOL
OnTreeExpanding(HWND hwndTV
, NMTREEVIEW
* pnmtv
)
247 DWORD dwCount
, dwIndex
, dwMaxSubKeyLen
;
248 HKEY hRoot
, hNewKey
, hKey
;
254 static int expanding
;
255 if (expanding
) return FALSE
;
256 if (pnmtv
->itemNew
.state
& TVIS_EXPANDEDONCE
) {
260 hcursorOld
= SetCursor(LoadCursor(NULL
, IDC_WAIT
));
261 SendMessage(hwndTV
, WM_SETREDRAW
, FALSE
, 0);
263 keyPath
= GetItemPath(hwndTV
, pnmtv
->itemNew
.hItem
, &hRoot
);
264 if (!keyPath
) goto done
;
267 errCode
= RegOpenKeyEx(hRoot
, keyPath
, 0, KEY_READ
, &hNewKey
);
268 if (errCode
!= ERROR_SUCCESS
) goto done
;
273 errCode
= RegQueryInfoKey(hNewKey
, 0, 0, 0, &dwCount
, &dwMaxSubKeyLen
, 0, 0, 0, 0, 0, 0);
274 if (errCode
!= ERROR_SUCCESS
) goto done
;
275 dwMaxSubKeyLen
++; /* account for the \0 terminator */
276 Name
= HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen
* sizeof(TCHAR
));
277 if (!Name
) goto done
;
279 for (dwIndex
= 0; dwIndex
< dwCount
; dwIndex
++) {
280 DWORD cName
= dwMaxSubKeyLen
, dwSubCount
;
281 FILETIME LastWriteTime
;
283 errCode
= RegEnumKeyEx(hNewKey
, dwIndex
, Name
, &cName
, 0, 0, 0, &LastWriteTime
);
284 if (errCode
!= ERROR_SUCCESS
) continue;
285 errCode
= RegOpenKeyEx(hNewKey
, Name
, 0, KEY_QUERY_VALUE
, &hKey
);
286 if (errCode
== ERROR_SUCCESS
) {
287 errCode
= RegQueryInfoKey(hKey
, 0, 0, 0, &dwSubCount
, 0, 0, 0, 0, 0, 0, 0);
290 if (errCode
!= ERROR_SUCCESS
) dwSubCount
= 0;
291 AddEntryToTree(hwndTV
, pnmtv
->itemNew
.hItem
, Name
, NULL
, dwSubCount
);
293 RegCloseKey(hNewKey
);
294 HeapFree(GetProcessHeap(), 0, Name
);
297 SendMessage(hwndTV
, WM_SETREDRAW
, TRUE
, 0);
298 SetCursor(hcursorOld
);
306 * CreateTreeView - creates a tree view control.
307 * Returns the handle to the new control if successful, or NULL otherwise.
308 * hwndParent - handle to the control's parent window.
310 HWND
CreateTreeView(HWND hwndParent
, LPTSTR pHostName
, int id
)
315 /* Get the dimensions of the parent window's client area, and create the tree view control. */
316 GetClientRect(hwndParent
, &rcClient
);
317 hwndTV
= CreateWindowEx(WS_EX_CLIENTEDGE
, WC_TREEVIEW
, _T("Tree View"),
318 WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
| TVS_HASLINES
| TVS_HASBUTTONS
| TVS_LINESATROOT
,
319 0, 0, rcClient
.right
, rcClient
.bottom
,
320 hwndParent
, (HMENU
)id
, hInst
, NULL
);
321 /* Initialize the image list, and add items to the control. */
322 if (!InitTreeViewImageLists(hwndTV
) || !InitTreeViewItems(hwndTV
, pHostName
)) {
323 DestroyWindow(hwndTV
);