Remove references to regapi.
[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
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_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;
41
42 static LPTSTR pathBuffer;
43
44 #define CX_BITMAP    16
45 #define CY_BITMAP    16
46 #define NUM_BITMAPS  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     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;
103 }
104
105 static HTREEITEM AddEntryToTree(HWND hwndTV, HTREEITEM hParent, LPTSTR label, HKEY hKey, DWORD dwChildren)
106 {
107     TVITEM tvi;
108     TVINSERTSTRUCT tvins;
109
110     tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
111     tvi.pszText = label;
112     tvi.cchTextMax = lstrlen(tvi.pszText);
113     tvi.iImage = Image_Closed;
114     tvi.iSelectedImage = Image_Open;
115     tvi.cChildren = dwChildren;
116     tvi.lParam = (LPARAM)hKey;
117     tvins.u.item = tvi;
118     tvins.hInsertAfter = (HTREEITEM)(hKey ? TVI_LAST : TVI_SORT);
119     tvins.hParent = hParent;
120     return TreeView_InsertItem(hwndTV, &tvins);
121 }
122
123
124 static BOOL InitTreeViewItems(HWND hwndTV, LPTSTR pHostName)
125 {
126     TVITEM tvi;
127     TVINSERTSTRUCT tvins;
128     HTREEITEM hRoot;
129
130     tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
131     /* Set the text of the item.  */
132     tvi.pszText = pHostName;
133     tvi.cchTextMax = lstrlen(tvi.pszText);
134     /* Assume the item is not a parent item, so give it an image.  */
135     tvi.iImage = Image_Root;
136     tvi.iSelectedImage = Image_Root;
137     tvi.cChildren = 5;
138     /* Save the heading level in the item's application-defined data area.  */
139     tvi.lParam = (LPARAM)NULL;
140     tvins.u.item = tvi;
141     tvins.hInsertAfter = (HTREEITEM)TVI_FIRST;
142     tvins.hParent = TVI_ROOT;
143     /* Add the item to the tree view control.  */
144     if (!(hRoot = TreeView_InsertItem(hwndTV, &tvins))) return FALSE;
145
146     if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_CLASSES_ROOT"), HKEY_CLASSES_ROOT, 1)) return FALSE;
147     if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_CURRENT_USER"), HKEY_CURRENT_USER, 1)) return FALSE;
148     if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_LOCAL_MACHINE"), HKEY_LOCAL_MACHINE, 1)) return FALSE;
149     if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_USERS"), HKEY_USERS, 1)) return FALSE;
150     if (!AddEntryToTree(hwndTV, hRoot, _T("HKEY_CURRENT_CONFIG"), HKEY_CURRENT_CONFIG, 1)) return FALSE;
151
152     return TRUE;
153 }
154
155
156 /*
157  * InitTreeViewImageLists - creates an image list, adds three bitmaps
158  * to it, and associates the image list with a tree view control.
159  * Returns TRUE if successful, or FALSE otherwise.
160  * hwndTV - handle to the tree view control.
161  */
162 static BOOL InitTreeViewImageLists(HWND hwndTV)
163 {
164     HIMAGELIST himl;  /* handle to image list  */
165     HBITMAP hbmp;     /* handle to bitmap  */
166
167     /* Create the image list.  */
168     if ((himl = ImageList_Create(CX_BITMAP, CY_BITMAP,
169                                  FALSE, NUM_BITMAPS, 0)) == NULL)
170         return FALSE;
171
172     /* Add the open file, closed file, and document bitmaps.  */
173     hbmp = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_OPEN_FILE));
174     Image_Open = ImageList_Add(himl, hbmp, NULL);
175     DeleteObject(hbmp);
176
177     hbmp = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_CLOSED_FILE));
178     Image_Closed = ImageList_Add(himl, hbmp, NULL);
179     DeleteObject(hbmp);
180
181     hbmp = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_ROOT));
182     Image_Root = ImageList_Add(himl, hbmp, NULL);
183     DeleteObject(hbmp);
184
185     /* Fail if not all of the images were added.  */
186     if (ImageList_GetImageCount(himl) < 3) return FALSE;
187
188     /* Associate the image list with the tree view control.  */
189     TreeView_SetImageList(hwndTV, himl, TVSIL_NORMAL);
190
191     return TRUE;
192 }
193
194 BOOL OnTreeExpanding(HWND hwndTV, NMTREEVIEW* pnmtv)
195 {
196     DWORD dwCount, dwIndex, dwMaxSubKeyLen;
197     HKEY hRoot, hNewKey, hKey;
198     LPCTSTR keyPath;
199     LPTSTR Name;
200     LONG errCode;
201
202     static int expanding;
203     if (expanding) return FALSE;
204     if (pnmtv->itemNew.state & TVIS_EXPANDEDONCE ) {
205         return TRUE;
206     }
207     expanding = TRUE;
208
209     keyPath = GetItemPath(hwndTV, pnmtv->itemNew.hItem, &hRoot);
210     if (!keyPath) goto done;
211
212     if (*keyPath) {
213         errCode = RegOpenKeyEx(hRoot, keyPath, 0, KEY_READ, &hNewKey);
214         if (errCode != ERROR_SUCCESS) goto done;
215     } else {
216         hNewKey = hRoot;
217     }
218
219     errCode = RegQueryInfoKey(hNewKey, 0, 0, 0, &dwCount, &dwMaxSubKeyLen, 0, 0, 0, 0, 0, 0);
220     if (errCode != ERROR_SUCCESS) goto done;
221     dwMaxSubKeyLen++; /* account for the \0 terminator */
222     Name = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(TCHAR));
223     if (!Name) goto done;
224
225     for (dwIndex = 0; dwIndex < dwCount; dwIndex++) {
226         DWORD cName = dwMaxSubKeyLen, dwSubCount;
227         FILETIME LastWriteTime;
228
229         errCode = RegEnumKeyEx(hNewKey, dwIndex, Name, &cName, 0, 0, 0, &LastWriteTime);
230         if (errCode != ERROR_SUCCESS) continue;
231         errCode = RegOpenKeyEx(hNewKey, Name, 0, KEY_QUERY_VALUE, &hKey);
232         if (errCode == ERROR_SUCCESS) {
233             errCode = RegQueryInfoKey(hKey, 0, 0, 0, &dwSubCount, 0, 0, 0, 0, 0, 0, 0);
234             RegCloseKey(hKey);
235         }
236         if (errCode != ERROR_SUCCESS) dwSubCount = 0;
237         AddEntryToTree(hwndTV, pnmtv->itemNew.hItem, Name, NULL, dwSubCount);
238     }
239     RegCloseKey(hNewKey);
240     HeapFree(GetProcessHeap(), 0, Name);
241
242 done:
243     expanding = FALSE;
244
245     return TRUE;
246 }
247
248
249 /*
250  * CreateTreeView - creates a tree view control.
251  * Returns the handle to the new control if successful, or NULL otherwise.
252  * hwndParent - handle to the control's parent window.
253  */
254 HWND CreateTreeView(HWND hwndParent, LPTSTR pHostName, int id)
255 {
256     RECT rcClient;
257     HWND hwndTV;
258
259     /* Get the dimensions of the parent window's client area, and create the tree view control.  */
260     GetClientRect(hwndParent, &rcClient);
261     hwndTV = CreateWindowEx(WS_EX_CLIENTEDGE, WC_TREEVIEW, _T("Tree View"),
262                             WS_VISIBLE | WS_CHILD | TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT,
263                             0, 0, rcClient.right, rcClient.bottom,
264                             hwndParent, (HMENU)id, hInst, NULL);
265     /* Initialize the image list, and add items to the control.  */
266     if (!InitTreeViewImageLists(hwndTV) || !InitTreeViewItems(hwndTV, pHostName)) {
267         DestroyWindow(hwndTV);
268         return NULL;
269     }
270     return hwndTV;
271 }