Implement BeginUpdateResource and UpdateResource.
[wine.git] / programs / regedit / treeview.c
blobb91493860e415e9951fe800f933d044947f8e315
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 <process.h>
30 #include <stdio.h>
32 #include "main.h"
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;
42 static LPTSTR pathBuffer;
44 #define CX_ICON 16
45 #define CY_ICON 16
46 #define NUM_ICONS 3
48 static BOOL get_item_path(HWND hwndTV, HTREEITEM hItem, HKEY* phKey, LPTSTR* pKeyPath, int* pPathLen, int* pMaxLen)
50 TVITEM item;
51 int maxLen, len;
52 LPTSTR newStr;
54 item.mask = TVIF_PARAM;
55 item.hItem = hItem;
56 if (!TreeView_GetItem(hwndTV, &item)) return FALSE;
58 if (item.lParam) {
59 /* found root key with valid key value */
60 *phKey = (HKEY)item.lParam;
61 return TRUE;
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);
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;
81 newStr = HeapReAlloc(GetProcessHeap(), 0, *pKeyPath, *pMaxLen * 2);
82 if (!newStr) return FALSE;
83 *pKeyPath = newStr;
84 *pMaxLen *= 2;
85 } while(TRUE);
87 return TRUE;
90 LPCTSTR GetItemPath(HWND hwndTV, HTREEITEM hItem, HKEY* phRootKey)
92 int pathLen = 0, maxLen;
94 if (!pathBuffer) pathBuffer = HeapAlloc(GetProcessHeap(), 0, 1024);
95 if (!pathBuffer) return NULL;
96 *pathBuffer = 0;
97 maxLen = HeapSize(GetProcessHeap(), 0, pathBuffer);
98 if (maxLen == (SIZE_T) - 1) return NULL;
99 if (!hItem) hItem = TreeView_GetSelection(hwndTV);
100 if (!hItem) return NULL;
101 if (!get_item_path(hwndTV, hItem, phRootKey, &pathBuffer, &pathLen, &maxLen)) return NULL;
102 return pathBuffer;
105 BOOL DeleteNode(HWND hwndTV, HTREEITEM hItem)
107 if (!hItem) hItem = TreeView_GetSelection(hwndTV);
108 if (!hItem) return FALSE;
109 return TreeView_DeleteItem(hwndTV, hItem);
112 static HTREEITEM AddEntryToTree(HWND hwndTV, HTREEITEM hParent, LPTSTR label, HKEY hKey, DWORD dwChildren)
114 TVITEM tvi;
115 TVINSERTSTRUCT tvins;
117 tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
118 tvi.pszText = label;
119 tvi.cchTextMax = lstrlen(tvi.pszText);
120 tvi.iImage = Image_Closed;
121 tvi.iSelectedImage = Image_Open;
122 tvi.cChildren = dwChildren;
123 tvi.lParam = (LPARAM)hKey;
124 tvins.u.item = tvi;
125 tvins.hInsertAfter = (HTREEITEM)(hKey ? TVI_LAST : TVI_SORT);
126 tvins.hParent = hParent;
127 return TreeView_InsertItem(hwndTV, &tvins);
131 HTREEITEM InsertNode(HWND hwndTV, HTREEITEM hItem, LPTSTR name)
133 TCHAR buf[MAX_NEW_KEY_LEN];
134 HTREEITEM hNewItem = 0;
135 TVITEMEX item;
137 if (!hItem) hItem = TreeView_GetSelection(hwndTV);
138 if (!hItem) return FALSE;
139 if (TreeView_GetItemState(hwndTV, hItem, TVIS_EXPANDEDONCE)) {
140 hNewItem = AddEntryToTree(hwndTV, hItem, name, 0, 0);
141 } else {
142 item.mask = TVIF_CHILDREN | TVIF_HANDLE;
143 item.hItem = hItem;
144 if (!TreeView_GetItem(hwndTV, &item)) return FALSE;
145 item.cChildren = 1;
146 if (!TreeView_SetItem(hwndTV, &item)) return FALSE;
148 TreeView_Expand(hwndTV, hItem, TVE_EXPAND);
149 if (!hNewItem) {
150 for(hNewItem = TreeView_GetChild(hwndTV, hItem); hNewItem; hNewItem = TreeView_GetNextSibling(hwndTV, hNewItem)) {
151 item.mask = TVIF_HANDLE | TVIF_TEXT;
152 item.hItem = hNewItem;
153 item.pszText = buf;
154 item.cchTextMax = COUNT_OF(buf);
155 if (!TreeView_GetItem(hwndTV, &item)) continue;
156 if (lstrcmp(name, item.pszText) == 0) break;
159 if (hNewItem) TreeView_SelectItem(hwndTV, hNewItem);
161 return hNewItem;
164 HWND StartKeyRename(HWND hwndTV)
166 HTREEITEM hItem;
168 if(!(hItem = TreeView_GetSelection(hwndTV))) return 0;
169 return TreeView_EditLabel(hwndTV, hItem);
172 static BOOL InitTreeViewItems(HWND hwndTV, LPTSTR pHostName)
174 TVITEM tvi;
175 TVINSERTSTRUCT tvins;
176 HTREEITEM hRoot;
178 tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
179 /* Set the text of the item. */
180 tvi.pszText = pHostName;
181 tvi.cchTextMax = lstrlen(tvi.pszText);
182 /* Assume the item is not a parent item, so give it an image. */
183 tvi.iImage = Image_Root;
184 tvi.iSelectedImage = Image_Root;
185 tvi.cChildren = 5;
186 /* Save the heading level in the item's application-defined data area. */
187 tvi.lParam = (LPARAM)NULL;
188 tvins.u.item = tvi;
189 tvins.hInsertAfter = (HTREEITEM)TVI_FIRST;
190 tvins.hParent = TVI_ROOT;
191 /* Add the item to the tree view control. */
192 if (!(hRoot = TreeView_InsertItem(hwndTV, &tvins))) return FALSE;
194 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_CLASSES_ROOT"), HKEY_CLASSES_ROOT, 1)) return FALSE;
195 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_CURRENT_USER"), HKEY_CURRENT_USER, 1)) return FALSE;
196 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_LOCAL_MACHINE"), HKEY_LOCAL_MACHINE, 1)) return FALSE;
197 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_USERS"), HKEY_USERS, 1)) return FALSE;
198 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_CURRENT_CONFIG"), HKEY_CURRENT_CONFIG, 1)) return FALSE;
199 if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_DYN_DATA"), HKEY_DYN_DATA, 1)) return FALSE;
201 /* expand and select host name */
202 TreeView_Expand(hwndTV, hRoot, TVE_EXPAND);
203 TreeView_Select(hwndTV, hRoot, TVGN_CARET);
204 return TRUE;
209 * InitTreeViewImageLists - creates an image list, adds three bitmaps
210 * to it, and associates the image list with a tree view control.
211 * Returns TRUE if successful, or FALSE otherwise.
212 * hwndTV - handle to the tree view control.
214 static BOOL InitTreeViewImageLists(HWND hwndTV)
216 HIMAGELIST himl; /* handle to image list */
217 HICON hico; /* handle to icon */
219 /* Create the image list. */
220 if ((himl = ImageList_Create(CX_ICON, CY_ICON,
221 ILC_MASK, 0, NUM_ICONS)) == NULL)
222 return FALSE;
224 /* Add the open file, closed file, and document bitmaps. */
225 hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_OPEN_FILE));
226 Image_Open = ImageList_AddIcon(himl, hico);
228 hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_CLOSED_FILE));
229 Image_Closed = ImageList_AddIcon(himl, hico);
231 hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_ROOT));
232 Image_Root = ImageList_AddIcon(himl, hico);
234 /* Fail if not all of the images were added. */
235 if (ImageList_GetImageCount(himl) < NUM_ICONS)
237 return FALSE;
240 /* Associate the image list with the tree view control. */
241 TreeView_SetImageList(hwndTV, himl, TVSIL_NORMAL);
243 return TRUE;
246 BOOL OnTreeExpanding(HWND hwndTV, NMTREEVIEW* pnmtv)
248 DWORD dwCount, dwIndex, dwMaxSubKeyLen;
249 HKEY hRoot, hNewKey, hKey;
250 LPCTSTR keyPath;
251 LPTSTR Name;
252 LONG errCode;
253 HCURSOR hcursorOld;
255 static int expanding;
256 if (expanding) return FALSE;
257 if (pnmtv->itemNew.state & TVIS_EXPANDEDONCE ) {
258 return TRUE;
260 expanding = TRUE;
261 hcursorOld = SetCursor(LoadCursor(NULL, IDC_WAIT));
262 SendMessage(hwndTV, WM_SETREDRAW, FALSE, 0);
264 keyPath = GetItemPath(hwndTV, pnmtv->itemNew.hItem, &hRoot);
265 if (!keyPath) goto done;
267 if (*keyPath) {
268 errCode = RegOpenKeyEx(hRoot, keyPath, 0, KEY_READ, &hNewKey);
269 if (errCode != ERROR_SUCCESS) goto done;
270 } else {
271 hNewKey = hRoot;
274 errCode = RegQueryInfoKey(hNewKey, 0, 0, 0, &dwCount, &dwMaxSubKeyLen, 0, 0, 0, 0, 0, 0);
275 if (errCode != ERROR_SUCCESS) goto done;
276 dwMaxSubKeyLen++; /* account for the \0 terminator */
277 Name = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(TCHAR));
278 if (!Name) goto done;
280 for (dwIndex = 0; dwIndex < dwCount; dwIndex++) {
281 DWORD cName = dwMaxSubKeyLen, dwSubCount;
282 FILETIME LastWriteTime;
284 errCode = RegEnumKeyEx(hNewKey, dwIndex, Name, &cName, 0, 0, 0, &LastWriteTime);
285 if (errCode != ERROR_SUCCESS) continue;
286 errCode = RegOpenKeyEx(hNewKey, Name, 0, KEY_QUERY_VALUE, &hKey);
287 if (errCode == ERROR_SUCCESS) {
288 errCode = RegQueryInfoKey(hKey, 0, 0, 0, &dwSubCount, 0, 0, 0, 0, 0, 0, 0);
289 RegCloseKey(hKey);
291 if (errCode != ERROR_SUCCESS) dwSubCount = 0;
292 AddEntryToTree(hwndTV, pnmtv->itemNew.hItem, Name, NULL, dwSubCount);
294 RegCloseKey(hNewKey);
295 HeapFree(GetProcessHeap(), 0, Name);
297 done:
298 SendMessage(hwndTV, WM_SETREDRAW, TRUE, 0);
299 SetCursor(hcursorOld);
300 expanding = FALSE;
302 return TRUE;
307 * CreateTreeView - creates a tree view control.
308 * Returns the handle to the new control if successful, or NULL otherwise.
309 * hwndParent - handle to the control's parent window.
311 HWND CreateTreeView(HWND hwndParent, LPTSTR pHostName, int id)
313 RECT rcClient;
314 HWND hwndTV;
316 /* Get the dimensions of the parent window's client area, and create the tree view control. */
317 GetClientRect(hwndParent, &rcClient);
318 hwndTV = CreateWindowEx(WS_EX_CLIENTEDGE, WC_TREEVIEW, _T("Tree View"),
319 WS_VISIBLE | WS_CHILD | WS_TABSTOP | TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT,
320 0, 0, rcClient.right, rcClient.bottom,
321 hwndParent, (HMENU)id, hInst, NULL);
322 /* Initialize the image list, and add items to the control. */
323 if (!InitTreeViewImageLists(hwndTV) || !InitTreeViewItems(hwndTV, pHostName)) {
324 DestroyWindow(hwndTV);
325 return NULL;
327 return hwndTV;