winhlp32: Remove redundant comparison.
[wine.git] / programs / regedit / edit.c
blob6ccce00ce5fd7a6806d61e90ab2b5abda22b5c12
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/unicode.h"
33 #include "main.h"
34 #include "regproc.h"
35 #include "resource.h"
37 static const WCHAR* editValueName;
38 static WCHAR* stringValueData;
39 static BOOL isDecimal;
41 struct edit_params
43 HKEY hKey;
44 LPCWSTR lpszValueName;
45 void *pData;
46 LONG cbData;
49 static int vmessagebox(HWND hwnd, int buttons, int titleId, int resId, __ms_va_list va_args)
51 WCHAR title[256];
52 WCHAR fmt[1024];
53 WCHAR *str;
54 int ret;
56 LoadStringW(hInst, titleId, title, COUNT_OF(title));
57 LoadStringW(hInst, resId, fmt, COUNT_OF(fmt));
59 FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
60 fmt, 0, 0, (WCHAR *)&str, 0, &va_args);
61 ret = MessageBoxW(hwnd, str, title, buttons);
62 LocalFree(str);
64 return ret;
67 int WINAPIV messagebox(HWND hwnd, int buttons, int titleId, int resId, ...)
69 __ms_va_list ap;
70 INT result;
72 __ms_va_start(ap, resId);
73 result = vmessagebox(hwnd, buttons, titleId, resId, ap);
74 __ms_va_end(ap);
76 return result;
79 static void WINAPIV error_code_messagebox(HWND hwnd, unsigned int msg_id, ...)
81 __ms_va_list ap;
83 __ms_va_start(ap, msg_id);
84 vmessagebox(hwnd, MB_OK|MB_ICONERROR, IDS_ERROR, msg_id, ap);
85 __ms_va_end(ap);
88 static BOOL change_dword_base(HWND hwndDlg, BOOL toHex)
90 static const WCHAR percent_u[] = {'%','u',0};
91 static const WCHAR percent_x[] = {'%','x',0};
93 WCHAR buf[128];
94 DWORD val;
96 if (!GetDlgItemTextW(hwndDlg, IDC_VALUE_DATA, buf, COUNT_OF(buf))) return FALSE;
97 if (!swscanf(buf, toHex ? percent_u : percent_x, &val)) return FALSE;
98 wsprintfW(buf, toHex ? percent_x : percent_u, val);
99 return SetDlgItemTextW(hwndDlg, IDC_VALUE_DATA, buf);
102 static INT_PTR CALLBACK modify_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
104 HWND hwndValue;
105 int len;
107 switch(uMsg) {
108 case WM_INITDIALOG:
109 SetDlgItemTextW(hwndDlg, IDC_VALUE_NAME, editValueName);
110 SetDlgItemTextW(hwndDlg, IDC_VALUE_DATA, stringValueData);
111 CheckRadioButton(hwndDlg, IDC_DWORD_HEX, IDC_DWORD_DEC, isDecimal ? IDC_DWORD_DEC : IDC_DWORD_HEX);
112 return TRUE;
113 case WM_COMMAND:
114 switch (LOWORD(wParam)) {
115 case IDC_DWORD_HEX:
116 if (isDecimal && change_dword_base(hwndDlg, TRUE)) isDecimal = FALSE;
117 break;
118 case IDC_DWORD_DEC:
119 if (!isDecimal && change_dword_base(hwndDlg, FALSE)) isDecimal = TRUE;
120 break;
121 case IDOK:
122 if ((hwndValue = GetDlgItem(hwndDlg, IDC_VALUE_DATA))) {
123 len = GetWindowTextLengthW(hwndValue);
124 stringValueData = heap_xrealloc(stringValueData, (len + 1) * sizeof(WCHAR));
125 if (!GetWindowTextW(hwndValue, stringValueData, len + 1))
126 *stringValueData = 0;
128 /* Fall through */
129 case IDCANCEL:
130 EndDialog(hwndDlg, wParam);
131 return TRUE;
134 return FALSE;
137 static INT_PTR CALLBACK bin_modify_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
139 struct edit_params *params;
140 LPBYTE pData;
141 LONG cbData;
142 LONG lRet;
144 switch(uMsg) {
145 case WM_INITDIALOG:
146 params = (struct edit_params *)lParam;
147 SetWindowLongPtrW(hwndDlg, DWLP_USER, (ULONG_PTR)params);
148 if (params->lpszValueName)
149 SetDlgItemTextW(hwndDlg, IDC_VALUE_NAME, params->lpszValueName);
150 else
151 SetDlgItemTextW(hwndDlg, IDC_VALUE_NAME, g_pszDefaultValueName);
152 SendDlgItemMessageW(hwndDlg, IDC_VALUE_DATA, HEM_SETDATA, (WPARAM)params->cbData, (LPARAM)params->pData);
153 return TRUE;
154 case WM_COMMAND:
155 switch (LOWORD(wParam)) {
156 case IDOK:
157 params = (struct edit_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
158 cbData = SendDlgItemMessageW(hwndDlg, IDC_VALUE_DATA, HEM_GETDATA, 0, 0);
159 pData = heap_xalloc(cbData);
161 if (pData)
163 SendDlgItemMessageW(hwndDlg, IDC_VALUE_DATA, HEM_GETDATA, (WPARAM)cbData, (LPARAM)pData);
164 lRet = RegSetValueExW(params->hKey, params->lpszValueName, 0, REG_BINARY, pData, cbData);
165 heap_free(pData);
167 else
168 lRet = ERROR_OUTOFMEMORY;
170 if (lRet == ERROR_SUCCESS)
171 EndDialog(hwndDlg, 1);
172 else
174 error_code_messagebox(hwndDlg, IDS_SET_VALUE_FAILED);
175 EndDialog(hwndDlg, 0);
177 return TRUE;
178 case IDCANCEL:
179 EndDialog(hwndDlg, 0);
180 return TRUE;
183 return FALSE;
186 static BOOL check_value(HWND hwnd, HKEY hKey, LPCWSTR valueName)
188 WCHAR empty = 0;
189 LONG lRet = RegQueryValueExW(hKey, valueName ? valueName : &empty, 0, NULL, 0, NULL);
190 if(lRet != ERROR_SUCCESS) return FALSE;
191 return TRUE;
194 static LPWSTR read_value(HWND hwnd, HKEY hKey, LPCWSTR valueName, DWORD *lpType, LONG *len)
196 DWORD valueDataLen;
197 LPWSTR buffer = NULL;
198 LONG lRet;
199 WCHAR empty = 0;
201 lRet = RegQueryValueExW(hKey, valueName ? valueName : &empty, 0, lpType, 0, &valueDataLen);
202 if (lRet != ERROR_SUCCESS) {
203 if (lRet == ERROR_FILE_NOT_FOUND && !valueName) { /* no default value here, make it up */
204 if (len) *len = 1;
205 if (lpType) *lpType = REG_SZ;
206 buffer = heap_xalloc(sizeof(WCHAR));
207 *buffer = '\0';
208 return buffer;
210 error_code_messagebox(hwnd, IDS_BAD_VALUE, valueName);
211 goto done;
213 if ( *lpType == REG_DWORD ) valueDataLen = sizeof(DWORD);
214 buffer = heap_xalloc(valueDataLen + sizeof(WCHAR));
215 lRet = RegQueryValueExW(hKey, valueName, 0, 0, (LPBYTE)buffer, &valueDataLen);
216 if (lRet != ERROR_SUCCESS) {
217 error_code_messagebox(hwnd, IDS_BAD_VALUE, valueName);
218 goto done;
220 if((valueDataLen % sizeof(WCHAR)) == 0)
221 buffer[valueDataLen / sizeof(WCHAR)] = 0;
222 if(len) *len = valueDataLen;
223 return buffer;
225 done:
226 heap_free(buffer);
227 return NULL;
230 BOOL CreateKey(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPWSTR keyName)
232 BOOL result = FALSE;
233 LONG lRet = ERROR_SUCCESS;
234 HKEY retKey = NULL;
235 WCHAR newKey[MAX_NEW_KEY_LEN - 4];
236 int keyNum;
237 HKEY hKey;
239 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_CREATE_SUB_KEY, &hKey);
240 if (lRet != ERROR_SUCCESS) {
241 error_code_messagebox(hwnd, IDS_CREATE_KEY_FAILED);
242 goto done;
245 if (!LoadStringW(GetModuleHandleW(0), IDS_NEWKEY, newKey, COUNT_OF(newKey))) goto done;
247 /* try to find a name for the key being created (maximum = 100 attempts) */
248 for (keyNum = 1; keyNum < 100; keyNum++) {
249 wsprintfW(keyName, newKey, keyNum);
250 lRet = RegOpenKeyW(hKey, keyName, &retKey);
251 if (lRet != ERROR_SUCCESS) break;
252 RegCloseKey(retKey);
254 if (lRet == ERROR_SUCCESS) goto done;
256 lRet = RegCreateKeyW(hKey, keyName, &retKey);
257 if (lRet != ERROR_SUCCESS) {
258 error_code_messagebox(hwnd, IDS_CREATE_KEY_FAILED);
259 goto done;
262 result = TRUE;
264 done:
265 RegCloseKey(retKey);
266 return result;
269 BOOL ModifyValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR valueName)
271 BOOL result = FALSE;
272 DWORD type;
273 LONG lRet;
274 HKEY hKey;
275 LONG len;
277 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
278 if (lRet != ERROR_SUCCESS) {
279 error_code_messagebox(hwnd, IDS_SET_VALUE_FAILED);
280 return FALSE;
283 editValueName = valueName ? valueName : g_pszDefaultValueName;
284 if(!(stringValueData = read_value(hwnd, hKey, valueName, &type, &len))) goto done;
286 if ( (type == REG_SZ) || (type == REG_EXPAND_SZ) ) {
287 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_STRING), hwnd, modify_dlgproc) == IDOK) {
288 lRet = RegSetValueExW(hKey, valueName, 0, type, (LPBYTE)stringValueData, (lstrlenW(stringValueData) + 1) * sizeof(WCHAR));
289 if (lRet == ERROR_SUCCESS) result = TRUE;
290 else error_code_messagebox(hwnd, IDS_SET_VALUE_FAILED);
292 } else if ( type == REG_DWORD ) {
293 const WCHAR u[] = {'%','u',0};
294 const WCHAR x[] = {'%','x',0};
295 wsprintfW(stringValueData, isDecimal ? u : x, *((DWORD*)stringValueData));
296 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_DWORD), hwnd, modify_dlgproc) == IDOK) {
297 DWORD val;
298 CHAR* valueA = GetMultiByteString(stringValueData);
299 if (sscanf(valueA, isDecimal ? "%u" : "%x", &val)) {
300 lRet = RegSetValueExW(hKey, valueName, 0, type, (BYTE*)&val, sizeof(val));
301 if (lRet == ERROR_SUCCESS) result = TRUE;
302 else error_code_messagebox(hwnd, IDS_SET_VALUE_FAILED);
304 heap_free(valueA);
306 } else if ( type == REG_MULTI_SZ ) {
307 WCHAR char1 = '\r', char2 = '\n';
308 WCHAR *tmpValueData = NULL;
309 INT i, j, count;
311 for ( i = 0, count = 0; i < len - 1; i++)
312 if ( !stringValueData[i] && stringValueData[i + 1] )
313 count++;
314 tmpValueData = heap_xalloc((len + count) * sizeof(WCHAR));
316 for ( i = 0, j = 0; i < len - 1; i++)
318 if ( !stringValueData[i] && stringValueData[i + 1])
320 tmpValueData[j++] = char1;
321 tmpValueData[j++] = char2;
323 else
324 tmpValueData[j++] = stringValueData[i];
326 tmpValueData[j] = stringValueData[i];
327 heap_free(stringValueData);
328 stringValueData = tmpValueData;
329 tmpValueData = NULL;
331 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_MULTI_STRING), hwnd, modify_dlgproc) == IDOK)
333 len = lstrlenW( stringValueData );
334 tmpValueData = heap_xalloc((len + 2) * sizeof(WCHAR));
336 for ( i = 0, j = 0; i < len - 1; i++)
338 if ( stringValueData[i] == char1 && stringValueData[i + 1] == char2)
340 if ( tmpValueData[j - 1] != 0)
341 tmpValueData[j++] = 0;
342 i++;
344 else
345 tmpValueData[j++] = stringValueData[i];
347 tmpValueData[j++] = stringValueData[i];
348 tmpValueData[j++] = 0;
349 tmpValueData[j++] = 0;
350 heap_free(stringValueData);
351 stringValueData = tmpValueData;
353 lRet = RegSetValueExW(hKey, valueName, 0, type, (LPBYTE)stringValueData, j * sizeof(WCHAR));
354 if (lRet == ERROR_SUCCESS) result = TRUE;
355 else error_code_messagebox(hwnd, IDS_SET_VALUE_FAILED);
358 else /* hex data types */
360 struct edit_params params;
362 params.hKey = hKey;
363 params.lpszValueName = valueName;
364 params.pData = stringValueData;
365 params.cbData = len;
366 result = DialogBoxParamW(NULL, MAKEINTRESOURCEW(IDD_EDIT_BINARY), hwnd,
367 bin_modify_dlgproc, (LPARAM)&params);
370 /* Update the listview item with the new data string */
371 if (result)
373 int index = SendMessageW(g_pChildWnd->hListWnd, LVM_GETNEXTITEM, -1,
374 MAKELPARAM(LVNI_FOCUSED | LVNI_SELECTED, 0));
375 heap_free(stringValueData);
376 stringValueData = read_value(hwnd, hKey, valueName, &type, &len);
377 format_value_data(g_pChildWnd->hListWnd, index, type, stringValueData, len);
380 done:
381 heap_free(stringValueData);
382 stringValueData = NULL;
383 RegCloseKey(hKey);
384 return result;
387 BOOL DeleteKey(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath)
389 BOOL result = FALSE;
390 LONG lRet;
391 HKEY hKey;
393 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ|KEY_SET_VALUE, &hKey);
394 if (lRet != ERROR_SUCCESS) {
395 error_code_messagebox(hwnd, IDS_DELETE_KEY_FAILED);
396 return FALSE;
399 if (messagebox(hwnd, MB_YESNO | MB_ICONEXCLAMATION, IDS_DELETE_KEY_TITLE,
400 IDS_DELETE_KEY_TEXT) != IDYES)
401 goto done;
403 lRet = SHDeleteKeyW(hKeyRoot, keyPath);
404 if (lRet != ERROR_SUCCESS) {
405 error_code_messagebox(hwnd, IDS_BAD_KEY, keyPath);
406 goto done;
408 result = TRUE;
410 done:
411 RegCloseKey(hKey);
412 return result;
415 BOOL DeleteValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR valueName, BOOL showMessageBox)
417 BOOL result = FALSE;
418 LONG lRet;
419 HKEY hKey;
420 LPCWSTR visibleValueName = valueName ? valueName : g_pszDefaultValueName;
421 WCHAR empty = 0;
423 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
424 if (lRet != ERROR_SUCCESS) return FALSE;
426 if (showMessageBox)
428 if (messagebox(hwnd, MB_YESNO | MB_ICONEXCLAMATION, IDS_DELETE_VALUE_TITLE, IDS_DELETE_VALUE_TEXT,
429 visibleValueName) != IDYES)
430 goto done;
433 lRet = RegDeleteValueW(hKey, valueName ? valueName : &empty);
434 if (lRet != ERROR_SUCCESS && valueName) {
435 error_code_messagebox(hwnd, IDS_BAD_VALUE, valueName);
437 if (lRet != ERROR_SUCCESS) goto done;
438 result = TRUE;
440 done:
441 RegCloseKey(hKey);
442 return result;
445 BOOL CreateValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, DWORD valueType, LPWSTR valueName)
447 LONG lRet = ERROR_SUCCESS;
448 WCHAR newValue[256];
449 DWORD valueDword = 0;
450 BOOL result = FALSE;
451 int valueNum, index;
452 HKEY hKey;
453 LVITEMW item;
455 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
456 if (lRet != ERROR_SUCCESS) {
457 error_code_messagebox(hwnd, IDS_CREATE_VALUE_FAILED);
458 return FALSE;
461 if (!LoadStringW(GetModuleHandleW(0), IDS_NEWVALUE, newValue, COUNT_OF(newValue))) goto done;
463 /* try to find a name for the value being created (maximum = 100 attempts) */
464 for (valueNum = 1; valueNum < 100; valueNum++) {
465 wsprintfW(valueName, newValue, valueNum);
466 lRet = RegQueryValueExW(hKey, valueName, 0, 0, 0, 0);
467 if (lRet == ERROR_FILE_NOT_FOUND) break;
469 if (lRet != ERROR_FILE_NOT_FOUND) {
470 error_code_messagebox(hwnd, IDS_CREATE_VALUE_FAILED);
471 goto done;
474 lRet = RegSetValueExW(hKey, valueName, 0, valueType, (BYTE*)&valueDword, sizeof(DWORD));
475 if (lRet != ERROR_SUCCESS) {
476 error_code_messagebox(hwnd, IDS_CREATE_VALUE_FAILED);
477 goto done;
480 /* Add the new item to the listview */
481 index = AddEntryToList(g_pChildWnd->hListWnd, valueName, valueType,
482 (BYTE *)&valueDword, sizeof(DWORD), -1);
483 item.state = LVIS_FOCUSED | LVIS_SELECTED;
484 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
485 SendMessageW(g_pChildWnd->hListWnd, LVM_SETITEMSTATE, index, (LPARAM)&item);
487 result = TRUE;
489 done:
490 RegCloseKey(hKey);
491 return result;
494 BOOL RenameValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR oldName, LPCWSTR newName)
496 LPWSTR value = NULL;
497 DWORD type;
498 LONG len, lRet;
499 BOOL result = FALSE;
500 HKEY hKey;
502 if (!oldName) return FALSE;
503 if (!newName) return FALSE;
505 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
506 if (lRet != ERROR_SUCCESS) {
507 error_code_messagebox(hwnd, IDS_RENAME_VALUE_FAILED);
508 return FALSE;
510 /* check if the value already exists */
511 if (check_value(hwnd, hKey, newName)) {
512 error_code_messagebox(hwnd, IDS_VALUE_EXISTS, oldName);
513 goto done;
515 value = read_value(hwnd, hKey, oldName, &type, &len);
516 if(!value) goto done;
517 lRet = RegSetValueExW(hKey, newName, 0, type, (BYTE*)value, len);
518 if (lRet != ERROR_SUCCESS) {
519 error_code_messagebox(hwnd, IDS_RENAME_VALUE_FAILED);
520 goto done;
522 lRet = RegDeleteValueW(hKey, oldName);
523 if (lRet != ERROR_SUCCESS) {
524 RegDeleteValueW(hKey, newName);
525 error_code_messagebox(hwnd, IDS_RENAME_VALUE_FAILED);
526 goto done;
528 result = TRUE;
530 done:
531 heap_free(value);
532 RegCloseKey(hKey);
533 return result;
537 BOOL RenameKey(HWND hwnd, HKEY hRootKey, LPCWSTR keyPath, LPCWSTR newName)
539 LPWSTR parentPath = 0;
540 LPCWSTR srcSubKey = 0;
541 HKEY parentKey = 0;
542 HKEY destKey = 0;
543 BOOL result = FALSE;
544 LONG lRet;
545 DWORD disposition;
547 if (!keyPath || !newName) return FALSE;
549 if (!strrchrW(keyPath, '\\')) {
550 parentKey = hRootKey;
551 srcSubKey = keyPath;
552 } else {
553 LPWSTR srcSubKey_copy;
555 parentPath = heap_xalloc((lstrlenW(keyPath) + 1) * sizeof(WCHAR));
556 lstrcpyW(parentPath, keyPath);
557 srcSubKey_copy = strrchrW(parentPath, '\\');
558 *srcSubKey_copy = 0;
559 srcSubKey = srcSubKey_copy + 1;
560 lRet = RegOpenKeyExW(hRootKey, parentPath, 0, KEY_READ | KEY_CREATE_SUB_KEY, &parentKey);
561 if (lRet != ERROR_SUCCESS) {
562 error_code_messagebox(hwnd, IDS_RENAME_KEY_FAILED);
563 goto done;
567 /* The following fails if the old name is the same as the new name. */
568 if (!lstrcmpW(srcSubKey, newName)) goto done;
570 lRet = RegCreateKeyExW(parentKey, newName, 0, NULL, REG_OPTION_NON_VOLATILE,
571 KEY_WRITE, NULL /* FIXME */, &destKey, &disposition);
572 if (disposition == REG_OPENED_EXISTING_KEY)
573 lRet = ERROR_FILE_EXISTS;
574 if (lRet != ERROR_SUCCESS) {
575 error_code_messagebox(hwnd, IDS_KEY_EXISTS, srcSubKey);
576 goto done;
579 /* FIXME: SHCopyKey does not copy the security attributes */
580 lRet = SHCopyKeyW(parentKey, srcSubKey, destKey, 0);
581 if (lRet != ERROR_SUCCESS) {
582 RegCloseKey(destKey);
583 RegDeleteKeyW(parentKey, newName);
584 error_code_messagebox(hwnd, IDS_RENAME_KEY_FAILED);
585 goto done;
588 lRet = SHDeleteKeyW(hRootKey, keyPath);
589 if (lRet != ERROR_SUCCESS) {
590 error_code_messagebox(hwnd, IDS_RENAME_KEY_FAILED);
591 goto done;
594 result = TRUE;
596 done:
597 RegCloseKey(destKey);
598 if (parentKey) {
599 RegCloseKey(parentKey);
600 heap_free(parentPath);
602 return result;