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