Merged regedit from winehq cvs.
[reactos.git] / reactos / subsys / system / regedit / treeview.c
1 /*
2 * Regedit treeview
3 *
4 * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #define WIN32_LEAN_AND_MEAN /* Exclude rarely-used stuff from Windows headers */
22
23 #define NONAMELESSUNION
24 #define NONAMELESSSTRUCT
25 #include <windows.h>
26 #include <commctrl.h>
27 #include <stdlib.h>
28 #include <tchar.h>
29 #include <process.h>
30 #include <stdio.h>
31
32 #include "main.h"
33
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. */
38 int Image_Open;
39 int Image_Closed;
40 int Image_Root;
41
42 #define CX_BITMAP 16
43 #define CY_BITMAP 16
44 #define NUM_BITMAPS 3
45
46
47 HKEY FindRegRoot(HWND hwndTV, HTREEITEM hItem, LPTSTR keyPath, int* pPathLen, int max)
48 {
49 HKEY hKey = NULL;
50 TVITEM item;
51 item.mask = TVIF_PARAM;
52 item.hItem = TreeView_GetParent(hwndTV, hItem);
53
54 if (TreeView_GetItem(hwndTV, &item)) {
55 if (item.lParam == 0) {
56 /* recurse */
57 hKey = FindRegRoot(hwndTV, item.hItem, keyPath, pPathLen, max);
58 keyPath[*pPathLen] = _T('\\');
59 ++(*pPathLen);
60 item.mask = TVIF_TEXT;
61 item.hItem = hItem;
62 item.pszText = &keyPath[*pPathLen];
63 item.cchTextMax = max - *pPathLen;
64 if (TreeView_GetItem(hwndTV, &item)) {
65 *pPathLen += _tcslen(item.pszText);
66 }
67 } else {
68 /* found root key with valid key value */
69 hKey = (HKEY)item.lParam;
70 item.mask = TVIF_TEXT;
71 item.hItem = hItem;
72 /* item.pszText = &keyPath[*pPathLen]; */
73 item.pszText = keyPath;
74 item.cchTextMax = max;
75 if (TreeView_GetItem(hwndTV, &item)) {
76 *pPathLen += _tcslen(item.pszText);
77 }
78 }
79 }
80 return hKey;
81 }
82
83 static HTREEITEM AddEntryToTree(HWND hwndTV, HTREEITEM hParent, LPTSTR label, HKEY hKey, DWORD dwChildren)
84 {
85 HTREEITEM hItem = 0;
86 TVITEM tvi;
87 TVINSERTSTRUCT tvins;
88
89 tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
90 tvi.pszText = label;
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;
96 tvins.u.item = tvi;
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);
101 return hItem;
102 }
103
104
105 static BOOL InitTreeViewItems(HWND hwndTV, LPTSTR pHostName)
106 {
107 TVITEM tvi;
108 TVINSERTSTRUCT tvins;
109 HTREEITEM hRoot;
110
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;
118 tvi.cChildren = 5;
119 /* Save the heading level in the item's application-defined data area. */
120 tvi.lParam = (LPARAM)NULL;
121 tvins.u.item = tvi;
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);
126
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);
132
133 return TRUE;
134 }
135
136 /*
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.
141 */
142
143 static BOOL InitTreeViewImageLists(HWND hwndTV)
144 {
145 HIMAGELIST himl; /* handle to image list */
146 HBITMAP hbmp; /* handle to bitmap */
147
148 /* Create the image list. */
149 if ((himl = ImageList_Create(CX_BITMAP, CY_BITMAP,
150 FALSE, NUM_BITMAPS, 0)) == NULL)
151 return FALSE;
152
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);
156 DeleteObject(hbmp);
157
158 hbmp = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_CLOSED_FILE));
159 Image_Closed = ImageList_Add(himl, hbmp, (HBITMAP) NULL);
160 DeleteObject(hbmp);
161
162 hbmp = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_ROOT));
163 Image_Root = ImageList_Add(himl, hbmp, (HBITMAP) NULL);
164 DeleteObject(hbmp);
165
166 /* Fail if not all of the images were added. */
167 if (ImageList_GetImageCount(himl) < 3)
168 return FALSE;
169
170 /* Associate the image list with the tree view control. */
171 TreeView_SetImageList(hwndTV, himl, TVSIL_NORMAL);
172
173 return TRUE;
174 }
175
176 BOOL OnTreeExpanding(HWND hwndTV, NMTREEVIEW* pnmtv)
177 {
178 HKEY hKey;
179 TCHAR keyPath[1000];
180 int keyPathLen = 0;
181
182 static int expanding;
183 if (expanding) return FALSE;
184 if (pnmtv->itemNew.state & TVIS_EXPANDEDONCE ) {
185 return TRUE;
186 }
187 expanding = TRUE;
188
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));
193 } else {
194 hKey = (HKEY)pnmtv->itemNew.lParam;
195 keyPath[0] = _T('\0');
196 }
197
198 if (hKey != NULL) {
199 HKEY hNewKey;
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;
205 DWORD dwIndex = 0L;
206 /*ShowWindow(hwndTV, SW_HIDE); */
207 while (RegEnumKeyEx(hNewKey, dwIndex, Name, &cName, NULL, NULL, NULL, &LastWriteTime) == ERROR_SUCCESS) {
208 DWORD dwCount = 0L;
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) {
215 ++dwCount;
216 }
217 }
218 RegCloseKey(hKey);
219 AddEntryToTree(hwndTV, pnmtv->itemNew.hItem, Name, NULL, dwCount);
220 cName = MAX_NAME_LEN;
221 ++dwIndex;
222 }
223 /*ShowWindow(hwndTV, SW_SHOWNOACTIVATE); */
224 RegCloseKey(hNewKey);
225 }
226 } else {
227 }
228 expanding = FALSE;
229 return TRUE;
230 }
231
232 /*
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.
236 */
237
238 HWND CreateTreeView(HWND hwndParent, LPTSTR pHostName, int id)
239 {
240 RECT rcClient;
241 HWND hwndTV;
242
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);
252 return NULL;
253 }
254 return hwndTV;
255 }