There's no way to properly unload a driver, so don't try.
[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 <stdio.h>
30 #include <wine/debug.h>
31
32 #include "main.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(regedit);
35
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;
43
44 static LPTSTR pathBuffer;
45
46 #define CX_ICON    16
47 #define CY_ICON    16
48 #define NUM_ICONS    3
49
50 static BOOL get_item_path(HWND hwndTV, HTREEITEM hItem, HKEY* phKey, LPTSTR* pKeyPath, int* pPathLen, int* pMaxLen)
51 {
52     TVITEM item;
53     int maxLen, len;
54     LPTSTR newStr;
55
56     item.mask = TVIF_PARAM;
57     item.hItem = hItem;
58     if (!TreeView_GetItem(hwndTV, &item)) return FALSE;
59
60     if (item.lParam) {
61         /* found root key with valid key value */
62         *phKey = (HKEY)item.lParam;
63         return TRUE;
64     }
65
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);
70     }
71
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;
82         }
83         newStr = HeapReAlloc(GetProcessHeap(), 0, *pKeyPath, *pMaxLen * 2);
84         if (!newStr) return FALSE;
85         *pKeyPath = newStr;
86         *pMaxLen *= 2;
87     } while(TRUE);
88
89     return TRUE;
90 }
91
92 LPCTSTR GetItemPath(HWND hwndTV, HTREEITEM hItem, HKEY* phRootKey)
93 {
94     int pathLen = 0, maxLen;
95
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;
105 }
106
107 BOOL DeleteNode(HWND hwndTV, HTREEITEM hItem)
108 {
109     if (!hItem) hItem = TreeView_GetSelection(hwndTV);
110     if (!hItem) return FALSE;
111     return TreeView_DeleteItem(hwndTV, hItem);
112 }
113
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)
116 {
117     TVITEM tvi;
118     TVINSERTSTRUCT tvins;
119
120     if (hKey) {
121         if (RegQueryInfoKey(hKey, 0, 0, 0, &dwChildren, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
122             dwChildren = 0;
123         }
124     }
125
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);
137 }
138
139 static BOOL RefreshTreeItem(HWND hwndTV, HTREEITEM hItem)
140 {
141     HKEY hRoot, hKey, hSubKey;
142     HTREEITEM childItem;
143     LPCTSTR KeyPath;
144     DWORD dwCount, dwIndex, dwMaxSubKeyLen;
145     LPSTR Name;
146     TVITEM tvItem;
147     
148     KeyPath = GetItemPath(hwndTV, hItem, &hRoot);
149
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;
154         }
155     } else {
156         hKey = hRoot;
157     }
158
159     if (RegQueryInfoKey(hKey, 0, 0, 0, &dwCount, &dwMaxSubKeyLen, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
160         return FALSE;
161     }
162
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;
169     }
170
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;
175     }
176
177     dwMaxSubKeyLen++; /* account for the \0 terminator */
178     if (!(Name = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(TCHAR)))) {
179         return FALSE;
180     }
181     tvItem.cchTextMax = dwMaxSubKeyLen;
182     if (!(tvItem.pszText = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(TCHAR)))) {
183         return FALSE;
184     }
185
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;
190
191         found = FALSE;
192         if (RegEnumKeyEx(hKey, dwIndex, Name, &cName, 0, 0, 0, NULL) != ERROR_SUCCESS) {
193             continue;
194         }
195
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;
201             }
202             RegCloseKey(hSubKey);
203         }
204
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;
212             }
213
214             if (!strcmp(tvItem.pszText, Name)) {
215                 found = TRUE;
216                 break;
217             }
218         }
219
220         if (found == FALSE) {
221             WINE_TRACE("New subkey %s\n", Name);
222             AddEntryToTree(hwndTV, hItem, Name, NULL, dwSubCount);
223         }
224     }
225     HeapFree(GetProcessHeap(), 0, Name);
226     HeapFree(GetProcessHeap(), 0, tvItem.pszText);
227     RegCloseKey(hKey);
228
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);
235         }
236         childItem = nextItem;
237     }
238
239     return TRUE;
240 }
241
242 BOOL RefreshTreeView(HWND hwndTV)
243 {
244     HTREEITEM hItem;
245     HTREEITEM hSelectedItem;
246     HCURSOR hcursorOld;
247
248     WINE_TRACE("\n");
249     hSelectedItem = TreeView_GetSelection(hwndTV);
250     hcursorOld = SetCursor(LoadCursor(NULL, IDC_WAIT));
251     SendMessage(hwndTV, WM_SETREDRAW, FALSE, 0);
252
253     hItem = TreeView_GetChild(hwndTV, TreeView_GetRoot(hwndTV));
254     while (hItem) {
255         RefreshTreeItem(hwndTV, hItem);
256         hItem = TreeView_GetNextSibling(hwndTV, hItem);
257     }
258
259     SendMessage(hwndTV, WM_SETREDRAW, TRUE, 0);
260     SetCursor(hcursorOld);
261     
262     /* We reselect the currently selected node, this will prompt a refresh of the listview. */
263     TreeView_SelectItem(hwndTV, hSelectedItem);
264     return TRUE;
265 }
266
267 HTREEITEM InsertNode(HWND hwndTV, HTREEITEM hItem, LPTSTR name)
268 {
269     TCHAR buf[MAX_NEW_KEY_LEN];
270     HTREEITEM hNewItem = 0;
271     TVITEMEX item;
272
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;
283     }
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;
293         }       
294     }
295     if (hNewItem) TreeView_SelectItem(hwndTV, hNewItem);
296
297     return hNewItem;
298 }
299
300 HWND StartKeyRename(HWND hwndTV)
301 {
302     HTREEITEM hItem;
303
304     if(!(hItem = TreeView_GetSelection(hwndTV))) return 0;
305     return TreeView_EditLabel(hwndTV, hItem);
306 }
307
308 static BOOL InitTreeViewItems(HWND hwndTV, LPTSTR pHostName)
309 {
310     TVITEM tvi;
311     TVINSERTSTRUCT tvins;
312     HTREEITEM hRoot;
313
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;
329
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;
336     
337     /* expand and select host name */
338     TreeView_Expand(hwndTV, hRoot, TVE_EXPAND);
339     TreeView_Select(hwndTV, hRoot, TVGN_CARET);
340     return TRUE;
341 }
342
343
344 /*
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.
349  */
350 static BOOL InitTreeViewImageLists(HWND hwndTV)
351 {
352     HIMAGELIST himl;  /* handle to image list  */
353     HICON hico;       /* handle to icon  */
354
355     /* Create the image list.  */
356     if ((himl = ImageList_Create(CX_ICON, CY_ICON,
357                                  ILC_MASK, 0, NUM_ICONS)) == NULL)
358         return FALSE;
359
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);
363
364     hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_CLOSED_FILE));
365     Image_Closed = ImageList_AddIcon(himl, hico);
366
367     hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_ROOT));
368     Image_Root = ImageList_AddIcon(himl, hico);
369
370     /* Fail if not all of the images were added.  */
371     if (ImageList_GetImageCount(himl) < NUM_ICONS)
372     {
373       return FALSE;
374     }
375
376     /* Associate the image list with the tree view control.  */
377     TreeView_SetImageList(hwndTV, himl, TVSIL_NORMAL);
378
379     return TRUE;
380 }
381
382 BOOL OnTreeExpanding(HWND hwndTV, NMTREEVIEW* pnmtv)
383 {
384     DWORD dwCount, dwIndex, dwMaxSubKeyLen;
385     HKEY hRoot, hNewKey, hKey;
386     LPCTSTR keyPath;
387     LPTSTR Name;
388     LONG errCode;
389     HCURSOR hcursorOld;
390
391     static int expanding;
392     if (expanding) return FALSE;
393     if (pnmtv->itemNew.state & TVIS_EXPANDEDONCE ) {
394         return TRUE;
395     }
396     expanding = TRUE;
397     hcursorOld = SetCursor(LoadCursor(NULL, IDC_WAIT));
398     SendMessage(hwndTV, WM_SETREDRAW, FALSE, 0);
399
400     keyPath = GetItemPath(hwndTV, pnmtv->itemNew.hItem, &hRoot);
401     if (!keyPath) goto done;
402
403     if (*keyPath) {
404         errCode = RegOpenKeyEx(hRoot, keyPath, 0, KEY_READ, &hNewKey);
405         if (errCode != ERROR_SUCCESS) goto done;
406     } else {
407         hNewKey = hRoot;
408     }
409
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;
415
416     for (dwIndex = 0; dwIndex < dwCount; dwIndex++) {
417         DWORD cName = dwMaxSubKeyLen, dwSubCount;
418
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);
425         }
426         if (errCode != ERROR_SUCCESS) dwSubCount = 0;
427         AddEntryToTree(hwndTV, pnmtv->itemNew.hItem, Name, NULL, dwSubCount);
428     }
429     RegCloseKey(hNewKey);
430     HeapFree(GetProcessHeap(), 0, Name);
431
432 done:
433     SendMessage(hwndTV, WM_SETREDRAW, TRUE, 0);
434     SetCursor(hcursorOld);
435     expanding = FALSE;
436
437     return TRUE;
438 }
439
440
441 /*
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.
445  */
446 HWND CreateTreeView(HWND hwndParent, LPTSTR pHostName, int id)
447 {
448     RECT rcClient;
449     HWND hwndTV;
450
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;
461     }
462     return hwndTV;
463 }