Implement SetCPGlobal (an undocumented Win32 API).
[wine/multimedia.git] / programs / regedit / edit.c
blob01305e8b03c48582127a5f4090863d50f737052f
1 /*
2 * Registry editing UI functions.
4 * Copyright (C) 2003 Dimitrie O. Paun
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.
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.
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
21 #define WIN32_LEAN_AND_MEAN /* Exclude rarely-used stuff from Windows headers */
23 #include <windows.h>
24 #include <tchar.h>
25 #include <commctrl.h>
26 #include <commdlg.h>
27 #include <cderr.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <shellapi.h>
31 #include <shlwapi.h>
33 #include "main.h"
34 #include "regproc.h"
35 #include "resource.h"
37 static const TCHAR* editValueName;
38 static TCHAR* stringValueData;
39 static BOOL isDecimal;
41 INT vmessagebox(HWND hwnd, INT buttons, INT titleId, INT resId, va_list ap)
43 TCHAR title[256];
44 TCHAR errfmt[1024];
45 TCHAR errstr[1024];
47 if (!LoadString(hInst, titleId, title, COUNT_OF(title)))
48 lstrcpy(title, "Error");
50 if (!LoadString(hInst, resId, errfmt, COUNT_OF(errfmt)))
51 lstrcpy(errfmt, "Unknown error string!");
53 _vsntprintf(errstr, COUNT_OF(errstr), errfmt, ap);
55 return MessageBox(hwnd, errstr, title, buttons);
58 INT messagebox(HWND hwnd, INT buttons, INT titleId, INT resId, ...)
60 va_list ap;
61 INT result;
63 va_start(ap, resId);
64 result = vmessagebox(hwnd, buttons, titleId, resId, ap);
65 va_end(ap);
67 return result;
70 void error(HWND hwnd, INT resId, ...)
72 va_list ap;
74 va_start(ap, resId);
75 vmessagebox(hwnd, MB_OK | MB_ICONERROR, IDS_ERROR, resId, ap);
76 va_end(ap);
79 static void error_code_messagebox(HWND hwnd, DWORD error_code)
81 LPTSTR lpMsgBuf;
82 DWORD status;
83 TCHAR title[256];
84 static const TCHAR fallback[] = TEXT("Error displaying error message.\n");
85 if (!LoadString(hInst, IDS_ERROR, title, COUNT_OF(title)))
86 lstrcpy(title, TEXT("Error"));
87 status = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
88 NULL, error_code, 0, (LPTSTR)&lpMsgBuf, 0, NULL);
89 if (!status)
90 lpMsgBuf = (LPTSTR)fallback;
91 MessageBox(hwnd, lpMsgBuf, title, MB_OK | MB_ICONERROR);
92 if (lpMsgBuf != fallback)
93 LocalFree(lpMsgBuf);
96 BOOL change_dword_base(HWND hwndDlg, BOOL toHex)
98 TCHAR buf[128];
99 DWORD val;
101 if (!GetDlgItemText(hwndDlg, IDC_VALUE_DATA, buf, COUNT_OF(buf))) return FALSE;
102 if (!_stscanf(buf, toHex ? "%ld" : "%lx", &val)) return FALSE;
103 wsprintf(buf, toHex ? "%lx" : "%ld", val);
104 return SetDlgItemText(hwndDlg, IDC_VALUE_DATA, buf);
107 INT_PTR CALLBACK modify_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
109 TCHAR* valueData;
110 HWND hwndValue;
111 int len;
113 switch(uMsg) {
114 case WM_INITDIALOG:
115 SetDlgItemText(hwndDlg, IDC_VALUE_NAME, editValueName);
116 SetDlgItemText(hwndDlg, IDC_VALUE_DATA, stringValueData);
117 CheckRadioButton(hwndDlg, IDC_DWORD_HEX, IDC_DWORD_DEC, isDecimal ? IDC_DWORD_DEC : IDC_DWORD_HEX);
118 return TRUE;
119 case WM_COMMAND:
120 switch (LOWORD(wParam)) {
121 case IDC_DWORD_HEX:
122 if (isDecimal && change_dword_base(hwndDlg, TRUE)) isDecimal = FALSE;
123 break;
124 case IDC_DWORD_DEC:
125 if (!isDecimal && change_dword_base(hwndDlg, FALSE)) isDecimal = TRUE;
126 break;
127 case IDOK:
128 if ((hwndValue = GetDlgItem(hwndDlg, IDC_VALUE_DATA))) {
129 len = GetWindowTextLength(hwndValue);
130 if ((valueData = HeapReAlloc(GetProcessHeap(), 0, stringValueData, (len + 1) * sizeof(TCHAR)))) {
131 stringValueData = valueData;
132 if (!GetWindowText(hwndValue, stringValueData, len + 1))
133 *stringValueData = 0;
136 /* Fall through */
137 case IDCANCEL:
138 EndDialog(hwndDlg, wParam);
139 return TRUE;
142 return FALSE;
145 static BOOL check_value(HWND hwnd, HKEY hKey, LPCTSTR valueName)
147 LONG lRet = RegQueryValueEx(hKey, valueName ? valueName : _T(""), 0, NULL, 0, NULL);
148 if(lRet != ERROR_SUCCESS) return FALSE;
149 return TRUE;
152 static LPTSTR read_value(HWND hwnd, HKEY hKey, LPCTSTR valueName, DWORD *lpType, LONG *len)
154 DWORD valueDataLen;
155 LPTSTR buffer = NULL;
156 LONG lRet;
158 lRet = RegQueryValueEx(hKey, valueName ? valueName : _T(""), 0, lpType, 0, &valueDataLen);
159 if (lRet != ERROR_SUCCESS) {
160 if (lRet == ERROR_FILE_NOT_FOUND && !valueName) { /* no default value here, make it up */
161 if (len) *len = 1;
162 if (lpType) *lpType = REG_SZ;
163 buffer = HeapAlloc(GetProcessHeap(), 0, 1);
164 *buffer = '\0';
165 return buffer;
167 error(hwnd, IDS_BAD_VALUE, valueName);
168 goto done;
170 if ( *lpType == REG_DWORD ) valueDataLen = sizeof(DWORD);
171 if (!(buffer = HeapAlloc(GetProcessHeap(), 0, valueDataLen))) {
172 error(hwnd, IDS_TOO_BIG_VALUE, valueDataLen);
173 goto done;
175 lRet = RegQueryValueEx(hKey, valueName, 0, 0, buffer, &valueDataLen);
176 if (lRet != ERROR_SUCCESS) {
177 error(hwnd, IDS_BAD_VALUE, valueName);
178 goto done;
181 if(len) *len = valueDataLen;
182 return buffer;
184 done:
185 HeapFree(GetProcessHeap(), 0, buffer);
186 return NULL;
189 BOOL CreateKey(HWND hwnd, HKEY hKeyRoot, LPCTSTR keyPath, LPTSTR keyName)
191 BOOL result = FALSE;
192 LONG lRet = ERROR_SUCCESS;
193 HKEY retKey;
194 TCHAR newKey[MAX_NEW_KEY_LEN - 4];
195 int keyNum;
196 HKEY hKey;
198 lRet = RegOpenKeyEx(hKeyRoot, keyPath, 0, KEY_CREATE_SUB_KEY, &hKey);
199 if (lRet != ERROR_SUCCESS) {
200 error_code_messagebox(hwnd, lRet);
201 goto done;
204 if (!LoadString(GetModuleHandle(0), IDS_NEWKEY, newKey, COUNT_OF(newKey))) goto done;
206 /* try to find out a name for the newly create key (max 100 times) */
207 for (keyNum = 1; keyNum < 100; keyNum++) {
208 wsprintf(keyName, newKey, keyNum);
209 lRet = RegOpenKey(hKey, keyName, &retKey);
210 if (lRet != ERROR_SUCCESS) break;
211 RegCloseKey(retKey);
213 if (lRet == ERROR_SUCCESS) goto done;
215 lRet = RegCreateKey(hKey, keyName, &retKey);
216 if (lRet != ERROR_SUCCESS) {
217 error_code_messagebox(hwnd, lRet);
218 goto done;
221 result = TRUE;
223 done:
224 RegCloseKey(retKey);
225 return result;
228 BOOL ModifyValue(HWND hwnd, HKEY hKeyRoot, LPCTSTR keyPath, LPCTSTR valueName)
230 BOOL result = FALSE;
231 DWORD type;
232 LONG lRet;
233 HKEY hKey;
235 lRet = RegOpenKeyEx(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
236 if (lRet != ERROR_SUCCESS) {
237 error_code_messagebox(hwnd, lRet);
238 return FALSE;
241 editValueName = valueName ? valueName : g_pszDefaultValueName;
242 if(!(stringValueData = read_value(hwnd, hKey, valueName, &type, 0))) goto done;
244 if ( (type == REG_SZ) || (type == REG_EXPAND_SZ) ) {
245 if (DialogBox(0, MAKEINTRESOURCE(IDD_EDIT_STRING), hwnd, modify_dlgproc) == IDOK) {
246 lRet = RegSetValueEx(hKey, valueName, 0, type, stringValueData, lstrlen(stringValueData) + 1);
247 if (lRet == ERROR_SUCCESS) result = TRUE;
248 else error_code_messagebox(hwnd, lRet);
250 } else if ( type == REG_DWORD ) {
251 wsprintf(stringValueData, isDecimal ? "%ld" : "%lx", *((DWORD*)stringValueData));
252 if (DialogBox(0, MAKEINTRESOURCE(IDD_EDIT_DWORD), hwnd, modify_dlgproc) == IDOK) {
253 DWORD val;
254 if (_stscanf(stringValueData, isDecimal ? "%ld" : "%lx", &val)) {
255 lRet = RegSetValueEx(hKey, valueName, 0, type, (BYTE*)&val, sizeof(val));
256 if (lRet == ERROR_SUCCESS) result = TRUE;
257 else error_code_messagebox(hwnd, lRet);
260 } else {
261 error(hwnd, IDS_UNSUPPORTED_TYPE, type);
264 done:
265 HeapFree(GetProcessHeap(), 0, stringValueData);
266 stringValueData = NULL;
267 RegCloseKey(hKey);
268 return result;
271 BOOL DeleteKey(HWND hwnd, HKEY hKeyRoot, LPCTSTR keyPath)
273 BOOL result = FALSE;
274 LONG lRet;
275 HKEY hKey;
277 lRet = RegOpenKeyEx(hKeyRoot, keyPath, 0, KEY_READ|KEY_SET_VALUE, &hKey);
278 if (lRet != ERROR_SUCCESS) {
279 error_code_messagebox(hwnd, lRet);
280 return FALSE;
283 if (messagebox(hwnd, MB_YESNO | MB_ICONEXCLAMATION, IDS_DELETE_BOX_TITLE, IDS_DELETE_BOX_TEXT, keyPath) != IDYES)
284 goto done;
286 lRet = SHDeleteKey(hKeyRoot, keyPath);
287 if (lRet != ERROR_SUCCESS) {
288 error(hwnd, IDS_BAD_KEY, keyPath);
289 goto done;
291 result = TRUE;
293 done:
294 RegCloseKey(hKey);
295 return result;
298 BOOL DeleteValue(HWND hwnd, HKEY hKeyRoot, LPCTSTR keyPath, LPCTSTR valueName)
300 BOOL result = FALSE;
301 LONG lRet;
302 HKEY hKey;
303 LPCSTR visibleValueName = valueName ? valueName : g_pszDefaultValueName;
305 lRet = RegOpenKeyEx(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
306 if (lRet != ERROR_SUCCESS) return FALSE;
308 if (messagebox(hwnd, MB_YESNO | MB_ICONEXCLAMATION, IDS_DELETE_BOX_TITLE, IDS_DELETE_BOX_TEXT, visibleValueName) != IDYES)
309 goto done;
311 lRet = RegDeleteValue(hKey, valueName ? valueName : "");
312 if (lRet != ERROR_SUCCESS && valueName) {
313 error(hwnd, IDS_BAD_VALUE, valueName);
315 if (lRet != ERROR_SUCCESS) goto done;
316 result = TRUE;
318 done:
319 RegCloseKey(hKey);
320 return result;
323 BOOL CreateValue(HWND hwnd, HKEY hKeyRoot, LPCTSTR keyPath, DWORD valueType, LPTSTR valueName)
325 LONG lRet = ERROR_SUCCESS;
326 TCHAR newValue[256];
327 DWORD valueDword = 0;
328 BOOL result = FALSE;
329 int valueNum;
330 HKEY hKey;
332 lRet = RegOpenKeyEx(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
333 if (lRet != ERROR_SUCCESS) {
334 error_code_messagebox(hwnd, lRet);
335 return FALSE;
338 if (!LoadString(GetModuleHandle(0), IDS_NEWVALUE, newValue, COUNT_OF(newValue))) goto done;
340 /* try to find out a name for the newly create key (max 100 times) */
341 for (valueNum = 1; valueNum < 100; valueNum++) {
342 wsprintf(valueName, newValue, valueNum);
343 lRet = RegQueryValueEx(hKey, valueName, 0, 0, 0, 0);
344 if (lRet == ERROR_FILE_NOT_FOUND) break;
346 if (lRet != ERROR_FILE_NOT_FOUND) {
347 error_code_messagebox(hwnd, lRet);
348 goto done;
351 lRet = RegSetValueEx(hKey, valueName, 0, valueType, (BYTE*)&valueDword, sizeof(DWORD));
352 if (lRet != ERROR_SUCCESS) {
353 error_code_messagebox(hwnd, lRet);
354 goto done;
356 result = TRUE;
358 done:
359 RegCloseKey(hKey);
360 return result;
363 BOOL RenameValue(HWND hwnd, HKEY hKeyRoot, LPCTSTR keyPath, LPCTSTR oldName, LPCTSTR newName)
365 LPTSTR value = NULL;
366 DWORD type;
367 LONG len, lRet;
368 BOOL result = FALSE;
369 HKEY hKey;
371 if (!oldName) return FALSE;
372 if (!newName) return FALSE;
374 lRet = RegOpenKeyEx(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
375 if (lRet != ERROR_SUCCESS) {
376 error_code_messagebox(hwnd, lRet);
377 return FALSE;
379 /* check if value already exists */
380 if (check_value(hwnd, hKey, newName)) goto done;
381 value = read_value(hwnd, hKey, oldName, &type, &len);
382 if(!value) goto done;
383 lRet = RegSetValueEx(hKey, newName, 0, type, (BYTE*)value, len);
384 if (lRet != ERROR_SUCCESS) {
385 error_code_messagebox(hwnd, lRet);
386 goto done;
388 lRet = RegDeleteValue(hKey, oldName);
389 if (lRet != ERROR_SUCCESS) {
390 RegDeleteValue(hKey, newName);
391 error_code_messagebox(hwnd, lRet);
392 goto done;
394 result = TRUE;
396 done:
397 HeapFree(GetProcessHeap(), 0, value);
398 RegCloseKey(hKey);
399 return result;
403 BOOL RenameKey(HWND hwnd, HKEY hRootKey, LPCTSTR keyPath, LPCTSTR newName)
405 LPTSTR parentPath = 0;
406 LPCTSTR srcSubKey = 0;
407 HKEY parentKey = 0;
408 HKEY destKey = 0;
409 BOOL result = FALSE;
410 LONG lRet;
411 DWORD disposition;
413 if (!keyPath || !newName) return FALSE;
415 if (!strrchr(keyPath, '\\')) {
416 parentKey = hRootKey;
417 srcSubKey = keyPath;
418 } else {
419 parentPath = strdup(keyPath);
420 srcSubKey = strrchr(parentPath, '\\') + 1;
421 *((LPTSTR)srcSubKey - 1) = 0;
422 lRet = RegOpenKeyEx(hRootKey, parentPath, 0, KEY_READ | KEY_CREATE_SUB_KEY, &parentKey);
423 if (lRet != ERROR_SUCCESS) {
424 error_code_messagebox(hwnd, lRet);
425 goto done;
429 /* The following fails if the old name is the same as the new name. */
430 if (!strcmp(srcSubKey, newName)) goto done;
432 lRet = RegCreateKeyEx(parentKey, newName, 0, NULL, REG_OPTION_NON_VOLATILE,
433 KEY_WRITE, NULL /* FIXME */, &destKey, &disposition);
434 if (disposition == REG_OPENED_EXISTING_KEY)
435 lRet = ERROR_FILE_EXISTS; /* FIXME: we might want a better error message than this */
436 if (lRet != ERROR_SUCCESS) {
437 error_code_messagebox(hwnd, lRet);
438 goto done;
441 /* FIXME: SHCopyKey does not copy the security attributes */
442 lRet = SHCopyKey(parentKey, srcSubKey, destKey, 0);
443 if (lRet != ERROR_SUCCESS) {
444 RegCloseKey(destKey);
445 RegDeleteKey(parentKey, newName);
446 error_code_messagebox(hwnd, lRet);
447 goto done;
450 lRet = SHDeleteKey(hRootKey, keyPath);
451 if (lRet != ERROR_SUCCESS) {
452 error_code_messagebox(hwnd, lRet);
453 goto done;
456 result = TRUE;
458 done:
459 RegCloseKey(destKey);
460 if (parentKey) {
461 RegCloseKey(parentKey);
462 free(parentPath);
464 return result;