user32: Update the window DPI awareness in SetParent().
[wine.git] / programs / regedit / edit.c
blobc982791b35cdb8ac19fb0556d914808a89a094dd
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 SendDlgItemMessageW(hwndDlg, IDC_VALUE_DATA, WM_SETFONT, (WPARAM) GetStockObject(ANSI_FIXED_FONT), TRUE);
155 return TRUE;
156 case WM_COMMAND:
157 switch (LOWORD(wParam)) {
158 case IDOK:
159 params = (struct edit_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
160 cbData = SendDlgItemMessageW(hwndDlg, IDC_VALUE_DATA, HEM_GETDATA, 0, 0);
161 pData = heap_xalloc(cbData);
163 if (pData)
165 SendDlgItemMessageW(hwndDlg, IDC_VALUE_DATA, HEM_GETDATA, (WPARAM)cbData, (LPARAM)pData);
166 lRet = RegSetValueExW(params->hKey, params->lpszValueName, 0, REG_BINARY, pData, cbData);
167 heap_free(pData);
169 else
170 lRet = ERROR_OUTOFMEMORY;
172 if (lRet == ERROR_SUCCESS)
173 EndDialog(hwndDlg, 1);
174 else
176 error_code_messagebox(hwndDlg, IDS_SET_VALUE_FAILED);
177 EndDialog(hwndDlg, 0);
179 return TRUE;
180 case IDCANCEL:
181 EndDialog(hwndDlg, 0);
182 return TRUE;
185 return FALSE;
188 static BOOL check_value(HWND hwnd, HKEY hKey, LPCWSTR valueName)
190 WCHAR empty = 0;
191 LONG lRet = RegQueryValueExW(hKey, valueName ? valueName : &empty, 0, NULL, 0, NULL);
192 if(lRet != ERROR_SUCCESS) return FALSE;
193 return TRUE;
196 static LPWSTR read_value(HWND hwnd, HKEY hKey, LPCWSTR valueName, DWORD *lpType, LONG *len)
198 DWORD valueDataLen;
199 LPWSTR buffer = NULL;
200 LONG lRet;
201 WCHAR empty = 0;
203 lRet = RegQueryValueExW(hKey, valueName ? valueName : &empty, 0, lpType, 0, &valueDataLen);
204 if (lRet != ERROR_SUCCESS) {
205 if (lRet == ERROR_FILE_NOT_FOUND && !valueName) { /* no default value here, make it up */
206 if (len) *len = 1;
207 if (lpType) *lpType = REG_SZ;
208 buffer = heap_xalloc(sizeof(WCHAR));
209 *buffer = '\0';
210 return buffer;
212 error_code_messagebox(hwnd, IDS_BAD_VALUE, valueName);
213 goto done;
215 if ( *lpType == REG_DWORD ) valueDataLen = sizeof(DWORD);
216 buffer = heap_xalloc(valueDataLen + sizeof(WCHAR));
217 lRet = RegQueryValueExW(hKey, valueName, 0, 0, (LPBYTE)buffer, &valueDataLen);
218 if (lRet != ERROR_SUCCESS) {
219 error_code_messagebox(hwnd, IDS_BAD_VALUE, valueName);
220 goto done;
222 if((valueDataLen % sizeof(WCHAR)) == 0)
223 buffer[valueDataLen / sizeof(WCHAR)] = 0;
224 if(len) *len = valueDataLen;
225 return buffer;
227 done:
228 heap_free(buffer);
229 return NULL;
232 BOOL CreateKey(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPWSTR keyName)
234 BOOL result = FALSE;
235 LONG lRet = ERROR_SUCCESS;
236 HKEY retKey = NULL;
237 WCHAR newKey[MAX_NEW_KEY_LEN - 4];
238 int keyNum;
239 HKEY hKey;
241 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_CREATE_SUB_KEY, &hKey);
242 if (lRet != ERROR_SUCCESS) {
243 error_code_messagebox(hwnd, IDS_CREATE_KEY_FAILED);
244 goto done;
247 if (!LoadStringW(GetModuleHandleW(0), IDS_NEWKEY, newKey, COUNT_OF(newKey))) goto done;
249 /* try to find a name for the key being created (maximum = 100 attempts) */
250 for (keyNum = 1; keyNum < 100; keyNum++) {
251 wsprintfW(keyName, newKey, keyNum);
252 lRet = RegOpenKeyW(hKey, keyName, &retKey);
253 if (lRet != ERROR_SUCCESS) break;
254 RegCloseKey(retKey);
256 if (lRet == ERROR_SUCCESS) goto done;
258 lRet = RegCreateKeyW(hKey, keyName, &retKey);
259 if (lRet != ERROR_SUCCESS) {
260 error_code_messagebox(hwnd, IDS_CREATE_KEY_FAILED);
261 goto done;
264 result = TRUE;
266 done:
267 RegCloseKey(retKey);
268 return result;
271 BOOL ModifyValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR valueName)
273 BOOL result = FALSE;
274 DWORD type;
275 LONG lRet;
276 HKEY hKey;
277 LONG len;
279 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
280 if (lRet != ERROR_SUCCESS) {
281 error_code_messagebox(hwnd, IDS_SET_VALUE_FAILED);
282 return FALSE;
285 editValueName = valueName ? valueName : g_pszDefaultValueName;
286 if(!(stringValueData = read_value(hwnd, hKey, valueName, &type, &len))) goto done;
288 if ( (type == REG_SZ) || (type == REG_EXPAND_SZ) ) {
289 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_STRING), hwnd, modify_dlgproc) == IDOK) {
290 lRet = RegSetValueExW(hKey, valueName, 0, type, (LPBYTE)stringValueData, (lstrlenW(stringValueData) + 1) * sizeof(WCHAR));
291 if (lRet == ERROR_SUCCESS) result = TRUE;
292 else error_code_messagebox(hwnd, IDS_SET_VALUE_FAILED);
294 } else if ( type == REG_DWORD ) {
295 const WCHAR u[] = {'%','u',0};
296 const WCHAR x[] = {'%','x',0};
297 wsprintfW(stringValueData, isDecimal ? u : x, *((DWORD*)stringValueData));
298 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_DWORD), hwnd, modify_dlgproc) == IDOK) {
299 DWORD val;
300 CHAR* valueA = GetMultiByteString(stringValueData);
301 if (sscanf(valueA, isDecimal ? "%u" : "%x", &val)) {
302 lRet = RegSetValueExW(hKey, valueName, 0, type, (BYTE*)&val, sizeof(val));
303 if (lRet == ERROR_SUCCESS) result = TRUE;
304 else error_code_messagebox(hwnd, IDS_SET_VALUE_FAILED);
306 heap_free(valueA);
308 } else if ( type == REG_MULTI_SZ ) {
309 WCHAR char1 = '\r', char2 = '\n';
310 WCHAR *tmpValueData = NULL;
311 INT i, j, count;
313 for ( i = 0, count = 0; i < len - 1; i++)
314 if ( !stringValueData[i] && stringValueData[i + 1] )
315 count++;
316 tmpValueData = heap_xalloc((len + count) * sizeof(WCHAR));
318 for ( i = 0, j = 0; i < len - 1; i++)
320 if ( !stringValueData[i] && stringValueData[i + 1])
322 tmpValueData[j++] = char1;
323 tmpValueData[j++] = char2;
325 else
326 tmpValueData[j++] = stringValueData[i];
328 tmpValueData[j] = stringValueData[i];
329 heap_free(stringValueData);
330 stringValueData = tmpValueData;
331 tmpValueData = NULL;
333 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_MULTI_STRING), hwnd, modify_dlgproc) == IDOK)
335 len = lstrlenW( stringValueData );
336 tmpValueData = heap_xalloc((len + 2) * sizeof(WCHAR));
338 for ( i = 0, j = 0; i < len - 1; i++)
340 if ( stringValueData[i] == char1 && stringValueData[i + 1] == char2)
342 if ( tmpValueData[j - 1] != 0)
343 tmpValueData[j++] = 0;
344 i++;
346 else
347 tmpValueData[j++] = stringValueData[i];
349 tmpValueData[j++] = stringValueData[i];
350 tmpValueData[j++] = 0;
351 tmpValueData[j++] = 0;
352 heap_free(stringValueData);
353 stringValueData = tmpValueData;
355 lRet = RegSetValueExW(hKey, valueName, 0, type, (LPBYTE)stringValueData, j * sizeof(WCHAR));
356 if (lRet == ERROR_SUCCESS) result = TRUE;
357 else error_code_messagebox(hwnd, IDS_SET_VALUE_FAILED);
360 else /* hex data types */
362 struct edit_params params;
364 params.hKey = hKey;
365 params.lpszValueName = valueName;
366 params.pData = stringValueData;
367 params.cbData = len;
368 result = DialogBoxParamW(NULL, MAKEINTRESOURCEW(IDD_EDIT_BINARY), hwnd,
369 bin_modify_dlgproc, (LPARAM)&params);
372 /* Update the listview item with the new data string */
373 if (result)
375 int index = SendMessageW(g_pChildWnd->hListWnd, LVM_GETNEXTITEM, -1,
376 MAKELPARAM(LVNI_FOCUSED | LVNI_SELECTED, 0));
377 heap_free(stringValueData);
378 stringValueData = read_value(hwnd, hKey, valueName, &type, &len);
379 format_value_data(g_pChildWnd->hListWnd, index, type, stringValueData, len);
382 done:
383 heap_free(stringValueData);
384 stringValueData = NULL;
385 RegCloseKey(hKey);
386 return result;
389 BOOL DeleteKey(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath)
391 BOOL result = FALSE;
392 LONG lRet;
393 HKEY hKey;
395 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ|KEY_SET_VALUE, &hKey);
396 if (lRet != ERROR_SUCCESS) {
397 error_code_messagebox(hwnd, IDS_DELETE_KEY_FAILED);
398 return FALSE;
401 if (messagebox(hwnd, MB_YESNO | MB_ICONEXCLAMATION, IDS_DELETE_KEY_TITLE,
402 IDS_DELETE_KEY_TEXT) != IDYES)
403 goto done;
405 lRet = SHDeleteKeyW(hKeyRoot, keyPath);
406 if (lRet != ERROR_SUCCESS) {
407 error_code_messagebox(hwnd, IDS_BAD_KEY, keyPath);
408 goto done;
410 result = TRUE;
412 done:
413 RegCloseKey(hKey);
414 return result;
417 BOOL DeleteValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR valueName, BOOL showMessageBox)
419 BOOL result = FALSE;
420 LONG lRet;
421 HKEY hKey;
422 LPCWSTR visibleValueName = valueName ? valueName : g_pszDefaultValueName;
423 WCHAR empty = 0;
425 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
426 if (lRet != ERROR_SUCCESS) return FALSE;
428 if (showMessageBox)
430 if (messagebox(hwnd, MB_YESNO | MB_ICONEXCLAMATION, IDS_DELETE_VALUE_TITLE, IDS_DELETE_VALUE_TEXT,
431 visibleValueName) != IDYES)
432 goto done;
435 lRet = RegDeleteValueW(hKey, valueName ? valueName : &empty);
436 if (lRet != ERROR_SUCCESS && valueName) {
437 error_code_messagebox(hwnd, IDS_BAD_VALUE, valueName);
439 if (lRet != ERROR_SUCCESS) goto done;
440 result = TRUE;
442 done:
443 RegCloseKey(hKey);
444 return result;
447 BOOL CreateValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, DWORD valueType, LPWSTR valueName)
449 LONG lRet = ERROR_SUCCESS;
450 WCHAR newValue[256];
451 DWORD valueDword = 0;
452 BOOL result = FALSE;
453 int valueNum, index;
454 HKEY hKey;
455 LVITEMW item;
457 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
458 if (lRet != ERROR_SUCCESS) {
459 error_code_messagebox(hwnd, IDS_CREATE_VALUE_FAILED);
460 return FALSE;
463 if (!LoadStringW(GetModuleHandleW(0), IDS_NEWVALUE, newValue, COUNT_OF(newValue))) goto done;
465 /* try to find a name for the value being created (maximum = 100 attempts) */
466 for (valueNum = 1; valueNum < 100; valueNum++) {
467 wsprintfW(valueName, newValue, valueNum);
468 lRet = RegQueryValueExW(hKey, valueName, 0, 0, 0, 0);
469 if (lRet == ERROR_FILE_NOT_FOUND) break;
471 if (lRet != ERROR_FILE_NOT_FOUND) {
472 error_code_messagebox(hwnd, IDS_CREATE_VALUE_FAILED);
473 goto done;
476 lRet = RegSetValueExW(hKey, valueName, 0, valueType, (BYTE*)&valueDword, sizeof(DWORD));
477 if (lRet != ERROR_SUCCESS) {
478 error_code_messagebox(hwnd, IDS_CREATE_VALUE_FAILED);
479 goto done;
482 /* Add the new item to the listview */
483 index = AddEntryToList(g_pChildWnd->hListWnd, valueName, valueType,
484 (BYTE *)&valueDword, sizeof(DWORD), -1);
485 item.state = LVIS_FOCUSED | LVIS_SELECTED;
486 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
487 SendMessageW(g_pChildWnd->hListWnd, LVM_SETITEMSTATE, index, (LPARAM)&item);
489 result = TRUE;
491 done:
492 RegCloseKey(hKey);
493 return result;
496 BOOL RenameValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR oldName, LPCWSTR newName)
498 LPWSTR value = NULL;
499 DWORD type;
500 LONG len, lRet;
501 BOOL result = FALSE;
502 HKEY hKey;
504 if (!oldName) return FALSE;
505 if (!newName) return FALSE;
507 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
508 if (lRet != ERROR_SUCCESS) {
509 error_code_messagebox(hwnd, IDS_RENAME_VALUE_FAILED);
510 return FALSE;
512 /* check if the value already exists */
513 if (check_value(hwnd, hKey, newName)) {
514 error_code_messagebox(hwnd, IDS_VALUE_EXISTS, oldName);
515 goto done;
517 value = read_value(hwnd, hKey, oldName, &type, &len);
518 if(!value) goto done;
519 lRet = RegSetValueExW(hKey, newName, 0, type, (BYTE*)value, len);
520 if (lRet != ERROR_SUCCESS) {
521 error_code_messagebox(hwnd, IDS_RENAME_VALUE_FAILED);
522 goto done;
524 lRet = RegDeleteValueW(hKey, oldName);
525 if (lRet != ERROR_SUCCESS) {
526 RegDeleteValueW(hKey, newName);
527 error_code_messagebox(hwnd, IDS_RENAME_VALUE_FAILED);
528 goto done;
530 result = TRUE;
532 done:
533 heap_free(value);
534 RegCloseKey(hKey);
535 return result;
539 BOOL RenameKey(HWND hwnd, HKEY hRootKey, LPCWSTR keyPath, LPCWSTR newName)
541 LPWSTR parentPath = 0;
542 LPCWSTR srcSubKey = 0;
543 HKEY parentKey = 0;
544 HKEY destKey = 0;
545 BOOL result = FALSE;
546 LONG lRet;
547 DWORD disposition;
549 if (!keyPath || !newName) return FALSE;
551 if (!strrchrW(keyPath, '\\')) {
552 parentKey = hRootKey;
553 srcSubKey = keyPath;
554 } else {
555 LPWSTR srcSubKey_copy;
557 parentPath = heap_xalloc((lstrlenW(keyPath) + 1) * sizeof(WCHAR));
558 lstrcpyW(parentPath, keyPath);
559 srcSubKey_copy = strrchrW(parentPath, '\\');
560 *srcSubKey_copy = 0;
561 srcSubKey = srcSubKey_copy + 1;
562 lRet = RegOpenKeyExW(hRootKey, parentPath, 0, KEY_READ | KEY_CREATE_SUB_KEY, &parentKey);
563 if (lRet != ERROR_SUCCESS) {
564 error_code_messagebox(hwnd, IDS_RENAME_KEY_FAILED);
565 goto done;
569 /* The following fails if the old name is the same as the new name. */
570 if (!lstrcmpW(srcSubKey, newName)) goto done;
572 lRet = RegCreateKeyExW(parentKey, newName, 0, NULL, REG_OPTION_NON_VOLATILE,
573 KEY_WRITE, NULL /* FIXME */, &destKey, &disposition);
574 if (disposition == REG_OPENED_EXISTING_KEY)
575 lRet = ERROR_FILE_EXISTS;
576 if (lRet != ERROR_SUCCESS) {
577 error_code_messagebox(hwnd, IDS_KEY_EXISTS, srcSubKey);
578 goto done;
581 /* FIXME: SHCopyKey does not copy the security attributes */
582 lRet = SHCopyKeyW(parentKey, srcSubKey, destKey, 0);
583 if (lRet != ERROR_SUCCESS) {
584 RegCloseKey(destKey);
585 RegDeleteKeyW(parentKey, newName);
586 error_code_messagebox(hwnd, IDS_RENAME_KEY_FAILED);
587 goto done;
590 lRet = SHDeleteKeyW(hRootKey, keyPath);
591 if (lRet != ERROR_SUCCESS) {
592 error_code_messagebox(hwnd, IDS_RENAME_KEY_FAILED);
593 goto done;
596 result = TRUE;
598 done:
599 RegCloseKey(destKey);
600 if (parentKey) {
601 RegCloseKey(parentKey);
602 heap_free(parentPath);
604 return result;