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_ICON and CY_ICON - width and height of an icon. */
37 /* NUM_ICON - number of icons 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
;
95 if (!pathBuffer
) pathBuffer
= HeapAlloc(GetProcessHeap(), 0, 1024);
96 if (!pathBuffer
) return NULL
;
98 maxLen
= HeapSize(GetProcessHeap(), 0, pathBuffer
);
99 if (maxLen
== (SIZE_T
) - 1) return NULL
;
100 if (!hItem
) hItem
= TreeView_GetSelection(hwndTV
);
101 if (!hItem
) return NULL
;
102 if (!get_item_path(hwndTV
, hItem
, phRootKey
, &pathBuffer
, &pathLen
, &maxLen
)) return NULL
;
103 printf("hRoot=%p, keyPath='%s'\n", *phRootKey
, pathBuffer
);
107 static HTREEITEM
AddEntryToTree(HWND hwndTV
, HTREEITEM hParent
, LPTSTR label
, HKEY hKey
, DWORD dwChildren
, HTREEITEM insAfter
)
110 TVINSERTSTRUCT tvins
;
112 tvi
.mask
= TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
| TVIF_PARAM
;
114 tvi
.cchTextMax
= _tcslen(tvi
.pszText
);
115 tvi
.iImage
= Image_Closed
;
116 tvi
.iSelectedImage
= Image_Open
;
117 tvi
.cChildren
= dwChildren
;
118 tvi
.lParam
= (LPARAM
)hKey
;
120 tvins
.hInsertAfter
= insAfter
;
121 tvins
.hParent
= hParent
;
122 return TreeView_InsertItem(hwndTV
, &tvins
);
126 static BOOL
InitTreeViewItems(HWND hwndTV
, LPTSTR pHostName
)
129 TVINSERTSTRUCT tvins
;
132 tvi
.mask
= TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
| TVIF_PARAM
;
133 /* Set the text of the item. */
134 tvi
.pszText
= pHostName
;
135 tvi
.cchTextMax
= _tcslen(tvi
.pszText
);
136 /* Assume the item is not a parent item, so give it an image. */
137 tvi
.iImage
= Image_Root
;
138 tvi
.iSelectedImage
= Image_Root
;
140 /* Save the heading level in the item's application-defined data area. */
141 tvi
.lParam
= (LPARAM
)NULL
;
143 tvins
.hInsertAfter
= (HTREEITEM
)TVI_FIRST
;
144 tvins
.hParent
= TVI_ROOT
;
145 /* Add the item to the tree view control. */
146 if (!(hRoot
= TreeView_InsertItem(hwndTV
, &tvins
))) return FALSE
;
148 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_CLASSES_ROOT"), HKEY_CLASSES_ROOT
, 1, TVI_LAST
)) return FALSE
;
149 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_CURRENT_USER"), HKEY_CURRENT_USER
, 1, TVI_LAST
)) return FALSE
;
150 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_LOCAL_MACHINE"), HKEY_LOCAL_MACHINE
, 1, TVI_LAST
)) return FALSE
;
151 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_USERS"), HKEY_USERS
, 1, TVI_LAST
)) return FALSE
;
152 if (!AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_CURRENT_CONFIG"), HKEY_CURRENT_CONFIG
, 1, TVI_LAST
)) return FALSE
;
154 /* expand and select host name */
155 TreeView_Expand(hwndTV
, hRoot
, TVE_EXPAND
);
156 TreeView_Select(hwndTV
, hRoot
, TVGN_CARET
);
162 * InitTreeViewImageLists - creates an image list, adds three bitmaps
163 * to it, and associates the image list with a tree view control.
164 * Returns TRUE if successful, or FALSE otherwise.
165 * hwndTV - handle to the tree view control.
167 static BOOL
InitTreeViewImageLists(HWND hwndTV
)
169 HIMAGELIST himl
; /* handle to image list */
170 HICON hico
; /* handle to icon */
172 /* Create the image list. */
173 if ((himl
= ImageList_Create(CX_ICON
, CY_ICON
,
174 ILC_MASK
, 0, NUM_ICONS
)) == NULL
)
177 /* Add the open file, closed file, and document bitmaps. */
178 hico
= LoadIcon(hInst
, MAKEINTRESOURCE(IDI_OPEN_FILE
));
179 Image_Open
= ImageList_AddIcon(himl
, hico
);
181 hico
= LoadIcon(hInst
, MAKEINTRESOURCE(IDI_CLOSED_FILE
));
182 Image_Closed
= ImageList_AddIcon(himl
, hico
);
184 hico
= LoadIcon(hInst
, MAKEINTRESOURCE(IDI_ROOT
));
185 Image_Root
= ImageList_AddIcon(himl
, hico
);
187 /* Fail if not all of the images were added. */
188 if (ImageList_GetImageCount(himl
) < NUM_ICONS
)
193 /* Associate the image list with the tree view control. */
194 TreeView_SetImageList(hwndTV
, himl
, TVSIL_NORMAL
);
199 BOOL
OnTreeExpanding(HWND hwndTV
, NMTREEVIEW
* pnmtv
)
201 DWORD dwCount
, dwIndex
, dwMaxSubKeyLen
;
202 HKEY hRoot
, hNewKey
, hKey
;
207 static int expanding
;
208 if (expanding
) return FALSE
;
209 if (pnmtv
->itemNew
.state
& TVIS_EXPANDEDONCE
) {
214 keyPath
= GetItemPath(hwndTV
, pnmtv
->itemNew
.hItem
, &hRoot
);
215 if (!keyPath
) goto done
;
218 errCode
= RegOpenKeyEx(hRoot
, keyPath
, 0, KEY_READ
, &hNewKey
);
219 if (errCode
!= ERROR_SUCCESS
) goto done
;
224 errCode
= RegQueryInfoKey(hNewKey
, 0, 0, 0, &dwCount
, &dwMaxSubKeyLen
, 0, 0, 0, 0, 0, 0);
225 if (errCode
!= ERROR_SUCCESS
) goto done
;
226 dwMaxSubKeyLen
++; /* account for the \0 terminator */
227 Name
= HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen
* sizeof(TCHAR
));
228 if (!Name
) goto done
;
230 for (dwIndex
= 0; dwIndex
< dwCount
; dwIndex
++) {
231 DWORD cName
= dwMaxSubKeyLen
, dwSubCount
;
232 FILETIME LastWriteTime
;
234 errCode
= RegEnumKeyEx(hNewKey
, dwIndex
, Name
, &cName
, 0, 0, 0, &LastWriteTime
);
235 if (errCode
!= ERROR_SUCCESS
) continue;
236 errCode
= RegOpenKeyEx(hNewKey
, Name
, 0, KEY_QUERY_VALUE
, &hKey
);
237 if (errCode
== ERROR_SUCCESS
) {
238 errCode
= RegQueryInfoKey(hKey
, 0, 0, 0, &dwSubCount
, 0, 0, 0, 0, 0, 0, 0);
241 if (errCode
!= ERROR_SUCCESS
) dwSubCount
= 0;
242 printf("dwSubCount=%ld, Name=%s\n", dwSubCount
, Name
);
243 AddEntryToTree(hwndTV
, pnmtv
->itemNew
.hItem
, Name
, NULL
, dwSubCount
, TVI_FIRST
);
246 SendMessage(hwndTV
, TVM_SORTCHILDREN
, 0, (LPARAM
)pnmtv
->itemNew
.hItem
);
248 RegCloseKey(hNewKey
);
249 HeapFree(GetProcessHeap(), 0, Name
);
259 * CreateTreeView - creates a tree view control.
260 * Returns the handle to the new control if successful, or NULL otherwise.
261 * hwndParent - handle to the control's parent window.
263 HWND
CreateTreeView(HWND hwndParent
, LPTSTR pHostName
, int id
)
268 /* Get the dimensions of the parent window's client area, and create the tree view control. */
269 GetClientRect(hwndParent
, &rcClient
);
270 hwndTV
= CreateWindowEx(WS_EX_CLIENTEDGE
, WC_TREEVIEW
, NULL
,
271 WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
| TVS_HASLINES
| TVS_HASBUTTONS
| TVS_LINESATROOT
,
272 0, 0, rcClient
.right
, rcClient
.bottom
,
273 hwndParent
, (HMENU
)id
, hInst
, NULL
);
274 /* Initialize the image list, and add items to the control. */
275 if (!InitTreeViewImageLists(hwndTV
) || !InitTreeViewItems(hwndTV
, pHostName
)) {
276 DestroyWindow(hwndTV
);