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