3fe2f61c86e8cdb8d276f8f25e482a0c5be671ee
6 * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
37 //#include <windowsx.h>
42 // Global variables and constants
43 // Image_Open, Image_Closed, and Image_Root - integer variables for indexes of the images.
44 // CX_BITMAP and CY_BITMAP - width and height of an icon.
45 // NUM_BITMAPS - number of bitmaps to add to the image list.
55 static HKEY
FindRegRoot(HWND hwndTV
, HTREEITEM hItem
, LPTSTR keyPath
, int* pPathLen
, int max
)
59 item
.mask
= TVIF_PARAM
;
60 item
.hItem
= TreeView_GetParent(hwndTV
, hItem
);
62 if (TreeView_GetItem(hwndTV
, &item
)) {
63 if (item
.lParam
== 0) {
65 hKey
= FindRegRoot(hwndTV
, item
.hItem
, keyPath
, pPathLen
, max
);
66 keyPath
[*pPathLen
] = _T('\\');
68 item
.mask
= TVIF_TEXT
;
70 item
.pszText
= &keyPath
[*pPathLen
];
71 item
.cchTextMax
= max
- *pPathLen
;
72 if (TreeView_GetItem(hwndTV
, &item
)) {
73 *pPathLen
+= _tcslen(item
.pszText
);
76 // found root key with valid key value
77 hKey
= (HKEY
)item
.lParam
;
78 item
.mask
= TVIF_TEXT
;
80 // item.pszText = &keyPath[*pPathLen];
81 item
.pszText
= keyPath
;
82 item
.cchTextMax
= max
;
83 if (TreeView_GetItem(hwndTV
, &item
)) {
84 *pPathLen
+= _tcslen(item
.pszText
);
91 static HTREEITEM
AddEntryToTree(HWND hwndTV
, HTREEITEM hParent
, LPTSTR label
, HKEY hKey
, DWORD dwChildren
)
97 tvi
.mask
= TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
| TVIF_PARAM
;
99 tvi
.cchTextMax
= lstrlen(tvi
.pszText
);
100 tvi
.iImage
= Image_Closed
;
101 tvi
.iSelectedImage
= Image_Open
;
102 tvi
.cChildren
= dwChildren
;
103 tvi
.lParam
= (LPARAM
)hKey
;
105 if (hKey
) tvins
.hInsertAfter
= (HTREEITEM
)TVI_LAST
;
106 else tvins
.hInsertAfter
= (HTREEITEM
)TVI_SORT
;
107 tvins
.hParent
= hParent
;
108 hItem
= (HTREEITEM
)SendMessage(hwndTV
, TVM_INSERTITEM
, 0, (LPARAM
)(LPTVINSERTSTRUCT
)&tvins
);
113 static BOOL
InitTreeViewItems(HWND hwndTV
, LPTSTR pHostName
)
116 TVINSERTSTRUCT tvins
;
119 tvi
.mask
= TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
| TVIF_PARAM
;
120 // Set the text of the item.
121 tvi
.pszText
= pHostName
;
122 tvi
.cchTextMax
= lstrlen(tvi
.pszText
);
123 // Assume the item is not a parent item, so give it an image.
124 tvi
.iImage
= Image_Root
;
125 tvi
.iSelectedImage
= Image_Root
;
127 // Save the heading level in the item's application-defined data area.
128 tvi
.lParam
= (LPARAM
)NULL
;
130 tvins
.hInsertAfter
= (HTREEITEM
)TVI_FIRST
;
131 tvins
.hParent
= TVI_ROOT
;
132 // Add the item to the tree view control.
133 hRoot
= (HTREEITEM
)SendMessage(hwndTV
, TVM_INSERTITEM
, 0, (LPARAM
)(LPTVINSERTSTRUCT
)&tvins
);
135 AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_CLASSES_ROOT"), HKEY_CLASSES_ROOT
, 1);
136 AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_CURRENT_USER"), HKEY_CURRENT_USER
, 1);
137 AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_LOCAL_MACHINE"), HKEY_LOCAL_MACHINE
, 1);
138 AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_USERS"), HKEY_USERS
, 1);
139 AddEntryToTree(hwndTV
, hRoot
, _T("HKEY_CURRENT_CONFIG"), HKEY_CURRENT_CONFIG
, 1);
144 // InitTreeViewImageLists - creates an image list, adds three bitmaps
145 // to it, and associates the image list with a tree view control.
146 // Returns TRUE if successful, or FALSE otherwise.
147 // hwndTV - handle to the tree view control.
149 static BOOL
InitTreeViewImageLists(HWND hwndTV
)
151 HIMAGELIST himl
; // handle to image list
152 HBITMAP hbmp
; // handle to bitmap
154 // Create the image list.
155 if ((himl
= ImageList_Create(CX_BITMAP
, CY_BITMAP
,
156 FALSE
, NUM_BITMAPS
, 0)) == NULL
)
159 // Add the open file, closed file, and document bitmaps.
160 hbmp
= LoadBitmap(hInst
, MAKEINTRESOURCE(IDB_OPEN_FILE
));
161 Image_Open
= ImageList_Add(himl
, hbmp
, (HBITMAP
) NULL
);
164 hbmp
= LoadBitmap(hInst
, MAKEINTRESOURCE(IDB_CLOSED_FILE
));
165 Image_Closed
= ImageList_Add(himl
, hbmp
, (HBITMAP
) NULL
);
168 hbmp
= LoadBitmap(hInst
, MAKEINTRESOURCE(IDB_ROOT
));
169 Image_Root
= ImageList_Add(himl
, hbmp
, (HBITMAP
) NULL
);
172 // Fail if not all of the images were added.
173 if (ImageList_GetImageCount(himl
) < 3)
176 // Associate the image list with the tree view control.
177 TreeView_SetImageList(hwndTV
, himl
, TVSIL_NORMAL
);
182 BOOL
OnTreeExpanding(HWND hwndTV
, NMTREEVIEW
* pnmtv
)
188 static int expanding
;
189 if (expanding
) return FALSE
;
190 if (pnmtv
->itemNew
.state
& TVIS_EXPANDEDONCE
) {
195 // check if this is either the root or a subkey item...
196 if ((HKEY
)pnmtv
->itemNew
.lParam
== NULL
) {
197 keyPath
[0] = _T('\0');
198 hKey
= FindRegRoot(hwndTV
, pnmtv
->itemNew
.hItem
, keyPath
, &keyPathLen
, sizeof(keyPath
));
200 hKey
= (HKEY
)pnmtv
->itemNew
.lParam
;
201 keyPath
[0] = _T('\0');
206 LONG errCode
= RegOpenKeyEx(hKey
, keyPath
, 0, KEY_READ
, &hNewKey
);
207 if (errCode
== ERROR_SUCCESS
) {
208 TCHAR Name
[MAX_NAME_LEN
];
209 DWORD cName
= MAX_NAME_LEN
;
210 FILETIME LastWriteTime
;
212 ShowWindow(hwndTV
, SW_HIDE
);
213 while (RegEnumKeyEx(hNewKey
, dwIndex
, Name
, &cName
, NULL
, NULL
, NULL
, &LastWriteTime
) == ERROR_SUCCESS
) {
215 errCode
= RegOpenKeyEx(hNewKey
, Name
, 0, KEY_READ
, &hKey
);
216 if (errCode
== ERROR_SUCCESS
) {
217 TCHAR SubName
[MAX_NAME_LEN
];
218 DWORD cSubName
= MAX_NAME_LEN
;
219 // if (RegEnumKeyEx(hKey, 0, SubName, &cSubName, NULL, NULL, NULL, NULL) == ERROR_SUCCESS) {
220 while (RegEnumKeyEx(hKey
, dwCount
, SubName
, &cSubName
, NULL
, NULL
, NULL
, NULL
) == ERROR_SUCCESS
) {
225 AddEntryToTree(hwndTV
, pnmtv
->itemNew
.hItem
, Name
, NULL
, dwCount
);
226 cName
= MAX_NAME_LEN
;
229 ShowWindow(hwndTV
, SW_SHOW
);
230 //UpdateStatus(hwndTV, pnmtv->itemNew.hItem);
231 RegCloseKey(hNewKey
);
239 ////////////////////////////////////////////////////////////////////////////////
241 // CreateTreeView - creates a tree view control.
242 // Returns the handle to the new control if successful, or NULL otherwise.
243 // hwndParent - handle to the control's parent window.
245 HWND
CreateTreeView(HWND hwndParent
, LPTSTR pHostName
, int id
)
250 // Get the dimensions of the parent window's client area, and create the tree view control.
251 GetClientRect(hwndParent
, &rcClient
);
252 hwndTV
= CreateWindowEx(0, WC_TREEVIEW
, _T("Tree View"),
253 WS_VISIBLE
| WS_CHILD
| WS_BORDER
| TVS_HASLINES
| TVS_HASBUTTONS
| TVS_LINESATROOT
,
254 0, 0, rcClient
.right
, rcClient
.bottom
,
255 hwndParent
, (HMENU
)id
, hInst
, NULL
);
256 // Initialize the image list, and add items to the control.
257 if (!InitTreeViewImageLists(hwndTV
) || !InitTreeViewItems(hwndTV
, pHostName
)) {
258 DestroyWindow(hwndTV
);