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;
199 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_DYN_DATA"), HKEY_DYN_DATA, 1)) return FALSE;
201 /* expand and select host name */
202 TreeView_Expand(hwndTV, hRoot, TVE_EXPAND);
203 TreeView_Select(hwndTV, hRoot, TVGN_CARET);
209 * InitTreeViewImageLists - creates an image list, adds three bitmaps
210 * to it, and associates the image list with a tree view control.
211 * Returns TRUE if successful, or FALSE otherwise.
212 * hwndTV - handle to the tree view control.
214 static BOOL InitTreeViewImageLists(HWND hwndTV)
216 HIMAGELIST himl; /* handle to image list */
217 HICON hico; /* handle to icon */
219 /* Create the image list. */
220 if ((himl = ImageList_Create(CX_ICON, CY_ICON,
221 ILC_MASK, 0, NUM_ICONS)) == NULL)
224 /* Add the open file, closed file, and document bitmaps. */
225 hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_OPEN_FILE));
226 Image_Open = ImageList_AddIcon(himl, hico);
228 hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_CLOSED_FILE));
229 Image_Closed = ImageList_AddIcon(himl, hico);
231 hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_ROOT));
232 Image_Root = ImageList_AddIcon(himl, hico);
234 /* Fail if not all of the images were added. */
235 if (ImageList_GetImageCount(himl) < NUM_ICONS)
240 /* Associate the image list with the tree view control. */
241 TreeView_SetImageList(hwndTV, himl, TVSIL_NORMAL);
246 BOOL OnTreeExpanding(HWND hwndTV, NMTREEVIEW* pnmtv)
248 DWORD dwCount, dwIndex, dwMaxSubKeyLen;
249 HKEY hRoot, hNewKey, hKey;
255 static int expanding;
256 if (expanding) return FALSE;
257 if (pnmtv->itemNew.state & TVIS_EXPANDEDONCE ) {
261 hcursorOld = SetCursor(LoadCursor(NULL, IDC_WAIT));
262 SendMessage(hwndTV, WM_SETREDRAW, FALSE, 0);
264 keyPath = GetItemPath(hwndTV, pnmtv->itemNew.hItem, &hRoot);
265 if (!keyPath) goto done;
268 errCode = RegOpenKeyEx(hRoot, keyPath, 0, KEY_READ, &hNewKey);
269 if (errCode != ERROR_SUCCESS) goto done;
274 errCode = RegQueryInfoKey(hNewKey, 0, 0, 0, &dwCount, &dwMaxSubKeyLen, 0, 0, 0, 0, 0, 0);
275 if (errCode != ERROR_SUCCESS) goto done;
276 dwMaxSubKeyLen++; /* account for the \0 terminator */
277 Name = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(TCHAR));
278 if (!Name) goto done;
280 for (dwIndex = 0; dwIndex < dwCount; dwIndex++) {
281 DWORD cName = dwMaxSubKeyLen, dwSubCount;
282 FILETIME LastWriteTime;
284 errCode = RegEnumKeyEx(hNewKey, dwIndex, Name, &cName, 0, 0, 0, &LastWriteTime);
285 if (errCode != ERROR_SUCCESS) continue;
286 errCode = RegOpenKeyEx(hNewKey, Name, 0, KEY_QUERY_VALUE, &hKey);
287 if (errCode == ERROR_SUCCESS) {
288 errCode = RegQueryInfoKey(hKey, 0, 0, 0, &dwSubCount, 0, 0, 0, 0, 0, 0, 0);
291 if (errCode != ERROR_SUCCESS) dwSubCount = 0;
292 AddEntryToTree(hwndTV, pnmtv->itemNew.hItem, Name, NULL, dwSubCount);
294 RegCloseKey(hNewKey);
295 HeapFree(GetProcessHeap(), 0, Name);
298 SendMessage(hwndTV, WM_SETREDRAW, TRUE, 0);
299 SetCursor(hcursorOld);
307 * CreateTreeView - creates a tree view control.
308 * Returns the handle to the new control if successful, or NULL otherwise.
309 * hwndParent - handle to the control's parent window.
311 HWND CreateTreeView(HWND hwndParent, LPTSTR pHostName, int id)
316 /* Get the dimensions of the parent window's client area, and create the tree view control. */
317 GetClientRect(hwndParent, &rcClient);
318 hwndTV = CreateWindowEx(WS_EX_CLIENTEDGE, WC_TREEVIEW, _T("Tree View"),
319 WS_VISIBLE | WS_CHILD | WS_TABSTOP | TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT,
320 0, 0, rcClient.right, rcClient.bottom,
321 hwndParent, (HMENU)id, hInst, NULL);
322 /* Initialize the image list, and add items to the control. */
323 if (!InitTreeViewImageLists(hwndTV) || !InitTreeViewItems(hwndTV, pHostName)) {
324 DestroyWindow(hwndTV);