[NTOSKRNL]
[reactos.git] / base / applications / charmap / settings.c
1 /*
2 * PROJECT: ReactOS Character Map
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/applications/charmap/settings.c
5 * PURPOSE: save/load settings
6 * COPYRIGHT: Copyright 2012 Edijs Kolesnikovics <terminedijs@yahoo.com>
7 *
8 */
9
10 #include <precomp.h>
11
12
13 const TCHAR g_szGeneralRegKey[] = _T("Software\\Microsoft\\CharMap");
14 HWND hWnd;
15
16 LONG QueryStringValue(HKEY hKey, LPCTSTR lpSubKey, LPCTSTR lpValueName, LPTSTR pszBuffer, DWORD dwBufferLen)
17 {
18 LONG lResult;
19 HKEY hSubKey = NULL;
20 DWORD cbData, dwType;
21
22 if (lpSubKey)
23 {
24 lResult = RegOpenKey(hKey, lpSubKey, &hSubKey);
25 if (lResult != ERROR_SUCCESS)
26 goto done;
27 hKey = hSubKey;
28 }
29
30 cbData = (dwBufferLen - 1) * sizeof(*pszBuffer);
31 lResult = RegQueryValueEx(hKey, lpValueName, NULL, &dwType, (LPBYTE) pszBuffer, &cbData);
32 if (lResult != ERROR_SUCCESS)
33 goto done;
34 if (dwType != REG_SZ)
35 {
36 lResult = -1;
37 goto done;
38 }
39
40 pszBuffer[cbData / sizeof(*pszBuffer)] = _T('\0');
41
42 done:
43 if (lResult != ERROR_SUCCESS)
44 pszBuffer[0] = _T('\0');
45 if (hSubKey)
46 RegCloseKey(hSubKey);
47 return lResult;
48 }
49
50 extern void LoadSettings(void)
51 {
52 HKEY hKey = NULL;
53 int iItemIndex = -1;
54
55 if (RegOpenKeyEx(HKEY_CURRENT_USER, g_szGeneralRegKey, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
56 {
57 TCHAR szBuffer[MAX_PATH];
58 DWORD dwAdvanChecked;
59 unsigned long type = REG_DWORD, size = 1024;
60
61 /* Restore last selected font */
62 if (QueryStringValue(HKEY_CURRENT_USER, g_szGeneralRegKey, _T("Font"), szBuffer, (sizeof(szBuffer)/sizeof(szBuffer[0]))) == ERROR_SUCCESS)
63 {
64 //Get combobox handle
65 hWnd = GetDlgItem(hCharmapDlg, IDC_FONTCOMBO);
66
67 //Search for match and return index if match found
68 iItemIndex = ComboBox_FindStringExact(hWnd, -1, szBuffer);
69 if(iItemIndex != CB_ERR)
70 {
71 ComboBox_SetCurSel(hWnd, iItemIndex);
72 ChangeMapFont(hCharmapDlg);
73 }
74 }
75
76 /* Restore last selected character set */
77 if (QueryStringValue(HKEY_CURRENT_USER, g_szGeneralRegKey, _T("CodePage"), szBuffer, (sizeof(szBuffer)/sizeof(szBuffer[0]))) == ERROR_SUCCESS)
78 {
79 //Get combobox handle
80 hWnd = GetDlgItem(hCharmapDlg, IDC_COMBO_CHARSET);
81
82 iItemIndex = ComboBox_FindStringExact(hWnd, -1, szBuffer);
83 if(iItemIndex != CB_ERR)
84 {
85 ComboBox_SetCurSel(hWnd, iItemIndex);
86 }
87 }
88
89 RegQueryValueEx(hKey, _T("Advanced"), NULL, &type, (LPBYTE)&dwAdvanChecked, &size);
90 if(dwAdvanChecked == TRUE)
91 SendDlgItemMessage(hCharmapDlg, IDC_CHECK_ADVANCED, BM_CLICK, (dwAdvanChecked ? MF_CHECKED : MF_UNCHECKED), 0);
92
93 RegCloseKey(hKey);
94 }
95 else
96 {
97 /* Default font seems to be Arial */
98 hWnd = GetDlgItem(hCharmapDlg, IDC_FONTCOMBO);
99
100 iItemIndex = ComboBox_FindStringExact(hWnd, -1, _T("Arial"));
101 if(iItemIndex != CB_ERR)
102 {
103 ComboBox_SetCurSel(hWnd, iItemIndex);
104 ChangeMapFont(hCharmapDlg);
105 }
106 }
107 }
108
109 extern void SaveSettings(void)
110 {
111 HKEY hKey = NULL;
112
113 if (RegCreateKey(HKEY_CURRENT_USER, g_szGeneralRegKey, &hKey) == ERROR_SUCCESS)
114 {
115 if (RegOpenKeyEx(HKEY_CURRENT_USER, g_szGeneralRegKey, 0, KEY_SET_VALUE, &hKey) == ERROR_SUCCESS)
116 {
117 TCHAR szBuffer[MAX_PATH];
118
119 hWnd = GetDlgItem(hCharmapDlg, IDC_FONTCOMBO);
120 ComboBox_GetText(hWnd, szBuffer, MAX_PATH);
121
122 if(szBuffer != NULL && *szBuffer != '\0')
123 RegSetValueEx(hKey, _T("Font"), 0, REG_SZ, (LPBYTE) szBuffer, (DWORD) MAX_PATH);
124
125 hWnd = GetDlgItem(hCharmapDlg, IDC_COMBO_CHARSET);
126 ComboBox_GetText(hWnd, szBuffer, MAX_PATH);
127
128 if(szBuffer != NULL && *szBuffer != '\0')
129 RegSetValueEx(hKey, _T("CodePage"), 0, REG_SZ, (LPBYTE) szBuffer, (DWORD) MAX_PATH);
130
131 RegSetValueEx(hKey, _T("Advanced"), 0, REG_DWORD, (LPBYTE)&Settings.IsAdvancedView, (DWORD) sizeof(DWORD));
132
133 RegCloseKey(hKey);
134 }
135 }
136 }