Add partial implementation of the environment variables dialog.
[reactos.git] / reactos / lib / cpl / sysdm / environment.c
1 /*
2 * ReactOS
3 * Copyright (C) 2004 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /* $Id: environment.c,v 1.1 2004/07/02 20:28:00 ekohl Exp $
20 *
21 * PROJECT: ReactOS System Control Panel
22 * FILE: lib/cpl/sysdm/environment.c
23 * PURPOSE: Environment variable settings
24 * PROGRAMMER: Eric Kohl
25 */
26
27 #include <windows.h>
28 #include <commctrl.h>
29 #include <stdlib.h>
30 #include <tchar.h>
31
32 #include "resource.h"
33 #include "sysdm.h"
34
35
36 static VOID
37 SetEnvironmentVariables(HWND hwndListView,
38 HKEY hRootKey,
39 LPTSTR lpSubKeyName)
40 {
41 HKEY hKey;
42 DWORD dwValues;
43 DWORD dwMaxValueNameLength;
44 DWORD dwMaxValueDataLength;
45 DWORD i;
46 LPTSTR lpName;
47 LPTSTR lpData;
48 LPTSTR lpExpandData;
49 DWORD dwNameLength;
50 DWORD dwDataLength;
51 DWORD dwType;
52
53 LV_ITEM lvi;
54 int nIndex;
55
56 if (RegOpenKeyEx(hRootKey,
57 lpSubKeyName,
58 0,
59 KEY_READ,
60 &hKey))
61 return;
62
63 if (RegQueryInfoKey(hKey,
64 NULL,
65 NULL,
66 NULL,
67 NULL,
68 NULL,
69 NULL,
70 &dwValues,
71 &dwMaxValueNameLength,
72 &dwMaxValueDataLength,
73 NULL,
74 NULL))
75 {
76 RegCloseKey(hKey);
77 return;
78 }
79
80 lpName = GlobalAlloc(GPTR, (dwMaxValueNameLength + 1) * sizeof(TCHAR));
81 if (lpName == NULL)
82 {
83 RegCloseKey(hKey);
84 return;
85 }
86
87 lpData = GlobalAlloc(GPTR, (dwMaxValueDataLength + 1) * sizeof(TCHAR));
88 if (lpData == NULL)
89 {
90 GlobalFree(lpData);
91 RegCloseKey(hKey);
92 return;
93 }
94
95 for (i = 0; i < dwValues; i++)
96 {
97 dwNameLength = dwMaxValueNameLength + 1;
98 dwDataLength = dwMaxValueDataLength + 1;
99 if (RegEnumValue(hKey,
100 i,
101 lpName,
102 &dwNameLength,
103 NULL,
104 &dwType,
105 (LPBYTE)lpData,
106 &dwDataLength))
107 {
108 GlobalFree(lpName);
109 GlobalFree(lpData);
110 RegCloseKey(hKey);
111 return;
112 }
113
114 if (dwType == REG_EXPAND_SZ)
115 {
116 lpExpandData = GlobalAlloc(GPTR, MAX_PATH * sizeof(TCHAR));
117 if (lpExpandData == NULL)
118 {
119 GlobalFree(lpName);
120 GlobalFree(lpData);
121 RegCloseKey(hKey);
122 return;
123 }
124
125 ExpandEnvironmentStrings(lpData,
126 lpExpandData,
127 MAX_PATH);
128 }
129
130 memset(&lvi, 0x00, sizeof(lvi));
131 lvi.mask = LVIF_TEXT | LVIF_STATE;
132 lvi.pszText = lpName;
133 lvi.state=0;
134 nIndex = ListView_InsertItem(hwndListView, &lvi);
135
136 if (dwType == REG_EXPAND_SZ)
137 {
138 ListView_SetItemText(hwndListView, nIndex, 1, lpExpandData);
139 GlobalFree(lpExpandData);
140 }
141 else
142 {
143 ListView_SetItemText(hwndListView, nIndex, 1, lpData);
144 }
145 }
146
147 GlobalFree(lpName);
148 GlobalFree(lpData);
149 RegCloseKey(hKey);
150 }
151
152
153 static VOID
154 SetListViewColumns(HWND hwndListView)
155 {
156 RECT rect;
157 LV_COLUMN column;
158
159 GetClientRect(hwndListView, &rect);
160
161 memset(&column, 0x00, sizeof(column));
162 column.mask=LVCF_FMT | LVCF_WIDTH | LVCF_SUBITEM|LVCF_TEXT;
163 column.fmt=LVCFMT_LEFT;
164 column.cx = (rect.right - rect.left) / 3;
165 column.iSubItem = 0;
166 column.pszText = _T("Variable");
167 ListView_InsertColumn(hwndListView, 0, &column);
168
169 column.cx = (rect.right - rect.left) - ((rect.right - rect.left) / 3) - 1;
170 column.iSubItem = 1;
171 column.pszText = _T("Value");
172 ListView_InsertColumn(hwndListView, 1, &column);
173 }
174
175
176 static VOID
177 OnInitDialog(HWND hwndDlg)
178 {
179 HWND hwndListView;
180
181 /* Set user environment variables */
182 hwndListView = GetDlgItem(hwndDlg, IDC_USER_VARIABLE_LIST);
183
184 SetListViewColumns(hwndListView);
185
186 SetEnvironmentVariables(hwndListView,
187 HKEY_CURRENT_USER,
188 _T("Environment"));
189
190 ListView_SetColumnWidth(hwndListView,2,LVSCW_AUTOSIZE_USEHEADER);
191 ListView_Update(hwndListView,0);
192
193
194 /* Set system environment variables */
195 hwndListView = GetDlgItem(hwndDlg, IDC_SYSTEM_VARIABLE_LIST);
196
197 SetListViewColumns(hwndListView);
198
199 SetEnvironmentVariables(hwndListView,
200 HKEY_LOCAL_MACHINE,
201 _T("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment"));
202
203 ListView_SetColumnWidth(hwndListView,2,LVSCW_AUTOSIZE_USEHEADER);
204 ListView_Update(hwndListView,0);
205 }
206
207
208 /* Environment dialog procedure */
209 BOOL CALLBACK
210 EnvironmentDlgProc(HWND hwndDlg,
211 UINT uMsg,
212 WPARAM wParam,
213 LPARAM lParam)
214 {
215 switch (uMsg)
216 {
217 case WM_INITDIALOG:
218 OnInitDialog(hwndDlg);
219 break;
220
221 case WM_COMMAND:
222 switch (LOWORD(wParam))
223 {
224 case IDC_USER_VARIABLE_NEW:
225 case IDC_USER_VARIABLE_EDIT:
226 case IDC_USER_VARIABLE_DELETE:
227 break;
228
229 case IDC_SYSTEM_VARIABLE_NEW:
230 case IDC_SYSTEM_VARIABLE_EDIT:
231 case IDC_SYSTEM_VARIABLE_DELETE:
232 break;
233
234 case IDOK:
235 EndDialog(hwndDlg, 0);
236 return TRUE;
237
238 case IDCANCEL:
239 EndDialog(hwndDlg, 0);
240 return TRUE;
241 }
242 break;
243 }
244
245 return FALSE;
246 }
247
248 /* EOF */