Added save and restore of calculator mode using registry.
[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
27 #include "main.h"
28 #include "settings.h"
29
30
31 BOOL CheckResult(LONG error)
32 {
33 if (error != ERROR_SUCCESS) {
34 PTSTR msg;
35 if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
36 0, error, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), (PTSTR)&msg, 0, NULL))
37 MessageBox(NULL, msg, szTitle, MB_ICONERROR | MB_OK);
38 else
39 MessageBox(NULL, _T("Error"), szTitle, MB_ICONERROR | MB_OK);
40 LocalFree(msg);
41 return FALSE;
42 }
43 return TRUE;
44 }
45
46 void LoadSettings(void)
47 {
48 HKEY hKey;
49 DWORD dwSize;
50 LONG result;
51 char szSubKey[] = "Software\\ReactWare\\Calculator";
52
53 // Open the key
54 result = RegOpenKeyEx(HKEY_CURRENT_USER, szSubKey, 0, KEY_READ, &hKey);
55 if (!CheckResult(result)) return;
56
57 // Read the settings
58 dwSize = sizeof(CALC_TYPES);
59 result = RegQueryValueEx(hKey, _T("Preferences"), NULL, NULL, (LPBYTE)&CalcType, &dwSize);
60 if (!CheckResult(result)) goto abort;
61
62 abort:
63 // Close the key
64 RegCloseKey(hKey);
65 }
66
67 void SaveSettings(void)
68 {
69 HKEY hKey;
70 LONG result;
71 char szSubKey1[] = "Software";
72 char szSubKey2[] = "Software\\ReactWare";
73 char szSubKey3[] = "Software\\ReactWare\\Calculator";
74
75 // Open (or create) the key
76 hKey = NULL;
77 result = RegCreateKeyEx(HKEY_CURRENT_USER, szSubKey1, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
78 if (!CheckResult(result)) return;
79 RegCloseKey(hKey);
80 hKey = NULL;
81 result = RegCreateKeyEx(HKEY_CURRENT_USER, szSubKey2, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
82 if (!CheckResult(result)) return;
83 RegCloseKey(hKey);
84 hKey = NULL;
85 result = RegCreateKeyEx(HKEY_CURRENT_USER, szSubKey3, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
86 if (!CheckResult(result)) return;
87
88 // Save the settings
89 result = RegSetValueEx(hKey, _T("Preferences"), 0, REG_DWORD, (LPBYTE)&CalcType, sizeof(CALC_TYPES));
90 if (!CheckResult(result)) goto abort;
91
92 abort:
93 // Close the key
94 RegCloseKey(hKey);
95 }