moved several strings to the application resources
[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_ICON and CY_ICON - width and height of an icon. */
37 /* NUM_ICON - number of icons to add to the image list. */
38 int Image_Open;
39 int Image_Closed;
40 int Image_Root;
41
42 static LPTSTR pathBuffer;
43
44 #define CX_ICON 16
45 #define CY_ICON 16
46 #define NUM_ICONS 3
47
48 static BOOL get_item_path(HWND hwndTV, HTREEITEM hItem, HKEY* phKey, LPTSTR* pKeyPath, int* pPathLen, int* pMaxLen)
49 {
50 TVITEM item;
51 int maxLen, len;
52 LPTSTR newStr;
53
54 item.mask = TVIF_PARAM;
55 item.hItem = hItem;
56 if (!TreeView_GetItem(hwndTV, &item)) return FALSE;
57
58 if (item.lParam) {
59 /* found root key with valid key value */
60 *phKey = (HKEY)item.lParam;
61 return TRUE;
62 }
63
64 if(!get_item_path(hwndTV, TreeView_GetParent(hwndTV, hItem), phKey, pKeyPath, pPathLen, pMaxLen)) return FALSE;
65 if (*pPathLen) {
66 (*pKeyPath)[*pPathLen] = _T('\\');
67 ++(*pPathLen);
68 }
69
70 do {
71 item.mask = TVIF_TEXT;
72 item.hItem = hItem;
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) {
78 *pPathLen += len;
79 break;
80 }
81 newStr = HeapReAlloc(GetProcessHeap(), 0, *pKeyPath, *pMaxLen * 2);
82 if (!newStr) return FALSE;
83 *pKeyPath = newStr;
84 *pMaxLen *= 2;
85 } while(TRUE);
86
87 return TRUE;
88 }
89
90 LPCTSTR GetItemPath(HWND hwndTV, HTREEITEM hItem, HKEY* phRootKey)
91 {
92 int pathLen = 0, maxLen;
93
94 *phRootKey = NULL;
95 if (!pathBuffer) pathBuffer = HeapAlloc(GetProcessHeap(), 0, 1024);
96 if (!pathBuffer) return NULL;
97 *pathBuffer = 0;
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);
104 return pathBuffer;
105 }
106
107 static HTREEITEM AddEntryToTree(HWND hwndTV, HTREEITEM hParent, LPTSTR label, HKEY hKey, DWORD dwChildren, HTREEITEM insAfter )
108 {
109 TVITEM tvi;
110 TVINSERTSTRUCT tvins;
111
112 tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
113 tvi.pszText = label;
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;
119 tvins.u.item = tvi;
120 tvins.hInsertAfter = insAfter;
121 tvins.hParent = hParent;
122 return TreeView_InsertItem(hwndTV, &tvins);
123 }
124
125
126 static BOOL InitTreeViewItems(HWND hwndTV, LPTSTR pHostName)
127 {
128 TVITEM tvi;
129 TVINSERTSTRUCT tvins;
130 HTREEITEM hRoot;
131
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;
139 tvi.cChildren = 5;
140 /* Save the heading level in the item's application-defined data area. */
141 tvi.lParam = (LPARAM)NULL;
142 tvins.u.item = tvi;
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;
147
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;
153
154 /* expand and select host name */
155 TreeView_Expand(hwndTV, hRoot, TVE_EXPAND);
156 TreeView_Select(hwndTV, hRoot, TVGN_CARET);
157 return TRUE;
158 }
159
160
161 /*
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.
166 */
167 static BOOL InitTreeViewImageLists(HWND hwndTV)
168 {
169 HIMAGELIST himl; /* handle to image list */
170 HICON hico; /* handle to icon */
171
172 /* Create the image list. */
173 if ((himl = ImageList_Create(CX_ICON, CY_ICON,
174 ILC_MASK, 0, NUM_ICONS)) == NULL)
175 return FALSE;
176
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);
180
181 hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_CLOSED_FILE));
182 Image_Closed = ImageList_AddIcon(himl, hico);
183
184 hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_ROOT));
185 Image_Root = ImageList_AddIcon(himl, hico);
186
187 /* Fail if not all of the images were added. */
188 if (ImageList_GetImageCount(himl) < NUM_ICONS)
189 {
190 return FALSE;
191 }
192
193 /* Associate the image list with the tree view control. */
194 TreeView_SetImageList(hwndTV, himl, TVSIL_NORMAL);
195
196 return TRUE;
197 }
198
199 BOOL OnTreeExpanding(HWND hwndTV, NMTREEVIEW* pnmtv)
200 {
201 DWORD dwCount, dwIndex, dwMaxSubKeyLen;
202 HKEY hRoot, hNewKey, hKey;
203 LPCTSTR keyPath;
204 LPTSTR Name;
205 LONG errCode;
206
207 static int expanding;
208 if (expanding) return FALSE;
209 if (pnmtv->itemNew.state & TVIS_EXPANDEDONCE ) {
210 return TRUE;
211 }
212 expanding = TRUE;
213
214 keyPath = GetItemPath(hwndTV, pnmtv->itemNew.hItem, &hRoot);
215 if (!keyPath) goto done;
216
217 if (*keyPath) {
218 errCode = RegOpenKeyEx(hRoot, keyPath, 0, KEY_READ, &hNewKey);
219 if (errCode != ERROR_SUCCESS) goto done;
220 } else {
221 hNewKey = hRoot;
222 }
223
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;
229
230 for (dwIndex = 0; dwIndex < dwCount; dwIndex++) {
231 DWORD cName = dwMaxSubKeyLen, dwSubCount;
232 FILETIME LastWriteTime;
233
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);
239 RegCloseKey(hKey);
240 }
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);
244 }
245
246 SendMessage(hwndTV, TVM_SORTCHILDREN, 0, (LPARAM)pnmtv->itemNew.hItem);
247
248 RegCloseKey(hNewKey);
249 HeapFree(GetProcessHeap(), 0, Name);
250
251 done:
252 expanding = FALSE;
253
254 return TRUE;
255 }
256
257
258 /*
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.
262 */
263 HWND CreateTreeView(HWND hwndParent, LPTSTR pHostName, int id)
264 {
265 RECT rcClient;
266 HWND hwndTV;
267
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);
277 return NULL;
278 }
279 return hwndTV;
280 }