hidclass.sys: Use IoRegisterDeviceInterface.
[wine.git] / programs / regedit / edit.c
bloba3cf17b993a75be11b3ed05148db2abbcc1557de
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 <shellapi.h>
29 #include <shlwapi.h>
31 #include "wine/heap.h"
32 #include "wine/unicode.h"
33 #include "main.h"
35 static const WCHAR* editValueName;
36 static WCHAR* stringValueData;
37 static BOOL isDecimal;
39 struct edit_params
41 HKEY hkey;
42 const WCHAR *value_name;
43 DWORD type;
44 void *data;
45 DWORD size;
48 static int vmessagebox(HWND hwnd, int buttons, int titleId, int resId, __ms_va_list va_args)
50 WCHAR title[256];
51 WCHAR fmt[1024];
52 WCHAR *str;
53 int ret;
55 LoadStringW(hInst, titleId, title, ARRAY_SIZE(title));
56 LoadStringW(hInst, resId, fmt, ARRAY_SIZE(fmt));
58 FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
59 fmt, 0, 0, (WCHAR *)&str, 0, &va_args);
60 ret = MessageBoxW(hwnd, str, title, buttons);
61 LocalFree(str);
63 return ret;
66 int WINAPIV messagebox(HWND hwnd, int buttons, int titleId, int resId, ...)
68 __ms_va_list ap;
69 INT result;
71 __ms_va_start(ap, resId);
72 result = vmessagebox(hwnd, buttons, titleId, resId, ap);
73 __ms_va_end(ap);
75 return result;
78 static void WINAPIV error_code_messagebox(HWND hwnd, unsigned int msg_id, ...)
80 __ms_va_list ap;
82 __ms_va_start(ap, msg_id);
83 vmessagebox(hwnd, MB_OK|MB_ICONERROR, IDS_ERROR, msg_id, ap);
84 __ms_va_end(ap);
87 static BOOL change_dword_base(HWND hwndDlg, BOOL toHex)
89 static const WCHAR percent_u[] = {'%','u',0};
90 static const WCHAR percent_x[] = {'%','x',0};
92 WCHAR buf[128];
93 DWORD val;
95 if (!GetDlgItemTextW(hwndDlg, IDC_VALUE_DATA, buf, ARRAY_SIZE(buf))) return FALSE;
96 if (!swscanf(buf, toHex ? percent_u : percent_x, &val)) return FALSE;
97 wsprintfW(buf, toHex ? percent_x : percent_u, val);
98 return SetDlgItemTextW(hwndDlg, IDC_VALUE_DATA, buf);
101 static INT_PTR CALLBACK modify_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
103 HWND hwndValue;
104 int len;
106 switch(uMsg) {
107 case WM_INITDIALOG:
108 SetDlgItemTextW(hwndDlg, IDC_VALUE_NAME, editValueName);
109 SetDlgItemTextW(hwndDlg, IDC_VALUE_DATA, stringValueData);
110 CheckRadioButton(hwndDlg, IDC_DWORD_HEX, IDC_DWORD_DEC, IDC_DWORD_HEX);
111 isDecimal = FALSE;
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 BYTE *data;
141 LONG size;
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->value_name)
149 SetDlgItemTextW(hwndDlg, IDC_VALUE_NAME, params->value_name);
150 else
151 SetDlgItemTextW(hwndDlg, IDC_VALUE_NAME, g_pszDefaultValueName);
152 SendDlgItemMessageW(hwndDlg, IDC_VALUE_DATA, HEM_SETDATA, (WPARAM)params->size, (LPARAM)params->data);
153 SendDlgItemMessageW(hwndDlg, IDC_VALUE_DATA, WM_SETFONT, (WPARAM) GetStockObject(ANSI_FIXED_FONT), TRUE);
154 return TRUE;
155 case WM_COMMAND:
156 switch (LOWORD(wParam)) {
157 case IDOK:
158 params = (struct edit_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
159 size = SendDlgItemMessageW(hwndDlg, IDC_VALUE_DATA, HEM_GETDATA, 0, 0);
160 data = heap_xalloc(size);
162 SendDlgItemMessageW(hwndDlg, IDC_VALUE_DATA, HEM_GETDATA, (WPARAM)size, (LPARAM)data);
163 lRet = RegSetValueExW(params->hkey, params->value_name, 0, params->type, data, size);
164 heap_free(data);
166 if (lRet == ERROR_SUCCESS)
167 EndDialog(hwndDlg, 1);
168 else
170 error_code_messagebox(hwndDlg, IDS_SET_VALUE_FAILED);
171 EndDialog(hwndDlg, 0);
173 return TRUE;
174 case IDCANCEL:
175 EndDialog(hwndDlg, 0);
176 return TRUE;
179 return FALSE;
182 static BOOL value_exists(HWND hwnd, HKEY hKey, const WCHAR *value_name)
184 return !RegQueryValueExW(hKey, value_name, NULL, NULL, NULL, NULL);
187 static LPWSTR read_value(HWND hwnd, HKEY hKey, LPCWSTR valueName, DWORD *lpType, LONG *len)
189 DWORD valueDataLen;
190 LPWSTR buffer = NULL;
191 LONG lRet;
193 lRet = RegQueryValueExW(hKey, valueName, NULL, lpType, NULL, &valueDataLen);
194 if (lRet) {
195 if (lRet == ERROR_FILE_NOT_FOUND && !valueName) { /* no default value here, make it up */
196 if (len) *len = 1;
197 if (lpType) *lpType = REG_SZ;
198 buffer = heap_xalloc(sizeof(WCHAR));
199 *buffer = '\0';
200 return buffer;
202 error_code_messagebox(hwnd, IDS_BAD_VALUE, valueName);
203 goto done;
205 if ( *lpType == REG_DWORD ) valueDataLen = sizeof(DWORD);
206 buffer = heap_xalloc(valueDataLen + sizeof(WCHAR));
207 lRet = RegQueryValueExW(hKey, valueName, 0, 0, (LPBYTE)buffer, &valueDataLen);
208 if (lRet) {
209 error_code_messagebox(hwnd, IDS_BAD_VALUE, valueName);
210 goto done;
212 if((valueDataLen % sizeof(WCHAR)) == 0)
213 buffer[valueDataLen / sizeof(WCHAR)] = 0;
214 if(len) *len = valueDataLen;
215 return buffer;
217 done:
218 heap_free(buffer);
219 return NULL;
222 BOOL CreateKey(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPWSTR keyName)
224 BOOL result = FALSE;
225 LONG lRet = ERROR_SUCCESS;
226 HKEY retKey = NULL;
227 WCHAR newKey[MAX_NEW_KEY_LEN - 4];
228 int keyNum;
229 HKEY hKey;
231 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_CREATE_SUB_KEY, &hKey);
232 if (lRet) {
233 error_code_messagebox(hwnd, IDS_CREATE_KEY_FAILED);
234 goto done;
237 if (!LoadStringW(GetModuleHandleW(0), IDS_NEWKEY, newKey, ARRAY_SIZE(newKey))) goto done;
239 /* try to find a name for the key being created (maximum = 100 attempts) */
240 for (keyNum = 1; keyNum < 100; keyNum++) {
241 wsprintfW(keyName, newKey, keyNum);
242 lRet = RegOpenKeyW(hKey, keyName, &retKey);
243 if (lRet) break;
244 RegCloseKey(retKey);
246 if (lRet == ERROR_SUCCESS) goto done;
248 lRet = RegCreateKeyW(hKey, keyName, &retKey);
249 if (lRet) {
250 error_code_messagebox(hwnd, IDS_CREATE_KEY_FAILED);
251 goto done;
254 result = TRUE;
256 done:
257 RegCloseKey(retKey);
258 return result;
261 BOOL ModifyValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR valueName)
263 BOOL result = FALSE;
264 DWORD type;
265 LONG lRet;
266 HKEY hKey;
267 LONG len;
269 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
270 if (lRet) {
271 error_code_messagebox(hwnd, IDS_SET_VALUE_FAILED);
272 return FALSE;
275 editValueName = valueName ? valueName : g_pszDefaultValueName;
276 if(!(stringValueData = read_value(hwnd, hKey, valueName, &type, &len))) goto done;
278 if ( (type == REG_SZ) || (type == REG_EXPAND_SZ) ) {
279 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_STRING), hwnd, modify_dlgproc) == IDOK) {
280 lRet = RegSetValueExW(hKey, valueName, 0, type, (LPBYTE)stringValueData, (lstrlenW(stringValueData) + 1) * sizeof(WCHAR));
281 if (lRet == ERROR_SUCCESS) result = TRUE;
282 else error_code_messagebox(hwnd, IDS_SET_VALUE_FAILED);
284 } else if ( type == REG_DWORD ) {
285 static const WCHAR x[] = {'%','x',0};
286 wsprintfW(stringValueData, x, *((DWORD*)stringValueData));
287 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_DWORD), hwnd, modify_dlgproc) == IDOK) {
288 DWORD val;
289 CHAR* valueA = GetMultiByteString(stringValueData);
290 if (sscanf(valueA, isDecimal ? "%u" : "%x", &val)) {
291 lRet = RegSetValueExW(hKey, valueName, 0, type, (BYTE*)&val, sizeof(val));
292 if (lRet == ERROR_SUCCESS) result = TRUE;
293 else error_code_messagebox(hwnd, IDS_SET_VALUE_FAILED);
295 heap_free(valueA);
297 } else if ( type == REG_MULTI_SZ ) {
298 WCHAR char1 = '\r', char2 = '\n';
299 WCHAR *tmpValueData = NULL;
300 INT i, j, count;
302 for ( i = 0, count = 0; i < len - 1; i++)
303 if ( !stringValueData[i] && stringValueData[i + 1] )
304 count++;
305 tmpValueData = heap_xalloc((len + count) * sizeof(WCHAR));
307 for ( i = 0, j = 0; i < len - 1; i++)
309 if ( !stringValueData[i] && stringValueData[i + 1])
311 tmpValueData[j++] = char1;
312 tmpValueData[j++] = char2;
314 else
315 tmpValueData[j++] = stringValueData[i];
317 tmpValueData[j] = stringValueData[i];
318 heap_free(stringValueData);
319 stringValueData = tmpValueData;
320 tmpValueData = NULL;
322 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_MULTI_STRING), hwnd, modify_dlgproc) == IDOK)
324 len = lstrlenW( stringValueData );
325 tmpValueData = heap_xalloc((len + 2) * sizeof(WCHAR));
327 for ( i = 0, j = 0; i < len - 1; i++)
329 if ( stringValueData[i] == char1 && stringValueData[i + 1] == char2)
331 if ( tmpValueData[j - 1] != 0)
332 tmpValueData[j++] = 0;
333 i++;
335 else
336 tmpValueData[j++] = stringValueData[i];
338 tmpValueData[j++] = stringValueData[i];
339 tmpValueData[j++] = 0;
340 tmpValueData[j++] = 0;
341 heap_free(stringValueData);
342 stringValueData = tmpValueData;
344 lRet = RegSetValueExW(hKey, valueName, 0, type, (LPBYTE)stringValueData, j * sizeof(WCHAR));
345 if (lRet == ERROR_SUCCESS) result = TRUE;
346 else error_code_messagebox(hwnd, IDS_SET_VALUE_FAILED);
349 else /* hex data types */
351 struct edit_params params;
353 params.hkey = hKey;
354 params.value_name = valueName;
355 params.type = type;
356 params.data = stringValueData;
357 params.size = len;
358 result = DialogBoxParamW(NULL, MAKEINTRESOURCEW(IDD_EDIT_BINARY), hwnd,
359 bin_modify_dlgproc, (LPARAM)&params);
362 /* Update the listview item with the new data string */
363 if (result)
365 int index = SendMessageW(g_pChildWnd->hListWnd, LVM_GETNEXTITEM, -1,
366 MAKELPARAM(LVNI_FOCUSED | LVNI_SELECTED, 0));
367 heap_free(stringValueData);
368 stringValueData = read_value(hwnd, hKey, valueName, &type, &len);
369 format_value_data(g_pChildWnd->hListWnd, index, type, stringValueData, len);
372 done:
373 heap_free(stringValueData);
374 stringValueData = NULL;
375 RegCloseKey(hKey);
376 return result;
379 BOOL DeleteKey(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath)
381 BOOL result = FALSE;
382 LONG lRet;
383 HKEY hKey;
385 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ|KEY_SET_VALUE, &hKey);
386 if (lRet) {
387 error_code_messagebox(hwnd, IDS_DELETE_KEY_FAILED);
388 return FALSE;
391 if (messagebox(hwnd, MB_YESNO | MB_ICONEXCLAMATION, IDS_DELETE_KEY_TITLE,
392 IDS_DELETE_KEY_TEXT) != IDYES)
393 goto done;
395 lRet = SHDeleteKeyW(hKeyRoot, keyPath);
396 if (lRet) {
397 error_code_messagebox(hwnd, IDS_BAD_KEY, keyPath);
398 goto done;
400 result = TRUE;
402 done:
403 RegCloseKey(hKey);
404 return result;
407 BOOL DeleteValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR valueName)
409 BOOL result = FALSE;
410 LONG lRet;
411 HKEY hKey;
413 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
414 if (lRet) return FALSE;
416 lRet = RegDeleteValueW(hKey, valueName);
417 if (lRet && valueName) {
418 error_code_messagebox(hwnd, IDS_BAD_VALUE, valueName);
420 if (lRet) goto done;
421 result = TRUE;
423 done:
424 RegCloseKey(hKey);
425 return result;
428 BOOL CreateValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, DWORD valueType, LPWSTR valueName)
430 LONG lRet = ERROR_SUCCESS;
431 WCHAR newValue[256];
432 DWORD valueDword = 0;
433 BOOL result = FALSE;
434 int valueNum, index;
435 HKEY hKey;
436 LVITEMW item;
438 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
439 if (lRet) {
440 error_code_messagebox(hwnd, IDS_CREATE_VALUE_FAILED);
441 return FALSE;
444 if (!LoadStringW(GetModuleHandleW(0), IDS_NEWVALUE, newValue, ARRAY_SIZE(newValue))) goto done;
446 /* try to find a name for the value being created (maximum = 100 attempts) */
447 for (valueNum = 1; valueNum < 100; valueNum++) {
448 wsprintfW(valueName, newValue, valueNum);
449 lRet = RegQueryValueExW(hKey, valueName, 0, 0, 0, 0);
450 if (lRet == ERROR_FILE_NOT_FOUND) break;
452 if (lRet != ERROR_FILE_NOT_FOUND) {
453 error_code_messagebox(hwnd, IDS_CREATE_VALUE_FAILED);
454 goto done;
457 lRet = RegSetValueExW(hKey, valueName, 0, valueType, (BYTE*)&valueDword, sizeof(DWORD));
458 if (lRet) {
459 error_code_messagebox(hwnd, IDS_CREATE_VALUE_FAILED);
460 goto done;
463 /* Add the new item to the listview */
464 index = AddEntryToList(g_pChildWnd->hListWnd, valueName, valueType,
465 (BYTE *)&valueDword, sizeof(DWORD), -1);
466 item.state = LVIS_FOCUSED | LVIS_SELECTED;
467 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
468 SendMessageW(g_pChildWnd->hListWnd, LVM_SETITEMSTATE, index, (LPARAM)&item);
470 result = TRUE;
472 done:
473 RegCloseKey(hKey);
474 return result;
477 BOOL RenameValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR oldName, LPCWSTR newName)
479 LPWSTR value = NULL;
480 DWORD type;
481 LONG len, lRet;
482 BOOL result = FALSE;
483 HKEY hKey;
485 if (!oldName) return FALSE;
486 if (!newName) return FALSE;
488 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
489 if (lRet) {
490 error_code_messagebox(hwnd, IDS_RENAME_VALUE_FAILED);
491 return FALSE;
494 if (value_exists(hwnd, hKey, newName)) {
495 error_code_messagebox(hwnd, IDS_VALUE_EXISTS, oldName);
496 goto done;
498 value = read_value(hwnd, hKey, oldName, &type, &len);
499 if(!value) goto done;
500 lRet = RegSetValueExW(hKey, newName, 0, type, (BYTE*)value, len);
501 if (lRet) {
502 error_code_messagebox(hwnd, IDS_RENAME_VALUE_FAILED);
503 goto done;
505 lRet = RegDeleteValueW(hKey, oldName);
506 if (lRet) {
507 RegDeleteValueW(hKey, newName);
508 error_code_messagebox(hwnd, IDS_RENAME_VALUE_FAILED);
509 goto done;
511 result = TRUE;
513 done:
514 heap_free(value);
515 RegCloseKey(hKey);
516 return result;
520 BOOL RenameKey(HWND hwnd, HKEY hRootKey, LPCWSTR keyPath, LPCWSTR newName)
522 LPWSTR parentPath = 0;
523 LPCWSTR srcSubKey = 0;
524 HKEY parentKey = 0;
525 HKEY destKey = 0;
526 BOOL result = FALSE;
527 LONG lRet;
528 DWORD disposition;
530 if (!keyPath || !newName) return FALSE;
532 if (!strrchrW(keyPath, '\\')) {
533 parentKey = hRootKey;
534 srcSubKey = keyPath;
535 } else {
536 LPWSTR srcSubKey_copy;
538 parentPath = heap_xalloc((lstrlenW(keyPath) + 1) * sizeof(WCHAR));
539 lstrcpyW(parentPath, keyPath);
540 srcSubKey_copy = strrchrW(parentPath, '\\');
541 *srcSubKey_copy = 0;
542 srcSubKey = srcSubKey_copy + 1;
543 lRet = RegOpenKeyExW(hRootKey, parentPath, 0, KEY_READ | KEY_CREATE_SUB_KEY, &parentKey);
544 if (lRet) {
545 error_code_messagebox(hwnd, IDS_RENAME_KEY_FAILED);
546 goto done;
550 /* The following fails if the old name is the same as the new name. */
551 if (!lstrcmpW(srcSubKey, newName)) goto done;
553 lRet = RegCreateKeyExW(parentKey, newName, 0, NULL, REG_OPTION_NON_VOLATILE,
554 KEY_WRITE, NULL /* FIXME */, &destKey, &disposition);
555 if (disposition == REG_OPENED_EXISTING_KEY)
556 lRet = ERROR_FILE_EXISTS;
557 if (lRet) {
558 error_code_messagebox(hwnd, IDS_KEY_EXISTS, srcSubKey);
559 goto done;
562 /* FIXME: SHCopyKey does not copy the security attributes */
563 lRet = SHCopyKeyW(parentKey, srcSubKey, destKey, 0);
564 if (lRet) {
565 RegCloseKey(destKey);
566 RegDeleteKeyW(parentKey, newName);
567 error_code_messagebox(hwnd, IDS_RENAME_KEY_FAILED);
568 goto done;
571 lRet = SHDeleteKeyW(hRootKey, keyPath);
572 if (lRet) {
573 error_code_messagebox(hwnd, IDS_RENAME_KEY_FAILED);
574 goto done;
577 result = TRUE;
579 done:
580 RegCloseKey(destKey);
581 if (parentKey) {
582 RegCloseKey(parentKey);
583 heap_free(parentPath);
585 return result;