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
30 #include <wine/debug.h>
34 WINE_DEFAULT_DEBUG_CHANNEL(regedit
);
36 /* Global variables and constants */
37 /* Image_Open, Image_Closed, and Image_Root - integer variables for indexes of the images. */
38 /* CX_BITMAP and CY_BITMAP - width and height of an icon. */
39 /* NUM_BITMAPS - number of bitmaps 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
;
96 if (!pathBuffer
) pathBuffer
= HeapAlloc(GetProcessHeap(), 0, 1024);
97 if (!pathBuffer
) return NULL
;
99 maxLen
= HeapSize(GetProcessHeap(), 0, pathBuffer
);
100 if (maxLen
== (SIZE_T
) - 1) return NULL
;
101 if (!hItem
) hItem
= TreeView_GetSelection(hwndTV
);
102 if (!hItem
) return NULL
;
103 if (!get_item_path(hwndTV
, hItem
, phRootKey
, &pathBuffer
, &pathLen
, &maxLen
)) return NULL
;
107 BOOL
DeleteNode(HWND hwndTV
, HTREEITEM hItem
)
109 if (!hItem
) hItem
= TreeView_GetSelection(hwndTV
);
110 if (!hItem
) return FALSE
;
111 return TreeView_DeleteItem(hwndTV
, hItem
);
114 /* Add an entry to the tree. Only give hKey for root nodes (HKEY_ constants) */
115 static HTREEITEM
AddEntryToTree(HWND hwndTV
, HTREEITEM hParent
, LPTSTR label
, HKEY hKey
, DWORD dwChildren
)
118 TVINSERTSTRUCT tvins
;
121 if (RegQueryInfoKey(hKey
, 0, 0, 0, &dwChildren
, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS
) {
126 tvi
.mask
= TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
| TVIF_PARAM
;
128 tvi
.cchTextMax
= lstrlen(tvi
.pszText
);
129 tvi
.iImage
= Image_Closed
;
130 tvi
.iSelectedImage
= Image_Open
;
131 tvi
.cChildren
= dwChildren
;
132 tvi
.lParam
= (LPARAM
)hKey
;
134 tvins
.hInsertAfter
= (HTREEITEM
)(hKey
? TVI_LAST
: TVI_SORT
);
135 tvins
.hParent
= hParent
;
136 return TreeView_InsertItem(hwndTV
, &tvins
);
139 static BOOL
RefreshTreeItem(HWND hwndTV
, HTREEITEM hItem
)
141 HKEY hRoot
, hKey
, hSubKey
;
144 DWORD dwCount
, dwIndex
, dwMaxSubKeyLen
;
148 KeyPath
= GetItemPath(hwndTV
, hItem
, &hRoot
);
151 if (RegOpenKeyEx(hRoot
, KeyPath
, 0, KEY_READ
, &hKey
) != ERROR_SUCCESS
) {
152 WINE_TRACE("RegOpenKeyEx failed, \"%s\" was probably removed.\n", KeyPath
);
159 if (RegQueryInfoKey(hKey
, 0, 0, 0, &dwCount
, &dwMaxSubKeyLen
, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS
) {
163 /* Set the number of children again */
164 tvItem
.mask
= TVIF_CHILDREN
;
165 tvItem
.hItem
= hItem
;
166 tvItem
.cChildren
= dwCount
;
167 if (!TreeView_SetItem(hwndTV
, &tvItem
)) {
171 /* We don't have to bother with the rest if it's not expanded. */
172 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 /* Now go through all the children in the registry, and check if any have to be added. */
187 for (dwIndex
= 0; dwIndex
< dwCount
; dwIndex
++) {
188 DWORD cName
= dwMaxSubKeyLen
, dwSubCount
;
192 if (RegEnumKeyEx(hKey
, dwIndex
, Name
, &cName
, 0, 0, 0, NULL
) != ERROR_SUCCESS
) {
196 /* Find the number of children of the node. */
198 if (RegOpenKeyEx(hKey
, Name
, 0, KEY_QUERY_VALUE
, &hSubKey
) == ERROR_SUCCESS
) {
199 if (RegQueryInfoKey(hSubKey
, 0, 0, 0, &dwSubCount
, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS
) {
202 RegCloseKey(hSubKey
);
205 /* Check if the node is already in there. */
206 for (childItem
= TreeView_GetChild(hwndTV
, hItem
); childItem
;
207 childItem
= TreeView_GetNextSibling(hwndTV
, childItem
)) {
208 tvItem
.mask
= TVIF_TEXT
;
209 tvItem
.hItem
= childItem
;
210 if (!TreeView_GetItem(hwndTV
, &tvItem
)) {
214 if (!strcmp(tvItem
.pszText
, Name
)) {
220 if (found
== FALSE
) {
221 WINE_TRACE("New subkey %s\n", Name
);
222 AddEntryToTree(hwndTV
, hItem
, Name
, NULL
, dwSubCount
);
225 HeapFree(GetProcessHeap(), 0, Name
);
226 HeapFree(GetProcessHeap(), 0, tvItem
.pszText
);
229 /* Now go through all the children in the tree, and check if any have to be removed. */
230 childItem
= TreeView_GetChild(hwndTV
, hItem
);
232 HTREEITEM nextItem
= TreeView_GetNextSibling(hwndTV
, childItem
);
233 if (RefreshTreeItem(hwndTV
, childItem
) == FALSE
) {
234 TreeView_DeleteItem(hwndTV
, childItem
);
236 childItem
= nextItem
;
242 BOOL
RefreshTreeView(HWND hwndTV
)
245 HTREEITEM hSelectedItem
;
249 hSelectedItem
= TreeView_GetSelection(hwndTV
);
250 hcursorOld
= SetCursor(LoadCursor(NULL
, IDC_WAIT
));
251 SendMessage(hwndTV
, WM_SETREDRAW
, FALSE
, 0);
253 hItem
= TreeView_GetChild(hwndTV
, TreeView_GetRoot(hwndTV
));
255 RefreshTreeItem(hwndTV
, hItem
);
256 hItem
= TreeView_GetNextSibling(hwndTV
, hItem
);
259 SendMessage(hwndTV
, WM_SETREDRAW
, TRUE
, 0);
260 SetCursor(hcursorOld
);
262 /* We reselect the currently selected node, this will prompt a refresh of the listview. */
263 TreeView_SelectItem(hwndTV
, hSelectedItem
);
267 HTREEITEM
InsertNode(HWND hwndTV
, HTREEITEM hItem
, LPTSTR name
)
269 TCHAR buf
[MAX_NEW_KEY_LEN
];
270 HTREEITEM hNewItem
= 0;
273 if (!hItem
) hItem
= TreeView_GetSelection(hwndTV
);
274 if (!hItem
) return FALSE
;
275 if (TreeView_GetItemState(hwndTV
, hItem
, TVIS_EXPANDEDONCE
)) {
276 hNewItem
= AddEntryToTree(hwndTV
, hItem
, name
, 0, 0);
278 item
.mask
= TVIF_CHILDREN
| TVIF_HANDLE
;
280 if (!TreeView_GetItem(hwndTV
, &item
)) return FALSE
;
282 if (!TreeView_SetItem(hwndTV
, &item
)) return FALSE
;
284 TreeView_Expand(hwndTV
, hItem
, TVE_EXPAND
);
286 for(hNewItem
= TreeView_GetChild(hwndTV
, hItem
); hNewItem
; hNewItem
= TreeView_GetNextSibling(hwndTV
, hNewItem
)) {
287 item
.mask
= TVIF_HANDLE
| TVIF_TEXT
;
288 item
.hItem
= hNewItem
;
290 item
.cchTextMax
= COUNT_OF(buf
);
291 if (!TreeView_GetItem(hwndTV
, &item
)) continue;
292 if (lstrcmp(name
, item
.pszText
) == 0) break;
295 if (hNewItem
) TreeView_SelectItem(hwndTV
, hNewItem
);
300 HWND
StartKeyRename(HWND hwndTV
)
304 if(!(hItem
= TreeView_GetSelection(hwndTV
))) return 0;
305 return TreeView_EditLabel(hwndTV
, hItem
);
308 static BOOL
InitTreeViewItems(HWND hwndTV
, LPTSTR pHostName
)
311 TVINSERTSTRUCT tvins
;
314 tvi
.mask
= TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
| TVIF_PARAM
;
315 /* Set the text of the item. */
316 tvi
.pszText
= pHostName
;
317 tvi
.cchTextMax
= lstrlen(tvi
.pszText
);
318 /* Assume the item is not a parent item, so give it an image. */
319 tvi
.iImage
= Image_Root
;
320 tvi
.iSelectedImage
= Image_Root
;
322 /* Save the heading level in the item's application-defined data area. */
323 tvi
.lParam
= (LPARAM
)NULL
;
325 tvins
.hInsertAfter
= (HTREEITEM
)TVI_FIRST
;
326 tvins
.hParent
= TVI_ROOT
;
327 /* Add the item to the tree view control. */
328 if (!(hRoot
= TreeView_InsertItem(hwndTV
, &tvins
))) return FALSE
;
330 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_CLASSES_ROOT"), HKEY_CLASSES_ROOT
, 1)) return FALSE
;
331 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_CURRENT_USER"), HKEY_CURRENT_USER
, 1)) return FALSE
;
332 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_LOCAL_MACHINE"), HKEY_LOCAL_MACHINE
, 1)) return FALSE
;
333 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_USERS"), HKEY_USERS
, 1)) return FALSE
;
334 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_CURRENT_CONFIG"), HKEY_CURRENT_CONFIG
, 1)) return FALSE
;
335 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_DYN_DATA"), HKEY_DYN_DATA
, 1)) return FALSE
;
337 /* expand and select host name */
338 TreeView_Expand(hwndTV
, hRoot
, TVE_EXPAND
);
339 TreeView_Select(hwndTV
, hRoot
, TVGN_CARET
);
345 * InitTreeViewImageLists - creates an image list, adds three bitmaps
346 * to it, and associates the image list with a tree view control.
347 * Returns TRUE if successful, or FALSE otherwise.
348 * hwndTV - handle to the tree view control.
350 static BOOL
InitTreeViewImageLists(HWND hwndTV
)
352 HIMAGELIST himl
; /* handle to image list */
353 HICON hico
; /* handle to icon */
355 /* Create the image list. */
356 if ((himl
= ImageList_Create(CX_ICON
, CY_ICON
,
357 ILC_MASK
, 0, NUM_ICONS
)) == NULL
)
360 /* Add the open file, closed file, and document bitmaps. */
361 hico
= LoadIcon(hInst
, MAKEINTRESOURCE(IDI_OPEN_FILE
));
362 Image_Open
= ImageList_AddIcon(himl
, hico
);
364 hico
= LoadIcon(hInst
, MAKEINTRESOURCE(IDI_CLOSED_FILE
));
365 Image_Closed
= ImageList_AddIcon(himl
, hico
);
367 hico
= LoadIcon(hInst
, MAKEINTRESOURCE(IDI_ROOT
));
368 Image_Root
= ImageList_AddIcon(himl
, hico
);
370 /* Fail if not all of the images were added. */
371 if (ImageList_GetImageCount(himl
) < NUM_ICONS
)
376 /* Associate the image list with the tree view control. */
377 TreeView_SetImageList(hwndTV
, himl
, TVSIL_NORMAL
);
382 BOOL
OnTreeExpanding(HWND hwndTV
, NMTREEVIEW
* pnmtv
)
384 DWORD dwCount
, dwIndex
, dwMaxSubKeyLen
;
385 HKEY hRoot
, hNewKey
, hKey
;
391 static int expanding
;
392 if (expanding
) return FALSE
;
393 if (pnmtv
->itemNew
.state
& TVIS_EXPANDEDONCE
) {
397 hcursorOld
= SetCursor(LoadCursor(NULL
, IDC_WAIT
));
398 SendMessage(hwndTV
, WM_SETREDRAW
, FALSE
, 0);
400 keyPath
= GetItemPath(hwndTV
, pnmtv
->itemNew
.hItem
, &hRoot
);
401 if (!keyPath
) goto done
;
404 errCode
= RegOpenKeyEx(hRoot
, keyPath
, 0, KEY_READ
, &hNewKey
);
405 if (errCode
!= ERROR_SUCCESS
) goto done
;
410 errCode
= RegQueryInfoKey(hNewKey
, 0, 0, 0, &dwCount
, &dwMaxSubKeyLen
, 0, 0, 0, 0, 0, 0);
411 if (errCode
!= ERROR_SUCCESS
) goto done
;
412 dwMaxSubKeyLen
++; /* account for the \0 terminator */
413 Name
= HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen
* sizeof(TCHAR
));
414 if (!Name
) goto done
;
416 for (dwIndex
= 0; dwIndex
< dwCount
; dwIndex
++) {
417 DWORD cName
= dwMaxSubKeyLen
, dwSubCount
;
419 errCode
= RegEnumKeyEx(hNewKey
, dwIndex
, Name
, &cName
, 0, 0, 0, 0);
420 if (errCode
!= ERROR_SUCCESS
) continue;
421 errCode
= RegOpenKeyEx(hNewKey
, Name
, 0, KEY_QUERY_VALUE
, &hKey
);
422 if (errCode
== ERROR_SUCCESS
) {
423 errCode
= RegQueryInfoKey(hKey
, 0, 0, 0, &dwSubCount
, 0, 0, 0, 0, 0, 0, 0);
426 if (errCode
!= ERROR_SUCCESS
) dwSubCount
= 0;
427 AddEntryToTree(hwndTV
, pnmtv
->itemNew
.hItem
, Name
, NULL
, dwSubCount
);
429 RegCloseKey(hNewKey
);
430 HeapFree(GetProcessHeap(), 0, Name
);
433 SendMessage(hwndTV
, WM_SETREDRAW
, TRUE
, 0);
434 SetCursor(hcursorOld
);
442 * CreateTreeView - creates a tree view control.
443 * Returns the handle to the new control if successful, or NULL otherwise.
444 * hwndParent - handle to the control's parent window.
446 HWND
CreateTreeView(HWND hwndParent
, LPTSTR pHostName
, int id
)
451 /* Get the dimensions of the parent window's client area, and create the tree view control. */
452 GetClientRect(hwndParent
, &rcClient
);
453 hwndTV
= CreateWindowEx(WS_EX_CLIENTEDGE
, WC_TREEVIEW
, _T("Tree View"),
454 WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
| TVS_HASLINES
| TVS_HASBUTTONS
| TVS_LINESATROOT
,
455 0, 0, rcClient
.right
, rcClient
.bottom
,
456 hwndParent
, (HMENU
)id
, hInst
, NULL
);
457 /* Initialize the image list, and add items to the control. */
458 if (!InitTreeViewImageLists(hwndTV
) || !InitTreeViewItems(hwndTV
, pHostName
)) {
459 DestroyWindow(hwndTV
);