makefiles: Add a helper to split large file remove commands.
[wine.git] / programs / regedit / edit.c
blobc8b77bd75551dad022ea456c61f9a908411219c2
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #define WIN32_LEAN_AND_MEAN /* Exclude rarely-used stuff from Windows headers */
23 #include <windows.h>
24 #include <commctrl.h>
25 #include <commdlg.h>
26 #include <cderr.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <shellapi.h>
30 #include <shlwapi.h>
32 #include "wine/heap.h"
33 #include "wine/unicode.h"
34 #include "main.h"
35 #include "regproc.h"
36 #include "resource.h"
38 static const WCHAR* editValueName;
39 static WCHAR* stringValueData;
40 static BOOL isDecimal;
42 struct edit_params
44 HKEY hKey;
45 LPCWSTR lpszValueName;
46 void *pData;
47 LONG cbData;
50 static int vmessagebox(HWND hwnd, int buttons, int titleId, int resId, __ms_va_list va_args)
52 WCHAR title[256];
53 WCHAR fmt[1024];
54 WCHAR *str;
55 int ret;
57 LoadStringW(hInst, titleId, title, COUNT_OF(title));
58 LoadStringW(hInst, resId, fmt, COUNT_OF(fmt));
60 FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
61 fmt, 0, 0, (WCHAR *)&str, 0, &va_args);
62 ret = MessageBoxW(hwnd, str, title, buttons);
63 LocalFree(str);
65 return ret;
68 int WINAPIV messagebox(HWND hwnd, int buttons, int titleId, int resId, ...)
70 __ms_va_list ap;
71 INT result;
73 __ms_va_start(ap, resId);
74 result = vmessagebox(hwnd, buttons, titleId, resId, ap);
75 __ms_va_end(ap);
77 return result;
80 static void WINAPIV error_code_messagebox(HWND hwnd, unsigned int msg_id, ...)
82 __ms_va_list ap;
84 __ms_va_start(ap, msg_id);
85 vmessagebox(hwnd, MB_OK|MB_ICONERROR, IDS_ERROR, msg_id, ap);
86 __ms_va_end(ap);
89 static BOOL change_dword_base(HWND hwndDlg, BOOL toHex)
91 static const WCHAR percent_u[] = {'%','u',0};
92 static const WCHAR percent_x[] = {'%','x',0};
94 WCHAR buf[128];
95 DWORD val;
97 if (!GetDlgItemTextW(hwndDlg, IDC_VALUE_DATA, buf, COUNT_OF(buf))) return FALSE;
98 if (!swscanf(buf, toHex ? percent_u : percent_x, &val)) return FALSE;
99 wsprintfW(buf, toHex ? percent_x : percent_u, val);
100 return SetDlgItemTextW(hwndDlg, IDC_VALUE_DATA, buf);
103 static INT_PTR CALLBACK modify_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
105 HWND hwndValue;
106 int len;
108 switch(uMsg) {
109 case WM_INITDIALOG:
110 SetDlgItemTextW(hwndDlg, IDC_VALUE_NAME, editValueName);
111 SetDlgItemTextW(hwndDlg, IDC_VALUE_DATA, stringValueData);
112 CheckRadioButton(hwndDlg, IDC_DWORD_HEX, IDC_DWORD_DEC, isDecimal ? IDC_DWORD_DEC : IDC_DWORD_HEX);
113 return TRUE;
114 case WM_COMMAND:
115 switch (LOWORD(wParam)) {
116 case IDC_DWORD_HEX:
117 if (isDecimal && change_dword_base(hwndDlg, TRUE)) isDecimal = FALSE;
118 break;
119 case IDC_DWORD_DEC:
120 if (!isDecimal && change_dword_base(hwndDlg, FALSE)) isDecimal = TRUE;
121 break;
122 case IDOK:
123 if ((hwndValue = GetDlgItem(hwndDlg, IDC_VALUE_DATA))) {
124 len = GetWindowTextLengthW(hwndValue);
125 stringValueData = heap_xrealloc(stringValueData, (len + 1) * sizeof(WCHAR));
126 if (!GetWindowTextW(hwndValue, stringValueData, len + 1))
127 *stringValueData = 0;
129 /* Fall through */
130 case IDCANCEL:
131 EndDialog(hwndDlg, wParam);
132 return TRUE;
135 return FALSE;
138 static INT_PTR CALLBACK bin_modify_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
140 struct edit_params *params;
141 LPBYTE pData;
142 LONG cbData;
143 LONG lRet;
145 switch(uMsg) {
146 case WM_INITDIALOG:
147 params = (struct edit_params *)lParam;
148 SetWindowLongPtrW(hwndDlg, DWLP_USER, (ULONG_PTR)params);
149 if (params->lpszValueName)
150 SetDlgItemTextW(hwndDlg, IDC_VALUE_NAME, params->lpszValueName);
151 else
152 SetDlgItemTextW(hwndDlg, IDC_VALUE_NAME, g_pszDefaultValueName);
153 SendDlgItemMessageW(hwndDlg, IDC_VALUE_DATA, HEM_SETDATA, (WPARAM)params->cbData, (LPARAM)params->pData);
154 return TRUE;
155 case WM_COMMAND:
156 switch (LOWORD(wParam)) {
157 case IDOK:
158 params = (struct edit_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
159 cbData = SendDlgItemMessageW(hwndDlg, IDC_VALUE_DATA, HEM_GETDATA, 0, 0);
160 pData = heap_xalloc(cbData);
162 if (pData)
164 SendDlgItemMessageW(hwndDlg, IDC_VALUE_DATA, HEM_GETDATA, (WPARAM)cbData, (LPARAM)pData);
165 lRet = RegSetValueExW(params->hKey, params->lpszValueName, 0, REG_BINARY, pData, cbData);
166 heap_free(pData);
168 else
169 lRet = ERROR_OUTOFMEMORY;
171 if (lRet == ERROR_SUCCESS)
172 EndDialog(hwndDlg, 1);
173 else
175 error_code_messagebox(hwndDlg, IDS_SET_VALUE_FAILED);
176 EndDialog(hwndDlg, 0);
178 return TRUE;
179 case IDCANCEL:
180 EndDialog(hwndDlg, 0);
181 return TRUE;
184 return FALSE;
187 static BOOL check_value(HWND hwnd, HKEY hKey, LPCWSTR valueName)
189 WCHAR empty = 0;
190 LONG lRet = RegQueryValueExW(hKey, valueName ? valueName : &empty, 0, NULL, 0, NULL);
191 if(lRet != ERROR_SUCCESS) return FALSE;
192 return TRUE;
195 static LPWSTR read_value(HWND hwnd, HKEY hKey, LPCWSTR valueName, DWORD *lpType, LONG *len)
197 DWORD valueDataLen;
198 LPWSTR buffer = NULL;
199 LONG lRet;
200 WCHAR empty = 0;
202 lRet = RegQueryValueExW(hKey, valueName ? valueName : &empty, 0, lpType, 0, &valueDataLen);
203 if (lRet != ERROR_SUCCESS) {
204 if (lRet == ERROR_FILE_NOT_FOUND && !valueName) { /* no default value here, make it up */
205 if (len) *len = 1;
206 if (lpType) *lpType = REG_SZ;
207 buffer = heap_xalloc(sizeof(WCHAR));
208 *buffer = '\0';
209 return buffer;
211 error_code_messagebox(hwnd, IDS_BAD_VALUE, valueName);
212 goto done;
214 if ( *lpType == REG_DWORD ) valueDataLen = sizeof(DWORD);
215 buffer = heap_xalloc(valueDataLen + sizeof(WCHAR));
216 lRet = RegQueryValueExW(hKey, valueName, 0, 0, (LPBYTE)buffer, &valueDataLen);
217 if (lRet != ERROR_SUCCESS) {
218 error_code_messagebox(hwnd, IDS_BAD_VALUE, valueName);
219 goto done;
221 if((valueDataLen % sizeof(WCHAR)) == 0)
222 buffer[valueDataLen / sizeof(WCHAR)] = 0;
223 if(len) *len = valueDataLen;
224 return buffer;
226 done:
227 heap_free(buffer);
228 return NULL;
231 BOOL CreateKey(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPWSTR keyName)
233 BOOL result = FALSE;
234 LONG lRet = ERROR_SUCCESS;
235 HKEY retKey = NULL;
236 WCHAR newKey[MAX_NEW_KEY_LEN - 4];
237 int keyNum;
238 HKEY hKey;
240 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_CREATE_SUB_KEY, &hKey);
241 if (lRet != ERROR_SUCCESS) {
242 error_code_messagebox(hwnd, IDS_CREATE_KEY_FAILED);
243 goto done;
246 if (!LoadStringW(GetModuleHandleW(0), IDS_NEWKEY, newKey, COUNT_OF(newKey))) goto done;
248 /* try to find a name for the key being created (maximum = 100 attempts) */
249 for (keyNum = 1; keyNum < 100; keyNum++) {
250 wsprintfW(keyName, newKey, keyNum);
251 lRet = RegOpenKeyW(hKey, keyName, &retKey);
252 if (lRet != ERROR_SUCCESS) break;
253 RegCloseKey(retKey);
255 if (lRet == ERROR_SUCCESS) goto done;
257 lRet = RegCreateKeyW(hKey, keyName, &retKey);
258 if (lRet != ERROR_SUCCESS) {
259 error_code_messagebox(hwnd, IDS_CREATE_KEY_FAILED);
260 goto done;
263 result = TRUE;
265 done:
266 RegCloseKey(retKey);
267 return result;
270 BOOL ModifyValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR valueName)
272 BOOL result = FALSE;
273 DWORD type;
274 LONG lRet;
275 HKEY hKey;
276 LONG len;
278 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
279 if (lRet != ERROR_SUCCESS) {
280 error_code_messagebox(hwnd, IDS_SET_VALUE_FAILED);
281 return FALSE;
284 editValueName = valueName ? valueName : g_pszDefaultValueName;
285 if(!(stringValueData = read_value(hwnd, hKey, valueName, &type, &len))) goto done;
287 if ( (type == REG_SZ) || (type == REG_EXPAND_SZ) ) {
288 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_STRING), hwnd, modify_dlgproc) == IDOK) {
289 lRet = RegSetValueExW(hKey, valueName, 0, type, (LPBYTE)stringValueData, (lstrlenW(stringValueData) + 1) * sizeof(WCHAR));
290 if (lRet == ERROR_SUCCESS) result = TRUE;
291 else error_code_messagebox(hwnd, IDS_SET_VALUE_FAILED);
293 } else if ( type == REG_DWORD ) {
294 const WCHAR u[] = {'%','u',0};
295 const WCHAR x[] = {'%','x',0};
296 wsprintfW(stringValueData, isDecimal ? u : x, *((DWORD*)stringValueData));
297 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_DWORD), hwnd, modify_dlgproc) == IDOK) {
298 DWORD val;
299 CHAR* valueA = GetMultiByteString(stringValueData);
300 if (sscanf(valueA, isDecimal ? "%u" : "%x", &val)) {
301 lRet = RegSetValueExW(hKey, valueName, 0, type, (BYTE*)&val, sizeof(val));
302 if (lRet == ERROR_SUCCESS) result = TRUE;
303 else error_code_messagebox(hwnd, IDS_SET_VALUE_FAILED);
305 heap_free(valueA);
307 } else if ( type == REG_MULTI_SZ ) {
308 WCHAR char1 = '\r', char2 = '\n';
309 WCHAR *tmpValueData = NULL;
310 INT i, j, count;
312 for ( i = 0, count = 0; i < len - 1; i++)
313 if ( !stringValueData[i] && stringValueData[i + 1] )
314 count++;
315 tmpValueData = heap_xalloc((len + count) * sizeof(WCHAR));
317 for ( i = 0, j = 0; i < len - 1; i++)
319 if ( !stringValueData[i] && stringValueData[i + 1])
321 tmpValueData[j++] = char1;
322 tmpValueData[j++] = char2;
324 else
325 tmpValueData[j++] = stringValueData[i];
327 tmpValueData[j] = stringValueData[i];
328 heap_free(stringValueData);
329 stringValueData = tmpValueData;
330 tmpValueData = NULL;
332 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_MULTI_STRING), hwnd, modify_dlgproc) == IDOK)
334 len = lstrlenW( stringValueData );
335 tmpValueData = heap_xalloc((len + 2) * sizeof(WCHAR));
337 for ( i = 0, j = 0; i < len - 1; i++)
339 if ( stringValueData[i] == char1 && stringValueData[i + 1] == char2)
341 if ( tmpValueData[j - 1] != 0)
342 tmpValueData[j++] = 0;
343 i++;
345 else
346 tmpValueData[j++] = stringValueData[i];
348 tmpValueData[j++] = stringValueData[i];
349 tmpValueData[j++] = 0;
350 tmpValueData[j++] = 0;
351 heap_free(stringValueData);
352 stringValueData = tmpValueData;
354 lRet = RegSetValueExW(hKey, valueName, 0, type, (LPBYTE)stringValueData, j * sizeof(WCHAR));
355 if (lRet == ERROR_SUCCESS) result = TRUE;
356 else error_code_messagebox(hwnd, IDS_SET_VALUE_FAILED);
359 else /* hex data types */
361 struct edit_params params;
363 params.hKey = hKey;
364 params.lpszValueName = valueName;
365 params.pData = stringValueData;
366 params.cbData = len;
367 result = DialogBoxParamW(NULL, MAKEINTRESOURCEW(IDD_EDIT_BINARY), hwnd,
368 bin_modify_dlgproc, (LPARAM)&params);
371 /* Update the listview item with the new data string */
372 if (result)
374 int index = SendMessageW(g_pChildWnd->hListWnd, LVM_GETNEXTITEM, -1,
375 MAKELPARAM(LVNI_FOCUSED | LVNI_SELECTED, 0));
376 heap_free(stringValueData);
377 stringValueData = read_value(hwnd, hKey, valueName, &type, &len);
378 format_value_data(g_pChildWnd->hListWnd, index, type, stringValueData, len);
381 done:
382 heap_free(stringValueData);
383 stringValueData = NULL;
384 RegCloseKey(hKey);
385 return result;
388 BOOL DeleteKey(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath)
390 BOOL result = FALSE;
391 LONG lRet;
392 HKEY hKey;
394 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ|KEY_SET_VALUE, &hKey);
395 if (lRet != ERROR_SUCCESS) {
396 error_code_messagebox(hwnd, IDS_DELETE_KEY_FAILED);
397 return FALSE;
400 if (messagebox(hwnd, MB_YESNO | MB_ICONEXCLAMATION, IDS_DELETE_KEY_TITLE,
401 IDS_DELETE_KEY_TEXT) != IDYES)
402 goto done;
404 lRet = SHDeleteKeyW(hKeyRoot, keyPath);
405 if (lRet != ERROR_SUCCESS) {
406 error_code_messagebox(hwnd, IDS_BAD_KEY, keyPath);
407 goto done;
409 result = TRUE;
411 done:
412 RegCloseKey(hKey);
413 return result;
416 BOOL DeleteValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR valueName, BOOL showMessageBox)
418 BOOL result = FALSE;
419 LONG lRet;
420 HKEY hKey;
421 LPCWSTR visibleValueName = valueName ? valueName : g_pszDefaultValueName;
422 WCHAR empty = 0;
424 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
425 if (lRet != ERROR_SUCCESS) return FALSE;
427 if (showMessageBox)
429 if (messagebox(hwnd, MB_YESNO | MB_ICONEXCLAMATION, IDS_DELETE_VALUE_TITLE, IDS_DELETE_VALUE_TEXT,
430 visibleValueName) != IDYES)
431 goto done;
434 lRet = RegDeleteValueW(hKey, valueName ? valueName : &empty);
435 if (lRet != ERROR_SUCCESS && valueName) {
436 error_code_messagebox(hwnd, IDS_BAD_VALUE, valueName);
438 if (lRet != ERROR_SUCCESS) goto done;
439 result = TRUE;
441 done:
442 RegCloseKey(hKey);
443 return result;
446 BOOL CreateValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, DWORD valueType, LPWSTR valueName)
448 LONG lRet = ERROR_SUCCESS;
449 WCHAR newValue[256];
450 DWORD valueDword = 0;
451 BOOL result = FALSE;
452 int valueNum, index;
453 HKEY hKey;
454 LVITEMW item;
456 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
457 if (lRet != ERROR_SUCCESS) {
458 error_code_messagebox(hwnd, IDS_CREATE_VALUE_FAILED);
459 return FALSE;
462 if (!LoadStringW(GetModuleHandleW(0), IDS_NEWVALUE, newValue, COUNT_OF(newValue))) goto done;
464 /* try to find a name for the value being created (maximum = 100 attempts) */
465 for (valueNum = 1; valueNum < 100; valueNum++) {
466 wsprintfW(valueName, newValue, valueNum);
467 lRet = RegQueryValueExW(hKey, valueName, 0, 0, 0, 0);
468 if (lRet == ERROR_FILE_NOT_FOUND) break;
470 if (lRet != ERROR_FILE_NOT_FOUND) {
471 error_code_messagebox(hwnd, IDS_CREATE_VALUE_FAILED);
472 goto done;
475 lRet = RegSetValueExW(hKey, valueName, 0, valueType, (BYTE*)&valueDword, sizeof(DWORD));
476 if (lRet != ERROR_SUCCESS) {
477 error_code_messagebox(hwnd, IDS_CREATE_VALUE_FAILED);
478 goto done;
481 /* Add the new item to the listview */
482 index = AddEntryToList(g_pChildWnd->hListWnd, valueName, valueType,
483 (BYTE *)&valueDword, sizeof(DWORD), -1);
484 item.state = LVIS_FOCUSED | LVIS_SELECTED;
485 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
486 SendMessageW(g_pChildWnd->hListWnd, LVM_SETITEMSTATE, index, (LPARAM)&item);
488 result = TRUE;
490 done:
491 RegCloseKey(hKey);
492 return result;
495 BOOL RenameValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR oldName, LPCWSTR newName)
497 LPWSTR value = NULL;
498 DWORD type;
499 LONG len, lRet;
500 BOOL result = FALSE;
501 HKEY hKey;
503 if (!oldName) return FALSE;
504 if (!newName) return FALSE;
506 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
507 if (lRet != ERROR_SUCCESS) {
508 error_code_messagebox(hwnd, IDS_RENAME_VALUE_FAILED);
509 return FALSE;
511 /* check if the value already exists */
512 if (check_value(hwnd, hKey, newName)) {
513 error_code_messagebox(hwnd, IDS_VALUE_EXISTS, oldName);
514 goto done;
516 value = read_value(hwnd, hKey, oldName, &type, &len);
517 if(!value) goto done;
518 lRet = RegSetValueExW(hKey, newName, 0, type, (BYTE*)value, len);
519 if (lRet != ERROR_SUCCESS) {
520 error_code_messagebox(hwnd, IDS_RENAME_VALUE_FAILED);
521 goto done;
523 lRet = RegDeleteValueW(hKey, oldName);
524 if (lRet != ERROR_SUCCESS) {
525 RegDeleteValueW(hKey, newName);
526 error_code_messagebox(hwnd, IDS_RENAME_VALUE_FAILED);
527 goto done;
529 result = TRUE;
531 done:
532 heap_free(value);
533 RegCloseKey(hKey);
534 return result;
538 BOOL RenameKey(HWND hwnd, HKEY hRootKey, LPCWSTR keyPath, LPCWSTR newName)
540 LPWSTR parentPath = 0;
541 LPCWSTR srcSubKey = 0;
542 HKEY parentKey = 0;
543 HKEY destKey = 0;
544 BOOL result = FALSE;
545 LONG lRet;
546 DWORD disposition;
548 if (!keyPath || !newName) return FALSE;
550 if (!strrchrW(keyPath, '\\')) {
551 parentKey = hRootKey;
552 srcSubKey = keyPath;
553 } else {
554 LPWSTR srcSubKey_copy;
556 parentPath = heap_xalloc((lstrlenW(keyPath) + 1) * sizeof(WCHAR));
557 lstrcpyW(parentPath, keyPath);
558 srcSubKey_copy = strrchrW(parentPath, '\\');
559 *srcSubKey_copy = 0;
560 srcSubKey = srcSubKey_copy + 1;
561 lRet = RegOpenKeyExW(hRootKey, parentPath, 0, KEY_READ | KEY_CREATE_SUB_KEY, &parentKey);
562 if (lRet != ERROR_SUCCESS) {
563 error_code_messagebox(hwnd, IDS_RENAME_KEY_FAILED);
564 goto done;
568 /* The following fails if the old name is the same as the new name. */
569 if (!lstrcmpW(srcSubKey, newName)) goto done;
571 lRet = RegCreateKeyExW(parentKey, newName, 0, NULL, REG_OPTION_NON_VOLATILE,
572 KEY_WRITE, NULL /* FIXME */, &destKey, &disposition);
573 if (disposition == REG_OPENED_EXISTING_KEY)
574 lRet = ERROR_FILE_EXISTS;
575 if (lRet != ERROR_SUCCESS) {
576 error_code_messagebox(hwnd, IDS_KEY_EXISTS, srcSubKey);
577 goto done;
580 /* FIXME: SHCopyKey does not copy the security attributes */
581 lRet = SHCopyKeyW(parentKey, srcSubKey, destKey, 0);
582 if (lRet != ERROR_SUCCESS) {
583 RegCloseKey(destKey);
584 RegDeleteKeyW(parentKey, newName);
585 error_code_messagebox(hwnd, IDS_RENAME_KEY_FAILED);
586 goto done;
589 lRet = SHDeleteKeyW(hRootKey, keyPath);
590 if (lRet != ERROR_SUCCESS) {
591 error_code_messagebox(hwnd, IDS_RENAME_KEY_FAILED);
592 goto done;
595 result = TRUE;
597 done:
598 RegCloseKey(destKey);
599 if (parentKey) {
600 RegCloseKey(parentKey);
601 heap_free(parentPath);
603 return result;