Fixes for -Wmissing-declarations and -Wwrite-strings warnings.
[wine/wine64.git] / programs / regedit / treeview.c
blob0599ddf30e9246c9748f4aa9af9a94600342a255
1 /*
2 * Regedit treeview
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
25 #include <windows.h>
26 #include <commctrl.h>
27 #include <stdlib.h>
28 #include <tchar.h>
29 #include <stdio.h>
30 #include <wine/debug.h>
32 #include "main.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(regedit);
36 /* Global variables and constants */
37 /* Image_Open, Image_Closed, and Image_Root - integer variables for indexes of the images. */
38 /* CX_BITMAP and CY_BITMAP - width and height of an icon. */
39 /* NUM_BITMAPS - number of bitmaps to add to the image list. */
40 int Image_Open;
41 int Image_Closed;
42 int Image_Root;
44 static LPTSTR pathBuffer;
46 #define CX_ICON 16
47 #define CY_ICON 16
48 #define NUM_ICONS 3
50 static BOOL get_item_path(HWND hwndTV, HTREEITEM hItem, HKEY* phKey, LPTSTR* pKeyPath, int* pPathLen, int* pMaxLen)
52 TVITEM item;
53 int maxLen, len;
54 LPTSTR newStr;
56 item.mask = TVIF_PARAM;
57 item.hItem = hItem;
58 if (!TreeView_GetItem(hwndTV, &item)) return FALSE;
60 if (item.lParam) {
61 /* found root key with valid key value */
62 *phKey = (HKEY)item.lParam;
63 return TRUE;
66 if(!get_item_path(hwndTV, TreeView_GetParent(hwndTV, hItem), phKey, pKeyPath, pPathLen, pMaxLen)) return FALSE;
67 if (*pPathLen) {
68 (*pKeyPath)[*pPathLen] = _T('\\');
69 ++(*pPathLen);
72 do {
73 item.mask = TVIF_TEXT;
74 item.hItem = hItem;
75 item.pszText = *pKeyPath + *pPathLen;
76 item.cchTextMax = maxLen = *pMaxLen - *pPathLen;
77 if (!TreeView_GetItem(hwndTV, &item)) return FALSE;
78 len = _tcslen(item.pszText);
79 if (len < maxLen - 1) {
80 *pPathLen += len;
81 break;
83 newStr = HeapReAlloc(GetProcessHeap(), 0, *pKeyPath, *pMaxLen * 2);
84 if (!newStr) return FALSE;
85 *pKeyPath = newStr;
86 *pMaxLen *= 2;
87 } while(TRUE);
89 return TRUE;
92 LPCTSTR GetItemPath(HWND hwndTV, HTREEITEM hItem, HKEY* phRootKey)
94 int pathLen = 0, maxLen;
96 if (!pathBuffer) pathBuffer = HeapAlloc(GetProcessHeap(), 0, 1024);
97 if (!pathBuffer) return NULL;
98 *pathBuffer = 0;
99 maxLen = HeapSize(GetProcessHeap(), 0, pathBuffer);
100 if (maxLen == (SIZE_T) - 1) return NULL;
101 if (!hItem) hItem = TreeView_GetSelection(hwndTV);
102 if (!hItem) return NULL;
103 if (!get_item_path(hwndTV, hItem, phRootKey, &pathBuffer, &pathLen, &maxLen)) return NULL;
104 return pathBuffer;
107 BOOL DeleteNode(HWND hwndTV, HTREEITEM hItem)
109 if (!hItem) hItem = TreeView_GetSelection(hwndTV);
110 if (!hItem) return FALSE;
111 return TreeView_DeleteItem(hwndTV, hItem);
114 /* Add an entry to the tree. Only give hKey for root nodes (HKEY_ constants) */
115 static HTREEITEM AddEntryToTree(HWND hwndTV, HTREEITEM hParent, LPTSTR label, HKEY hKey, DWORD dwChildren)
117 TVITEM tvi;
118 TVINSERTSTRUCT tvins;
120 if (hKey) {
121 if (RegQueryInfoKey(hKey, 0, 0, 0, &dwChildren, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
122 dwChildren = 0;
126 tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
127 tvi.pszText = label;
128 tvi.cchTextMax = lstrlen(tvi.pszText);
129 tvi.iImage = Image_Closed;
130 tvi.iSelectedImage = Image_Open;
131 tvi.cChildren = dwChildren;
132 tvi.lParam = (LPARAM)hKey;
133 tvins.u.item = tvi;
134 tvins.hInsertAfter = (HTREEITEM)(hKey ? TVI_LAST : TVI_SORT);
135 tvins.hParent = hParent;
136 return TreeView_InsertItem(hwndTV, &tvins);
139 static BOOL RefreshTreeItem(HWND hwndTV, HTREEITEM hItem)
141 HKEY hRoot, hKey, hSubKey;
142 HTREEITEM childItem;
143 LPCTSTR KeyPath;
144 DWORD dwCount, dwIndex, dwMaxSubKeyLen;
145 LPSTR Name;
146 TVITEM tvItem;
148 KeyPath = GetItemPath(hwndTV, hItem, &hRoot);
150 if (*KeyPath) {
151 if (RegOpenKeyEx(hRoot, KeyPath, 0, KEY_READ, &hKey) != ERROR_SUCCESS) {
152 WINE_TRACE("RegOpenKeyEx failed, \"%s\" was probably removed.\n", KeyPath);
153 return FALSE;
155 } else {
156 hKey = hRoot;
159 if (RegQueryInfoKey(hKey, 0, 0, 0, &dwCount, &dwMaxSubKeyLen, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
160 return FALSE;
163 /* Set the number of children again */
164 tvItem.mask = TVIF_CHILDREN;
165 tvItem.hItem = hItem;
166 tvItem.cChildren = dwCount;
167 if (!TreeView_SetItem(hwndTV, &tvItem)) {
168 return FALSE;
171 /* We don't have to bother with the rest if it's not expanded. */
172 if (TreeView_GetItemState(hwndTV, hItem, TVIS_EXPANDED) == 0) {
173 RegCloseKey(hKey);
174 return TRUE;
177 dwMaxSubKeyLen++; /* account for the \0 terminator */
178 if (!(Name = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(TCHAR)))) {
179 return FALSE;
181 tvItem.cchTextMax = dwMaxSubKeyLen;
182 if (!(tvItem.pszText = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(TCHAR)))) {
183 return FALSE;
186 /* Now go through all the children in the registry, and check if any have to be added. */
187 for (dwIndex = 0; dwIndex < dwCount; dwIndex++) {
188 DWORD cName = dwMaxSubKeyLen, dwSubCount;
189 BOOL found;
191 found = FALSE;
192 if (RegEnumKeyEx(hKey, dwIndex, Name, &cName, 0, 0, 0, NULL) != ERROR_SUCCESS) {
193 continue;
196 /* Find the number of children of the node. */
197 dwSubCount = 0;
198 if (RegOpenKeyEx(hKey, Name, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
199 if (RegQueryInfoKey(hSubKey, 0, 0, 0, &dwSubCount, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
200 dwSubCount = 0;
202 RegCloseKey(hSubKey);
205 /* Check if the node is already in there. */
206 for (childItem = TreeView_GetChild(hwndTV, hItem); childItem;
207 childItem = TreeView_GetNextSibling(hwndTV, childItem)) {
208 tvItem.mask = TVIF_TEXT;
209 tvItem.hItem = childItem;
210 if (!TreeView_GetItem(hwndTV, &tvItem)) {
211 return FALSE;
214 if (!strcmp(tvItem.pszText, Name)) {
215 found = TRUE;
216 break;
220 if (found == FALSE) {
221 WINE_TRACE("New subkey %s\n", Name);
222 AddEntryToTree(hwndTV, hItem, Name, NULL, dwSubCount);
225 HeapFree(GetProcessHeap(), 0, Name);
226 HeapFree(GetProcessHeap(), 0, tvItem.pszText);
227 RegCloseKey(hKey);
229 /* Now go through all the children in the tree, and check if any have to be removed. */
230 childItem = TreeView_GetChild(hwndTV, hItem);
231 while (childItem) {
232 HTREEITEM nextItem = TreeView_GetNextSibling(hwndTV, childItem);
233 if (RefreshTreeItem(hwndTV, childItem) == FALSE) {
234 TreeView_DeleteItem(hwndTV, childItem);
236 childItem = nextItem;
239 return TRUE;
242 BOOL RefreshTreeView(HWND hwndTV)
244 HTREEITEM hItem;
245 HTREEITEM hSelectedItem;
246 HCURSOR hcursorOld;
248 WINE_TRACE("\n");
249 hSelectedItem = TreeView_GetSelection(hwndTV);
250 hcursorOld = SetCursor(LoadCursor(NULL, IDC_WAIT));
251 SendMessage(hwndTV, WM_SETREDRAW, FALSE, 0);
253 hItem = TreeView_GetChild(hwndTV, TreeView_GetRoot(hwndTV));
254 while (hItem) {
255 RefreshTreeItem(hwndTV, hItem);
256 hItem = TreeView_GetNextSibling(hwndTV, hItem);
259 SendMessage(hwndTV, WM_SETREDRAW, TRUE, 0);
260 SetCursor(hcursorOld);
262 /* We reselect the currently selected node, this will prompt a refresh of the listview. */
263 TreeView_SelectItem(hwndTV, hSelectedItem);
264 return TRUE;
267 HTREEITEM InsertNode(HWND hwndTV, HTREEITEM hItem, LPTSTR name)
269 TCHAR buf[MAX_NEW_KEY_LEN];
270 HTREEITEM hNewItem = 0;
271 TVITEMEX item;
273 if (!hItem) hItem = TreeView_GetSelection(hwndTV);
274 if (!hItem) return FALSE;
275 if (TreeView_GetItemState(hwndTV, hItem, TVIS_EXPANDEDONCE)) {
276 hNewItem = AddEntryToTree(hwndTV, hItem, name, 0, 0);
277 } else {
278 item.mask = TVIF_CHILDREN | TVIF_HANDLE;
279 item.hItem = hItem;
280 if (!TreeView_GetItem(hwndTV, &item)) return FALSE;
281 item.cChildren = 1;
282 if (!TreeView_SetItem(hwndTV, &item)) return FALSE;
284 TreeView_Expand(hwndTV, hItem, TVE_EXPAND);
285 if (!hNewItem) {
286 for(hNewItem = TreeView_GetChild(hwndTV, hItem); hNewItem; hNewItem = TreeView_GetNextSibling(hwndTV, hNewItem)) {
287 item.mask = TVIF_HANDLE | TVIF_TEXT;
288 item.hItem = hNewItem;
289 item.pszText = buf;
290 item.cchTextMax = COUNT_OF(buf);
291 if (!TreeView_GetItem(hwndTV, &item)) continue;
292 if (lstrcmp(name, item.pszText) == 0) break;
295 if (hNewItem) TreeView_SelectItem(hwndTV, hNewItem);
297 return hNewItem;
300 HWND StartKeyRename(HWND hwndTV)
302 HTREEITEM hItem;
304 if(!(hItem = TreeView_GetSelection(hwndTV))) return 0;
305 return TreeView_EditLabel(hwndTV, hItem);
308 static BOOL InitTreeViewItems(HWND hwndTV, LPTSTR pHostName)
310 TVITEM tvi;
311 TVINSERTSTRUCT tvins;
312 HTREEITEM hRoot;
314 tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
315 /* Set the text of the item. */
316 tvi.pszText = pHostName;
317 tvi.cchTextMax = lstrlen(tvi.pszText);
318 /* Assume the item is not a parent item, so give it an image. */
319 tvi.iImage = Image_Root;
320 tvi.iSelectedImage = Image_Root;
321 tvi.cChildren = 5;
322 /* Save the heading level in the item's application-defined data area. */
323 tvi.lParam = (LPARAM)NULL;
324 tvins.u.item = tvi;
325 tvins.hInsertAfter = (HTREEITEM)TVI_FIRST;
326 tvins.hParent = TVI_ROOT;
327 /* Add the item to the tree view control. */
328 if (!(hRoot = TreeView_InsertItem(hwndTV, &tvins))) return FALSE;
330 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_CLASSES_ROOT"), HKEY_CLASSES_ROOT, 1)) return FALSE;
331 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_CURRENT_USER"), HKEY_CURRENT_USER, 1)) return FALSE;
332 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_LOCAL_MACHINE"), HKEY_LOCAL_MACHINE, 1)) return FALSE;
333 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_USERS"), HKEY_USERS, 1)) return FALSE;
334 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_CURRENT_CONFIG"), HKEY_CURRENT_CONFIG, 1)) return FALSE;
335 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_DYN_DATA"), HKEY_DYN_DATA, 1)) return FALSE;
337 /* expand and select host name */
338 TreeView_Expand(hwndTV, hRoot, TVE_EXPAND);
339 TreeView_Select(hwndTV, hRoot, TVGN_CARET);
340 return TRUE;
345 * InitTreeViewImageLists - creates an image list, adds three bitmaps
346 * to it, and associates the image list with a tree view control.
347 * Returns TRUE if successful, or FALSE otherwise.
348 * hwndTV - handle to the tree view control.
350 static BOOL InitTreeViewImageLists(HWND hwndTV)
352 HIMAGELIST himl; /* handle to image list */
353 HICON hico; /* handle to icon */
355 /* Create the image list. */
356 if ((himl = ImageList_Create(CX_ICON, CY_ICON,
357 ILC_MASK, 0, NUM_ICONS)) == NULL)
358 return FALSE;
360 /* Add the open file, closed file, and document bitmaps. */
361 hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_OPEN_FILE));
362 Image_Open = ImageList_AddIcon(himl, hico);
364 hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_CLOSED_FILE));
365 Image_Closed = ImageList_AddIcon(himl, hico);
367 hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_ROOT));
368 Image_Root = ImageList_AddIcon(himl, hico);
370 /* Fail if not all of the images were added. */
371 if (ImageList_GetImageCount(himl) < NUM_ICONS)
373 return FALSE;
376 /* Associate the image list with the tree view control. */
377 TreeView_SetImageList(hwndTV, himl, TVSIL_NORMAL);
379 return TRUE;
382 BOOL OnTreeExpanding(HWND hwndTV, NMTREEVIEW* pnmtv)
384 DWORD dwCount, dwIndex, dwMaxSubKeyLen;
385 HKEY hRoot, hNewKey, hKey;
386 LPCTSTR keyPath;
387 LPTSTR Name;
388 LONG errCode;
389 HCURSOR hcursorOld;
391 static int expanding;
392 if (expanding) return FALSE;
393 if (pnmtv->itemNew.state & TVIS_EXPANDEDONCE ) {
394 return TRUE;
396 expanding = TRUE;
397 hcursorOld = SetCursor(LoadCursor(NULL, IDC_WAIT));
398 SendMessage(hwndTV, WM_SETREDRAW, FALSE, 0);
400 keyPath = GetItemPath(hwndTV, pnmtv->itemNew.hItem, &hRoot);
401 if (!keyPath) goto done;
403 if (*keyPath) {
404 errCode = RegOpenKeyEx(hRoot, keyPath, 0, KEY_READ, &hNewKey);
405 if (errCode != ERROR_SUCCESS) goto done;
406 } else {
407 hNewKey = hRoot;
410 errCode = RegQueryInfoKey(hNewKey, 0, 0, 0, &dwCount, &dwMaxSubKeyLen, 0, 0, 0, 0, 0, 0);
411 if (errCode != ERROR_SUCCESS) goto done;
412 dwMaxSubKeyLen++; /* account for the \0 terminator */
413 Name = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(TCHAR));
414 if (!Name) goto done;
416 for (dwIndex = 0; dwIndex < dwCount; dwIndex++) {
417 DWORD cName = dwMaxSubKeyLen, dwSubCount;
419 errCode = RegEnumKeyEx(hNewKey, dwIndex, Name, &cName, 0, 0, 0, 0);
420 if (errCode != ERROR_SUCCESS) continue;
421 errCode = RegOpenKeyEx(hNewKey, Name, 0, KEY_QUERY_VALUE, &hKey);
422 if (errCode == ERROR_SUCCESS) {
423 errCode = RegQueryInfoKey(hKey, 0, 0, 0, &dwSubCount, 0, 0, 0, 0, 0, 0, 0);
424 RegCloseKey(hKey);
426 if (errCode != ERROR_SUCCESS) dwSubCount = 0;
427 AddEntryToTree(hwndTV, pnmtv->itemNew.hItem, Name, NULL, dwSubCount);
429 RegCloseKey(hNewKey);
430 HeapFree(GetProcessHeap(), 0, Name);
432 done:
433 SendMessage(hwndTV, WM_SETREDRAW, TRUE, 0);
434 SetCursor(hcursorOld);
435 expanding = FALSE;
437 return TRUE;
442 * CreateTreeView - creates a tree view control.
443 * Returns the handle to the new control if successful, or NULL otherwise.
444 * hwndParent - handle to the control's parent window.
446 HWND CreateTreeView(HWND hwndParent, LPTSTR pHostName, int id)
448 RECT rcClient;
449 HWND hwndTV;
451 /* Get the dimensions of the parent window's client area, and create the tree view control. */
452 GetClientRect(hwndParent, &rcClient);
453 hwndTV = CreateWindowEx(WS_EX_CLIENTEDGE, WC_TREEVIEW, _T("Tree View"),
454 WS_VISIBLE | WS_CHILD | WS_TABSTOP | TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT,
455 0, 0, rcClient.right, rcClient.bottom,
456 hwndParent, (HMENU)id, hInst, NULL);
457 /* Initialize the image list, and add items to the control. */
458 if (!InitTreeViewImageLists(hwndTV) || !InitTreeViewItems(hwndTV, pHostName)) {
459 DestroyWindow(hwndTV);
460 return NULL;
462 return hwndTV;