Reformat regedit in a consistent manner.
[wine.git] / programs / regedit / edit.c
blob3c9f6f3622cf575879c70de31348f07946f6e3ed
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #define WIN32_LEAN_AND_MEAN /* Exclude rarely-used stuff from Windows headers */
23 #include <windows.h>
24 #include <tchar.h>
25 #include <commctrl.h>
26 #include <commdlg.h>
27 #include <cderr.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <shellapi.h>
32 #include "main.h"
33 #include "regproc.h"
34 #include "resource.h"
36 static TCHAR* editValueName;
37 static TCHAR* stringValueData;
39 void error(HWND hwnd, INT resId, ...)
41 va_list ap;
42 TCHAR title[256];
43 TCHAR errfmt[1024];
44 TCHAR errstr[1024];
45 HINSTANCE hInstance;
47 hInstance = GetModuleHandle(0);
49 if (!LoadString(hInstance, IDS_ERROR, title, COUNT_OF(title)))
50 lstrcpy(title, "Error");
52 if (!LoadString(hInstance, resId, errfmt, COUNT_OF(errfmt)))
53 lstrcpy(errfmt, "Unknown error string!");
55 va_start(ap, resId);
56 _vsntprintf(errstr, COUNT_OF(errstr), errfmt, ap);
57 va_end(ap);
59 MessageBox(hwnd, errstr, title, MB_OK | MB_ICONERROR);
62 INT_PTR CALLBACK modify_string_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
64 TCHAR* valueData;
65 HWND hwndValue;
66 int len;
68 switch(uMsg) {
69 case WM_INITDIALOG:
70 SetDlgItemText(hwndDlg, IDC_VALUE_NAME, editValueName);
71 SetDlgItemText(hwndDlg, IDC_VALUE_DATA, stringValueData);
72 return TRUE;
73 case WM_COMMAND:
74 switch (LOWORD(wParam)) {
75 case IDOK:
76 if ((hwndValue = GetDlgItem(hwndDlg, IDC_VALUE_DATA))) {
77 if ((len = GetWindowTextLength(hwndValue))) {
78 if ((valueData = HeapReAlloc(GetProcessHeap(), 0, stringValueData, (len + 1) * sizeof(TCHAR)))) {
79 stringValueData = valueData;
80 if (!GetWindowText(hwndValue, stringValueData, len + 1))
81 *stringValueData = 0;
85 /* Fall through */
86 case IDCANCEL:
87 EndDialog(hwndDlg, wParam);
88 return TRUE;
91 return FALSE;
94 BOOL ModifyValue(HWND hwnd, HKEY hKey, LPTSTR valueName)
96 DWORD valueDataLen;
97 DWORD type;
98 LONG lRet;
99 BOOL result = FALSE;
101 if (!hKey || !valueName) return FALSE;
103 editValueName = valueName;
105 lRet = RegQueryValueEx(hKey, valueName, 0, &type, 0, &valueDataLen);
106 if (lRet != ERROR_SUCCESS) {
107 error(hwnd, IDS_BAD_VALUE, valueName);
108 goto done;
111 if ( (type == REG_SZ) || (type == REG_EXPAND_SZ) ) {
112 if (!(stringValueData = HeapAlloc(GetProcessHeap(), 0, valueDataLen))) {
113 error(hwnd, IDS_TOO_BIG_VALUE, valueDataLen);
114 goto done;
116 lRet = RegQueryValueEx(hKey, valueName, 0, 0, stringValueData, &valueDataLen);
117 if (lRet != ERROR_SUCCESS) {
118 error(hwnd, IDS_BAD_VALUE, valueName);
119 goto done;
121 if (DialogBox(0, MAKEINTRESOURCE(IDD_EDIT_STRING), hwnd, modify_string_dlgproc) == IDOK) {
122 lRet = RegSetValueEx(hKey, valueName, 0, type, stringValueData, lstrlen(stringValueData) + 1);
123 if (lRet == ERROR_SUCCESS) result = TRUE;
125 } else if ( type == REG_DWORD ) {
126 MessageBox(hwnd, "Can't edit dwords for now", "Error", MB_OK | MB_ICONERROR);
127 } else {
128 error(hwnd, IDS_UNSUPPORTED_TYPE, type);
131 done:
132 HeapFree(GetProcessHeap(), 0, stringValueData);
133 stringValueData = NULL;
135 return result;