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. */
47 HKEY
FindRegRoot(HWND hwndTV
, HTREEITEM hItem
, LPTSTR keyPath
, int* pPathLen
, int max
)
51 item
.mask
= TVIF_PARAM
;
52 item
.hItem
= TreeView_GetParent(hwndTV
, hItem
);
54 if (TreeView_GetItem(hwndTV
, &item
)) {
55 if (item
.lParam
== 0) {
57 hKey
= FindRegRoot(hwndTV
, item
.hItem
, keyPath
, pPathLen
, max
);
58 keyPath
[*pPathLen
] = _T('\\');
60 item
.mask
= TVIF_TEXT
;
62 item
.pszText
= &keyPath
[*pPathLen
];
63 item
.cchTextMax
= max
- *pPathLen
;
64 if (TreeView_GetItem(hwndTV
, &item
)) {
65 *pPathLen
+= _tcslen(item
.pszText
);
68 /* found root key with valid key value */
69 hKey
= (HKEY
)item
.lParam
;
70 item
.mask
= TVIF_TEXT
;
72 /* item.pszText = &keyPath[*pPathLen]; */
73 item
.pszText
= keyPath
;
74 item
.cchTextMax
= max
;
75 if (TreeView_GetItem(hwndTV
, &item
)) {
76 *pPathLen
+= _tcslen(item
.pszText
);
83 static HTREEITEM
AddEntryToTree(HWND hwndTV
, HTREEITEM hParent
, LPTSTR label
, HKEY hKey
, DWORD dwChildren
)
89 tvi
.mask
= TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
| TVIF_PARAM
;
91 tvi
.cchTextMax
= lstrlen(tvi
.pszText
);
92 tvi
.iImage
= Image_Closed
;
93 tvi
.iSelectedImage
= Image_Open
;
94 tvi
.cChildren
= dwChildren
;
95 tvi
.lParam
= (LPARAM
)hKey
;
97 if (hKey
) tvins
.hInsertAfter
= (HTREEITEM
)TVI_LAST
;
98 else tvins
.hInsertAfter
= (HTREEITEM
)TVI_SORT
;
99 tvins
.hParent
= hParent
;
100 hItem
= (HTREEITEM
)SendMessage(hwndTV
, TVM_INSERTITEM
, 0, (LPARAM
)(LPTVINSERTSTRUCT
)&tvins
);
105 static BOOL
InitTreeViewItems(HWND hwndTV
, LPTSTR pHostName
)
108 TVINSERTSTRUCT tvins
;
111 tvi
.mask
= TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
| TVIF_PARAM
;
112 /* Set the text of the item. */
113 tvi
.pszText
= pHostName
;
114 tvi
.cchTextMax
= lstrlen(tvi
.pszText
);
115 /* Assume the item is not a parent item, so give it an image. */
116 tvi
.iImage
= Image_Root
;
117 tvi
.iSelectedImage
= Image_Root
;
119 /* Save the heading level in the item's application-defined data area. */
120 tvi
.lParam
= (LPARAM
)NULL
;
122 tvins
.hInsertAfter
= (HTREEITEM
)TVI_FIRST
;
123 tvins
.hParent
= TVI_ROOT
;
124 /* Add the item to the tree view control. */
125 hRoot
= (HTREEITEM
)SendMessage(hwndTV
, TVM_INSERTITEM
, 0, (LPARAM
)(LPTVINSERTSTRUCT
)&tvins
);
127 AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_CLASSES_ROOT"), HKEY_CLASSES_ROOT
, 1);
128 AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_CURRENT_USER"), HKEY_CURRENT_USER
, 1);
129 AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_LOCAL_MACHINE"), HKEY_LOCAL_MACHINE
, 1);
130 AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_USERS"), HKEY_USERS
, 1);
131 AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_CURRENT_CONFIG"), HKEY_CURRENT_CONFIG
, 1);
137 * InitTreeViewImageLists - creates an image list, adds three bitmaps
138 * to it, and associates the image list with a tree view control.
139 * Returns TRUE if successful, or FALSE otherwise.
140 * hwndTV - handle to the tree view control.
143 static BOOL
InitTreeViewImageLists(HWND hwndTV
)
145 HIMAGELIST himl
; /* handle to image list */
146 HBITMAP hbmp
; /* handle to bitmap */
148 /* Create the image list. */
149 if ((himl
= ImageList_Create(CX_BITMAP
, CY_BITMAP
,
150 FALSE
, NUM_BITMAPS
, 0)) == NULL
)
153 /* Add the open file, closed file, and document bitmaps. */
154 hbmp
= LoadBitmap(hInst
, MAKEINTRESOURCE(IDB_OPEN_FILE
));
155 Image_Open
= ImageList_Add(himl
, hbmp
, (HBITMAP
) NULL
);
158 hbmp
= LoadBitmap(hInst
, MAKEINTRESOURCE(IDB_CLOSED_FILE
));
159 Image_Closed
= ImageList_Add(himl
, hbmp
, (HBITMAP
) NULL
);
162 hbmp
= LoadBitmap(hInst
, MAKEINTRESOURCE(IDB_ROOT
));
163 Image_Root
= ImageList_Add(himl
, hbmp
, (HBITMAP
) NULL
);
166 /* Fail if not all of the images were added. */
167 if (ImageList_GetImageCount(himl
) < 3)
170 /* Associate the image list with the tree view control. */
171 TreeView_SetImageList(hwndTV
, himl
, TVSIL_NORMAL
);
176 BOOL
OnTreeExpanding(HWND hwndTV
, NMTREEVIEW
* pnmtv
)
182 static int expanding
;
183 if (expanding
) return FALSE
;
184 if (pnmtv
->itemNew
.state
& TVIS_EXPANDEDONCE
) {
189 /* check if this is either the root or a subkey item... */
190 if ((HKEY
)pnmtv
->itemNew
.lParam
== NULL
) {
191 keyPath
[0] = _T('\0');
192 hKey
= FindRegRoot(hwndTV
, pnmtv
->itemNew
.hItem
, keyPath
, &keyPathLen
, sizeof(keyPath
)/sizeof(TCHAR
));
194 hKey
= (HKEY
)pnmtv
->itemNew
.lParam
;
195 keyPath
[0] = _T('\0');
200 LONG errCode
= RegOpenKeyEx(hKey
, keyPath
, 0, KEY_READ
, &hNewKey
);
201 if (errCode
== ERROR_SUCCESS
) {
202 TCHAR Name
[MAX_NAME_LEN
];
203 DWORD cName
= MAX_NAME_LEN
;
204 FILETIME LastWriteTime
;
206 /*ShowWindow(hwndTV, SW_HIDE); */
207 while (RegEnumKeyEx(hNewKey
, dwIndex
, Name
, &cName
, NULL
, NULL
, NULL
, &LastWriteTime
) == ERROR_SUCCESS
) {
209 errCode
= RegOpenKeyEx(hNewKey
, Name
, 0, KEY_READ
, &hKey
);
210 if (errCode
== ERROR_SUCCESS
) {
211 TCHAR SubName
[MAX_NAME_LEN
];
212 DWORD cSubName
= MAX_NAME_LEN
;
213 /* if (RegEnumKeyEx(hKey, 0, SubName, &cSubName, NULL, NULL, NULL, NULL) == ERROR_SUCCESS) { */
214 while (RegEnumKeyEx(hKey
, dwCount
, SubName
, &cSubName
, NULL
, NULL
, NULL
, NULL
) == ERROR_SUCCESS
) {
219 AddEntryToTree(hwndTV
, pnmtv
->itemNew
.hItem
, Name
, NULL
, dwCount
);
220 cName
= MAX_NAME_LEN
;
223 /*ShowWindow(hwndTV, SW_SHOWNOACTIVATE); */
224 RegCloseKey(hNewKey
);
233 * CreateTreeView - creates a tree view control.
234 * Returns the handle to the new control if successful, or NULL otherwise.
235 * hwndParent - handle to the control's parent window.
238 HWND
CreateTreeView(HWND hwndParent
, LPTSTR pHostName
, int id
)
243 /* Get the dimensions of the parent window's client area, and create the tree view control. */
244 GetClientRect(hwndParent
, &rcClient
);
245 hwndTV
= CreateWindowEx(WS_EX_CLIENTEDGE
, WC_TREEVIEW
, _T("Tree View"),
246 WS_VISIBLE
| WS_CHILD
| TVS_HASLINES
| TVS_HASBUTTONS
| TVS_LINESATROOT
,
247 0, 0, rcClient
.right
, rcClient
.bottom
,
248 hwndParent
, (HMENU
)id
, hInst
, NULL
);
249 /* Initialize the image list, and add items to the control. */
250 if (!InitTreeViewImageLists(hwndTV
) || !InitTreeViewItems(hwndTV
, pHostName
)) {
251 DestroyWindow(hwndTV
);