Close the files after loading.
[reactos.git] / rosapps / regedit / regtree.cpp
1 /*
2 * ReactOS regedit
3 *
4 * regtree.cpp
5 *
6 * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #ifdef _MSC_VER
24 #include "stdafx.h"
25 #else
26 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
27 #include <windows.h>
28 #include <commctrl.h>
29 #include <stdlib.h>
30 #include <malloc.h>
31 #include <memory.h>
32 #include <tchar.h>
33 #include <process.h>
34 #include <stdio.h>
35 #endif
36
37 #include "regedit.h"
38 #include "regtree.h"
39
40 // Global Variables:
41 extern HINSTANCE hInst; // current instance
42 extern HWND hMainWnd; // Main Window
43
44 // Global variables and constants
45 // Image_Open, Image_Closed, and Image_Root - integer variables for
46 // indexes of the images.
47 // CX_BITMAP and CY_BITMAP - width and height of an icon.
48 // NUM_BITMAPS - number of bitmaps to add to the image list.
49 int Image_Open;
50 int Image_Closed;
51 int Image_Root;
52
53 #define CX_BITMAP 16
54 #define CY_BITMAP 16
55 #define NUM_BITMAPS 3
56
57
58 // AddItemToTree - adds items to a tree view control.
59 // Returns the handle to the newly added item.
60 // hwndTV - handle to the tree view control.
61 // lpszItem - text of the item to add.
62 // nLevel - level at which to add the item.
63
64 HTREEITEM AddItemToTree(HWND hwndTV, LPSTR lpszItem, int nLevel)
65 {
66 TVITEM tvi;
67 TVINSERTSTRUCT tvins;
68 static HTREEITEM hPrev = (HTREEITEM)TVI_FIRST;
69 static HTREEITEM hPrevRootItem = NULL;
70 static HTREEITEM hPrevLev2Item = NULL;
71 HTREEITEM hti;
72
73 tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
74
75 // Set the text of the item.
76 tvi.pszText = lpszItem;
77 tvi.cchTextMax = lstrlen(lpszItem);
78
79 // Assume the item is not a parent item, so give it an image.
80 tvi.iImage = Image_Root;
81 tvi.iSelectedImage = Image_Root;
82
83 tvi.cChildren = 1;
84
85
86 // Save the heading level in the item's application-defined data area.
87 tvi.lParam = (LPARAM)nLevel;
88
89 tvins.item = tvi;
90 tvins.hInsertAfter = hPrev;
91
92 // Set the parent item based on the specified level.
93 if (nLevel == 1)
94 tvins.hParent = TVI_ROOT;
95 else if (nLevel == 2)
96 tvins.hParent = hPrevRootItem;
97 else
98 tvins.hParent = hPrevLev2Item;
99
100 // Add the item to the tree view control.
101 hPrev = (HTREEITEM)SendMessage(hwndTV, TVM_INSERTITEM, 0, (LPARAM)(LPTVINSERTSTRUCT)&tvins);
102
103 // Save the handle to the item.
104 if (nLevel == 1)
105 hPrevRootItem = hPrev;
106 else if (nLevel == 2)
107 hPrevLev2Item = hPrev;
108
109 // The new item is a child item. Give the parent item a
110 // closed folder bitmap to indicate it now has child items.
111 if (nLevel > 1) {
112 hti = TreeView_GetParent(hwndTV, hPrev);
113 tvi.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE;
114 tvi.hItem = hti;
115 tvi.iImage = Image_Closed;
116 tvi.iSelectedImage = Image_Closed;
117 TreeView_SetItem(hwndTV, &tvi);
118 }
119
120 return hPrev;
121 }
122
123 // InitTreeViewItems - extracts headings from the specified file and
124 // passes them to a function that adds them to a tree view control.
125 // Returns TRUE if successful, or FALSE otherwise.
126 // hwndTV - handle to the tree view control.
127 // lpszFileName - name of file with headings.
128
129 BOOL InitTreeViewItems(HWND hwndTV, LPSTR lpszFileName)
130 {
131 HTREEITEM hItem;
132
133 hItem = AddItemToTree(hwndTV, "My Computer", 1);
134 AddItemToTree(hwndTV, "HKEY_CLASSES_ROOT", 2);
135 AddItemToTree(hwndTV, "HKEY_CURRENT_USER", 2);
136 AddItemToTree(hwndTV, "HKEY_LOCAL_MACHINE", 2);
137 AddItemToTree(hwndTV, "HKEY_USERS", 2);
138 AddItemToTree(hwndTV, "HKEY_CURRENT_CONFIG", 2);
139
140 TreeView_Expand(hwndTV, hItem, TVE_EXPAND);
141 return TRUE;
142 }
143
144 // InitTreeViewImageLists - creates an image list, adds three bitmaps
145 // to it, and associates the image list with a tree view control.
146 // Returns TRUE if successful, or FALSE otherwise.
147 // hwndTV - handle to the tree view control.
148
149 BOOL InitTreeViewImageLists(HWND hwndTV)
150 {
151 HIMAGELIST himl; // handle to image list
152 HBITMAP hbmp; // handle to bitmap
153
154 // Create the image list.
155 if ((himl = ImageList_Create(CX_BITMAP, CY_BITMAP,
156 FALSE, NUM_BITMAPS, 0)) == NULL)
157 return FALSE;
158
159 // Add the open file, closed file, and document bitmaps.
160 hbmp = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_OPEN_FILE));
161 Image_Open = ImageList_Add(himl, hbmp, (HBITMAP) NULL);
162 DeleteObject(hbmp);
163
164 hbmp = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_CLOSED_FILE));
165 Image_Closed = ImageList_Add(himl, hbmp, (HBITMAP) NULL);
166 DeleteObject(hbmp);
167
168 hbmp = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_ROOT));
169 Image_Root = ImageList_Add(himl, hbmp, (HBITMAP) NULL);
170 DeleteObject(hbmp);
171
172 // Fail if not all of the images were added.
173 if (ImageList_GetImageCount(himl) < 3)
174 return FALSE;
175
176 // Associate the image list with the tree view control.
177 TreeView_SetImageList(hwndTV, himl, TVSIL_NORMAL);
178
179 return TRUE;
180 }
181
182 // CreateTreeView - creates a tree view control.
183 // Returns the handle to the new control if successful,
184 // or NULL otherwise.
185 // hwndParent - handle to the control's parent window.
186 // lpszFileName - name of the file to parse for tree view items.
187
188 HWND CreateTreeView(HWND hwndParent, LPSTR lpszFileName)
189 {
190 RECT rcClient; // dimensions of client area
191 HWND hwndTV; // handle to tree view control
192
193 // Get the dimensions of the parent window's client area, and create
194 // the tree view control.
195 GetClientRect(hwndParent, &rcClient);
196 hwndTV = CreateWindowEx(0, WC_TREEVIEW, "Tree View",
197 WS_VISIBLE | WS_CHILD | WS_BORDER | TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT,
198 0, 0, rcClient.right, rcClient.bottom,
199 hwndParent, (HMENU)TREE_WINDOW, hInst, NULL);
200
201 // Initialize the image list, and add items to the control.
202 if (!InitTreeViewImageLists(hwndTV) || !InitTreeViewItems(hwndTV, lpszFileName)) {
203 DestroyWindow(hwndTV);
204 return FALSE;
205 }
206 return hwndTV;
207 }
208