d2d1: Remove unused D3D10 interfaces.
[wine.git] / programs / regedit / edit.c
blob813c1a1c84c9cebc7b21cffaab6bc459b985f7eb
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 "main.h"
34 static const WCHAR* editValueName;
35 static WCHAR* stringValueData;
36 static BOOL isDecimal;
38 struct edit_params
40 HKEY hkey;
41 const WCHAR *value_name;
42 DWORD type;
43 void *data;
44 DWORD size;
47 static int vmessagebox(HWND hwnd, int buttons, int titleId, int resId, __ms_va_list va_args)
49 WCHAR title[256];
50 WCHAR fmt[1024];
51 WCHAR *str;
52 int ret;
54 LoadStringW(hInst, titleId, title, ARRAY_SIZE(title));
55 LoadStringW(hInst, resId, fmt, ARRAY_SIZE(fmt));
57 FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
58 fmt, 0, 0, (WCHAR *)&str, 0, &va_args);
59 ret = MessageBoxW(hwnd, str, title, buttons);
60 LocalFree(str);
62 return ret;
65 int WINAPIV messagebox(HWND hwnd, int buttons, int titleId, int resId, ...)
67 __ms_va_list ap;
68 INT result;
70 __ms_va_start(ap, resId);
71 result = vmessagebox(hwnd, buttons, titleId, resId, ap);
72 __ms_va_end(ap);
74 return result;
77 static void WINAPIV error_code_messagebox(HWND hwnd, unsigned int msg_id, ...)
79 __ms_va_list ap;
81 __ms_va_start(ap, msg_id);
82 vmessagebox(hwnd, MB_OK|MB_ICONERROR, IDS_ERROR, msg_id, ap);
83 __ms_va_end(ap);
86 static BOOL change_dword_base(HWND hwndDlg, BOOL toHex)
88 static const WCHAR percent_u[] = {'%','u',0};
89 static const WCHAR percent_x[] = {'%','x',0};
91 WCHAR buf[128];
92 DWORD val;
94 if (!GetDlgItemTextW(hwndDlg, IDC_VALUE_DATA, buf, ARRAY_SIZE(buf))) return FALSE;
95 if (!swscanf(buf, toHex ? percent_u : percent_x, &val)) return FALSE;
96 wsprintfW(buf, toHex ? percent_x : percent_u, val);
97 return SetDlgItemTextW(hwndDlg, IDC_VALUE_DATA, buf);
100 static INT_PTR CALLBACK modify_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
102 HWND hwndValue;
103 int len;
105 switch(uMsg) {
106 case WM_INITDIALOG:
107 SetDlgItemTextW(hwndDlg, IDC_VALUE_NAME, editValueName);
108 SetDlgItemTextW(hwndDlg, IDC_VALUE_DATA, stringValueData);
109 CheckRadioButton(hwndDlg, IDC_DWORD_HEX, IDC_DWORD_DEC, IDC_DWORD_HEX);
110 isDecimal = FALSE;
111 return TRUE;
112 case WM_COMMAND:
113 switch (LOWORD(wParam)) {
114 case IDC_DWORD_HEX:
115 if (isDecimal && change_dword_base(hwndDlg, TRUE)) isDecimal = FALSE;
116 break;
117 case IDC_DWORD_DEC:
118 if (!isDecimal && change_dword_base(hwndDlg, FALSE)) isDecimal = TRUE;
119 break;
120 case IDOK:
121 if ((hwndValue = GetDlgItem(hwndDlg, IDC_VALUE_DATA))) {
122 len = GetWindowTextLengthW(hwndValue);
123 stringValueData = heap_xrealloc(stringValueData, (len + 1) * sizeof(WCHAR));
124 if (!GetWindowTextW(hwndValue, stringValueData, len + 1))
125 *stringValueData = 0;
127 /* Fall through */
128 case IDCANCEL:
129 EndDialog(hwndDlg, wParam);
130 return TRUE;
133 return FALSE;
136 static INT_PTR CALLBACK bin_modify_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
138 struct edit_params *params;
139 BYTE *data;
140 LONG size;
141 LONG lRet;
143 switch(uMsg) {
144 case WM_INITDIALOG:
145 params = (struct edit_params *)lParam;
146 SetWindowLongPtrW(hwndDlg, DWLP_USER, (ULONG_PTR)params);
147 if (params->value_name)
148 SetDlgItemTextW(hwndDlg, IDC_VALUE_NAME, params->value_name);
149 else
150 SetDlgItemTextW(hwndDlg, IDC_VALUE_NAME, g_pszDefaultValueName);
151 SendDlgItemMessageW(hwndDlg, IDC_VALUE_DATA, HEM_SETDATA, (WPARAM)params->size, (LPARAM)params->data);
152 SendDlgItemMessageW(hwndDlg, IDC_VALUE_DATA, WM_SETFONT, (WPARAM) GetStockObject(ANSI_FIXED_FONT), TRUE);
153 return TRUE;
154 case WM_COMMAND:
155 switch (LOWORD(wParam)) {
156 case IDOK:
157 params = (struct edit_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
158 size = SendDlgItemMessageW(hwndDlg, IDC_VALUE_DATA, HEM_GETDATA, 0, 0);
159 data = heap_xalloc(size);
161 SendDlgItemMessageW(hwndDlg, IDC_VALUE_DATA, HEM_GETDATA, (WPARAM)size, (LPARAM)data);
162 lRet = RegSetValueExW(params->hkey, params->value_name, 0, params->type, data, size);
163 heap_free(data);
165 if (lRet == ERROR_SUCCESS)
166 EndDialog(hwndDlg, 1);
167 else
169 error_code_messagebox(hwndDlg, IDS_SET_VALUE_FAILED);
170 EndDialog(hwndDlg, 0);
172 return TRUE;
173 case IDCANCEL:
174 EndDialog(hwndDlg, 0);
175 return TRUE;
178 return FALSE;
181 static BOOL value_exists(HWND hwnd, HKEY hKey, const WCHAR *value_name)
183 return !RegQueryValueExW(hKey, value_name, NULL, NULL, NULL, NULL);
186 static LPWSTR read_value(HWND hwnd, HKEY hKey, LPCWSTR valueName, DWORD *lpType, LONG *len)
188 DWORD valueDataLen;
189 LPWSTR buffer = NULL;
190 LONG lRet;
192 lRet = RegQueryValueExW(hKey, valueName, NULL, lpType, NULL, &valueDataLen);
193 if (lRet) {
194 if (lRet == ERROR_FILE_NOT_FOUND && !valueName) { /* no default value here, make it up */
195 if (len) *len = 1;
196 if (lpType) *lpType = REG_SZ;
197 buffer = heap_xalloc(sizeof(WCHAR));
198 *buffer = '\0';
199 return buffer;
201 error_code_messagebox(hwnd, IDS_BAD_VALUE, valueName);
202 goto done;
204 if ( *lpType == REG_DWORD ) valueDataLen = sizeof(DWORD);
205 buffer = heap_xalloc(valueDataLen + sizeof(WCHAR));
206 lRet = RegQueryValueExW(hKey, valueName, 0, 0, (LPBYTE)buffer, &valueDataLen);
207 if (lRet) {
208 error_code_messagebox(hwnd, IDS_BAD_VALUE, valueName);
209 goto done;
211 if((valueDataLen % sizeof(WCHAR)) == 0)
212 buffer[valueDataLen / sizeof(WCHAR)] = 0;
213 if(len) *len = valueDataLen;
214 return buffer;
216 done:
217 heap_free(buffer);
218 return NULL;
221 BOOL CreateKey(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPWSTR keyName)
223 BOOL result = FALSE;
224 LONG lRet = ERROR_SUCCESS;
225 HKEY retKey = NULL;
226 WCHAR newKey[MAX_NEW_KEY_LEN - 4];
227 int keyNum;
228 HKEY hKey;
230 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_CREATE_SUB_KEY, &hKey);
231 if (lRet) {
232 error_code_messagebox(hwnd, IDS_CREATE_KEY_FAILED);
233 goto done;
236 if (!LoadStringW(GetModuleHandleW(0), IDS_NEWKEY, newKey, ARRAY_SIZE(newKey))) goto done;
238 /* try to find a name for the key being created (maximum = 100 attempts) */
239 for (keyNum = 1; keyNum < 100; keyNum++) {
240 wsprintfW(keyName, newKey, keyNum);
241 lRet = RegOpenKeyW(hKey, keyName, &retKey);
242 if (lRet) break;
243 RegCloseKey(retKey);
245 if (lRet == ERROR_SUCCESS) goto done;
247 lRet = RegCreateKeyW(hKey, keyName, &retKey);
248 if (lRet) {
249 error_code_messagebox(hwnd, IDS_CREATE_KEY_FAILED);
250 goto done;
253 result = TRUE;
255 done:
256 RegCloseKey(retKey);
257 return result;
260 BOOL ModifyValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR valueName)
262 BOOL result = FALSE;
263 DWORD type;
264 LONG lRet;
265 HKEY hKey;
266 LONG len;
268 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
269 if (lRet) {
270 error_code_messagebox(hwnd, IDS_SET_VALUE_FAILED);
271 return FALSE;
274 editValueName = valueName ? valueName : g_pszDefaultValueName;
275 if(!(stringValueData = read_value(hwnd, hKey, valueName, &type, &len))) goto done;
277 if ( (type == REG_SZ) || (type == REG_EXPAND_SZ) ) {
278 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_STRING), hwnd, modify_dlgproc) == IDOK) {
279 lRet = RegSetValueExW(hKey, valueName, 0, type, (LPBYTE)stringValueData, (lstrlenW(stringValueData) + 1) * sizeof(WCHAR));
280 if (lRet == ERROR_SUCCESS) result = TRUE;
281 else error_code_messagebox(hwnd, IDS_SET_VALUE_FAILED);
283 } else if ( type == REG_DWORD ) {
284 static const WCHAR x[] = {'%','x',0};
285 DWORD value = *((DWORD*)stringValueData);
286 stringValueData = heap_xrealloc(stringValueData, 64);
287 wsprintfW(stringValueData, x, value);
288 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_DWORD), hwnd, modify_dlgproc) == IDOK) {
289 DWORD val;
290 CHAR* valueA = GetMultiByteString(stringValueData);
291 if (sscanf(valueA, isDecimal ? "%u" : "%x", &val)) {
292 lRet = RegSetValueExW(hKey, valueName, 0, type, (BYTE*)&val, sizeof(val));
293 if (lRet == ERROR_SUCCESS) result = TRUE;
294 else error_code_messagebox(hwnd, IDS_SET_VALUE_FAILED);
296 heap_free(valueA);
298 } else if ( type == REG_MULTI_SZ ) {
299 WCHAR char1 = '\r', char2 = '\n';
300 WCHAR *tmpValueData = NULL;
301 INT i, j, count;
303 for ( i = 0, count = 0; i < len - 1; i++)
304 if ( !stringValueData[i] && stringValueData[i + 1] )
305 count++;
306 tmpValueData = heap_xalloc((len + count) * sizeof(WCHAR));
308 for ( i = 0, j = 0; i < len - 1; i++)
310 if ( !stringValueData[i] && stringValueData[i + 1])
312 tmpValueData[j++] = char1;
313 tmpValueData[j++] = char2;
315 else
316 tmpValueData[j++] = stringValueData[i];
318 tmpValueData[j] = stringValueData[i];
319 heap_free(stringValueData);
320 stringValueData = tmpValueData;
321 tmpValueData = NULL;
323 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_MULTI_STRING), hwnd, modify_dlgproc) == IDOK)
325 len = lstrlenW( stringValueData );
326 tmpValueData = heap_xalloc((len + 2) * sizeof(WCHAR));
328 for ( i = 0, j = 0; i < len - 1; i++)
330 if ( stringValueData[i] == char1 && stringValueData[i + 1] == char2)
332 if ( tmpValueData[j - 1] != 0)
333 tmpValueData[j++] = 0;
334 i++;
336 else
337 tmpValueData[j++] = stringValueData[i];
339 tmpValueData[j++] = stringValueData[i];
340 tmpValueData[j++] = 0;
341 tmpValueData[j++] = 0;
342 heap_free(stringValueData);
343 stringValueData = tmpValueData;
345 lRet = RegSetValueExW(hKey, valueName, 0, type, (LPBYTE)stringValueData, j * sizeof(WCHAR));
346 if (lRet == ERROR_SUCCESS) result = TRUE;
347 else error_code_messagebox(hwnd, IDS_SET_VALUE_FAILED);
350 else /* hex data types */
352 struct edit_params params;
354 params.hkey = hKey;
355 params.value_name = valueName;
356 params.type = type;
357 params.data = stringValueData;
358 params.size = len;
359 result = DialogBoxParamW(NULL, MAKEINTRESOURCEW(IDD_EDIT_BINARY), hwnd,
360 bin_modify_dlgproc, (LPARAM)&params);
363 /* Update the listview item with the new data string */
364 if (result)
366 int index = SendMessageW(g_pChildWnd->hListWnd, LVM_GETNEXTITEM, -1,
367 MAKELPARAM(LVNI_FOCUSED | LVNI_SELECTED, 0));
368 heap_free(stringValueData);
369 stringValueData = read_value(hwnd, hKey, valueName, &type, &len);
370 format_value_data(g_pChildWnd->hListWnd, index, type, stringValueData, len);
373 done:
374 heap_free(stringValueData);
375 stringValueData = NULL;
376 RegCloseKey(hKey);
377 return result;
380 BOOL DeleteKey(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath)
382 BOOL result = FALSE;
383 LONG lRet;
384 HKEY hKey;
386 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ|KEY_SET_VALUE, &hKey);
387 if (lRet) {
388 error_code_messagebox(hwnd, IDS_DELETE_KEY_FAILED);
389 return FALSE;
392 if (messagebox(hwnd, MB_YESNO | MB_ICONEXCLAMATION, IDS_DELETE_KEY_TITLE,
393 IDS_DELETE_KEY_TEXT) != IDYES)
394 goto done;
396 lRet = SHDeleteKeyW(hKeyRoot, keyPath);
397 if (lRet) {
398 error_code_messagebox(hwnd, IDS_BAD_KEY, keyPath);
399 goto done;
401 result = TRUE;
403 done:
404 RegCloseKey(hKey);
405 return result;
408 BOOL DeleteValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR valueName)
410 BOOL result = FALSE;
411 LONG lRet;
412 HKEY hKey;
414 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
415 if (lRet) return FALSE;
417 lRet = RegDeleteValueW(hKey, valueName);
418 if (lRet && valueName) {
419 error_code_messagebox(hwnd, IDS_BAD_VALUE, valueName);
421 if (lRet) goto done;
422 result = TRUE;
424 done:
425 RegCloseKey(hKey);
426 return result;
429 BOOL CreateValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, DWORD valueType, LPWSTR valueName)
431 LONG lRet = ERROR_SUCCESS;
432 WCHAR newValue[256];
433 DWORD valueDword = 0;
434 BOOL result = FALSE;
435 int valueNum, index;
436 HKEY hKey;
437 LVITEMW item;
439 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
440 if (lRet) {
441 error_code_messagebox(hwnd, IDS_CREATE_VALUE_FAILED);
442 return FALSE;
445 if (!LoadStringW(GetModuleHandleW(0), IDS_NEWVALUE, newValue, ARRAY_SIZE(newValue))) goto done;
447 /* try to find a name for the value being created (maximum = 100 attempts) */
448 for (valueNum = 1; valueNum < 100; valueNum++) {
449 wsprintfW(valueName, newValue, valueNum);
450 lRet = RegQueryValueExW(hKey, valueName, 0, 0, 0, 0);
451 if (lRet == ERROR_FILE_NOT_FOUND) break;
453 if (lRet != ERROR_FILE_NOT_FOUND) {
454 error_code_messagebox(hwnd, IDS_CREATE_VALUE_FAILED);
455 goto done;
458 lRet = RegSetValueExW(hKey, valueName, 0, valueType, (BYTE*)&valueDword, sizeof(DWORD));
459 if (lRet) {
460 error_code_messagebox(hwnd, IDS_CREATE_VALUE_FAILED);
461 goto done;
464 /* Add the new item to the listview */
465 index = AddEntryToList(g_pChildWnd->hListWnd, valueName, valueType,
466 (BYTE *)&valueDword, sizeof(DWORD), -1);
467 item.state = LVIS_FOCUSED | LVIS_SELECTED;
468 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
469 SendMessageW(g_pChildWnd->hListWnd, LVM_SETITEMSTATE, index, (LPARAM)&item);
471 result = TRUE;
473 done:
474 RegCloseKey(hKey);
475 return result;
478 BOOL RenameValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR oldName, LPCWSTR newName)
480 LPWSTR value = NULL;
481 DWORD type;
482 LONG len, lRet;
483 BOOL result = FALSE;
484 HKEY hKey;
486 if (!oldName) return FALSE;
487 if (!newName) return FALSE;
489 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
490 if (lRet) {
491 error_code_messagebox(hwnd, IDS_RENAME_VALUE_FAILED);
492 return FALSE;
495 if (value_exists(hwnd, hKey, newName)) {
496 error_code_messagebox(hwnd, IDS_VALUE_EXISTS, oldName);
497 goto done;
499 value = read_value(hwnd, hKey, oldName, &type, &len);
500 if(!value) goto done;
501 lRet = RegSetValueExW(hKey, newName, 0, type, (BYTE*)value, len);
502 if (lRet) {
503 error_code_messagebox(hwnd, IDS_RENAME_VALUE_FAILED);
504 goto done;
506 lRet = RegDeleteValueW(hKey, oldName);
507 if (lRet) {
508 RegDeleteValueW(hKey, newName);
509 error_code_messagebox(hwnd, IDS_RENAME_VALUE_FAILED);
510 goto done;
512 result = TRUE;
514 done:
515 heap_free(value);
516 RegCloseKey(hKey);
517 return result;
521 BOOL RenameKey(HWND hwnd, HKEY hRootKey, LPCWSTR keyPath, LPCWSTR newName)
523 LPWSTR parentPath = 0;
524 LPCWSTR srcSubKey = 0;
525 HKEY parentKey = 0;
526 HKEY destKey = 0;
527 BOOL result = FALSE;
528 LONG lRet;
529 DWORD disposition;
531 if (!keyPath || !newName) return FALSE;
533 if (!wcsrchr(keyPath, '\\')) {
534 parentKey = hRootKey;
535 srcSubKey = keyPath;
536 } else {
537 LPWSTR srcSubKey_copy;
539 parentPath = heap_xalloc((lstrlenW(keyPath) + 1) * sizeof(WCHAR));
540 lstrcpyW(parentPath, keyPath);
541 srcSubKey_copy = wcsrchr(parentPath, '\\');
542 *srcSubKey_copy = 0;
543 srcSubKey = srcSubKey_copy + 1;
544 lRet = RegOpenKeyExW(hRootKey, parentPath, 0, KEY_READ | KEY_CREATE_SUB_KEY, &parentKey);
545 if (lRet) {
546 error_code_messagebox(hwnd, IDS_RENAME_KEY_FAILED);
547 goto done;
551 /* The following fails if the old name is the same as the new name. */
552 if (!lstrcmpW(srcSubKey, newName)) goto done;
554 lRet = RegCreateKeyExW(parentKey, newName, 0, NULL, REG_OPTION_NON_VOLATILE,
555 KEY_WRITE, NULL /* FIXME */, &destKey, &disposition);
556 if (disposition == REG_OPENED_EXISTING_KEY)
557 lRet = ERROR_FILE_EXISTS;
558 if (lRet) {
559 error_code_messagebox(hwnd, IDS_KEY_EXISTS, srcSubKey);
560 goto done;
563 /* FIXME: SHCopyKey does not copy the security attributes */
564 lRet = SHCopyKeyW(parentKey, srcSubKey, destKey, 0);
565 if (lRet) {
566 RegCloseKey(destKey);
567 RegDeleteKeyW(parentKey, newName);
568 error_code_messagebox(hwnd, IDS_RENAME_KEY_FAILED);
569 goto done;
572 lRet = SHDeleteKeyW(hRootKey, keyPath);
573 if (lRet) {
574 error_code_messagebox(hwnd, IDS_RENAME_KEY_FAILED);
575 goto done;
578 result = TRUE;
580 done:
581 RegCloseKey(destKey);
582 if (parentKey) {
583 RegCloseKey(parentKey);
584 heap_free(parentPath);
586 return result;