c15276fa465166acd80f3b84cec615ccae9de523
[reactos.git] / rosapps / calc / settings.c
1 /*
2 * ReactOS calc
3 *
4 * settings.c
5 *
6 * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
24 #include <windows.h>
25 #include <tchar.h>
26 #include <assert.h>
27 #define ASSERT assert
28
29 #include "main.h"
30 #include "settings.h"
31
32
33 static BOOL CheckResult(LONG error)
34 {
35 if (error != ERROR_SUCCESS) {
36 PTSTR msg;
37 if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
38 0, error, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), (PTSTR)&msg, 0, NULL))
39 MessageBox(NULL, msg, szTitle, MB_ICONERROR | MB_OK);
40 else
41 MessageBox(NULL, _T("Error"), szTitle, MB_ICONERROR | MB_OK);
42 LocalFree(msg);
43 return FALSE;
44 }
45 return TRUE;
46 }
47
48 static BOOL CreateRegistryPath(LPTSTR szRegPath, int nMaxLen)
49 {
50 LPTSTR pRegPath = szRegPath;
51
52 // Initialise registry path string from application PATH and KEY resources
53 int nLength = LoadString(hInst, IDS_APP_REG_PATH, szRegPath, nMaxLen);
54 nLength += LoadString(hInst, IDS_APP_REG_KEY, szRegPath + nLength, nMaxLen - nLength);
55 ASSERT(nLength < (nMaxLen - 1));
56 szRegPath[nLength] = _T('\\');
57
58 // walk the registry path string creating the tree if required
59 while (pRegPath = _tcschr(pRegPath, _T('\\'))) {
60 LONG result;
61 HKEY hKey = NULL;
62 *pRegPath = _T('\0');
63 // Open (or create) the key
64 result = RegCreateKeyEx(HKEY_CURRENT_USER, szRegPath, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
65 if (!CheckResult(result)) return FALSE;
66 RegCloseKey(hKey);
67 *pRegPath = _T('\\');
68 pRegPath = pRegPath + 1;
69 }
70 szRegPath[nLength] = _T('\0');
71 return TRUE;
72 }
73
74 void LoadSettings(void)
75 {
76 TCHAR szRegPath[MAX_LOADSTRING];
77
78 HKEY hKey;
79 DWORD dwSize;
80 LONG result;
81
82 if (!CreateRegistryPath(szRegPath, MAX_LOADSTRING)) return;
83
84 // Open the key
85 result = RegOpenKeyEx(HKEY_CURRENT_USER, szRegPath, 0, KEY_READ, &hKey);
86 if (!CheckResult(result)) return;
87
88 // Read the settings
89 dwSize = sizeof(CALC_TYPES);
90 result = RegQueryValueEx(hKey, _T("Preferences"), NULL, NULL, (LPBYTE)&CalcType, &dwSize);
91
92 // Close the key
93 RegCloseKey(hKey);
94 }
95
96 void SaveSettings(void)
97 {
98 TCHAR szRegPath[MAX_LOADSTRING];
99 HKEY hKey = NULL;
100 LONG result;
101
102 if (!CreateRegistryPath(szRegPath, MAX_LOADSTRING)) return;
103
104 // Open the key
105 result = RegOpenKeyEx(HKEY_CURRENT_USER, szRegPath, 0, KEY_WRITE, &hKey);
106 if (!CheckResult(result)) return;
107
108 // Save the settings
109 result = RegSetValueEx(hKey, _T("Preferences"), 0, REG_DWORD, (LPBYTE)&CalcType, sizeof(CALC_TYPES));
110 if (!CheckResult(result)) goto abort;
111
112 abort:
113 // Close the key
114 RegCloseKey(hKey);
115 }