d05e2bfaf242276636e94a3a19f1ec51ebb89648
[reactos.git] / reactos / subsys / system / notepad / settings.c
1 /*
2 * Notepad (settings.c)
3 *
4 * Copyright 1998,99 Marcel Baur <mbaur@g26.ethz.ch>
5 * Copyright 2002 Sylvain Petreolle <spetreolle@yahoo.fr>
6 * Copyright 2002 Andriy Palamarchuk
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library 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 GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23 #define UNICODE
24 #define _UNICODE
25
26 #include <assert.h>
27 #include <stdio.h>
28 #include <windows.h>
29 #include <commdlg.h>
30 #include <tchar.h>
31
32 #include "main.h"
33
34 static const TCHAR s_szRegistryKey[] = { 'S','o','f','t','w','a','r','e',
35 '\\','M','i','c','r','o','s','o','f','t',
36 '\\','N','o','t','e','p','a','d',0 };
37
38
39 static LONG HeightFromPointSize(DWORD dwPointSize)
40 {
41 LONG lHeight;
42 HDC hDC;
43
44 hDC = GetDC(NULL);
45 lHeight = -MulDiv(dwPointSize, GetDeviceCaps(hDC, LOGPIXELSY), 720);
46 ReleaseDC(NULL, hDC);
47
48 return lHeight;
49 }
50
51 static DWORD PointSizeFromHeight(LONG lHeight)
52 {
53 DWORD dwPointSize;
54 HDC hDC;
55
56 hDC = GetDC(NULL);
57 dwPointSize = -MulDiv(lHeight, 720, GetDeviceCaps(hDC, LOGPIXELSY));
58 ReleaseDC(NULL, hDC);
59
60 /* round to nearest multiple of 10 */
61 dwPointSize += 5;
62 dwPointSize -= dwPointSize % 10;
63
64 return dwPointSize;
65 }
66
67 static BOOL QueryGeneric(HKEY hKey, LPCSTR pszValueName, DWORD dwExpectedType,
68 LPVOID pvResult, DWORD dwResultSize)
69 {
70 WCHAR szValueW[32];
71 LPCTSTR pszValueNameT;
72 DWORD dwType, cbData;
73 LPVOID *pTemp;
74 BOOL bSuccess = FALSE;
75
76 #ifdef UNICODE
77 MultiByteToWideChar(CP_ACP, 0, pszValueName, -1, szValueW, sizeof(szValueW) / sizeof(szValueW[0]));
78 pszValueNameT = szValueW;
79 #else
80 pszValueNameT = pszValueName;
81 #endif
82
83 pTemp = HeapAlloc(GetProcessHeap(), 0, dwResultSize);
84 if (!pTemp)
85 goto done;
86 memset(pTemp, 0, dwResultSize);
87
88 cbData = dwResultSize;
89 if (RegQueryValueEx(hKey, pszValueNameT, NULL, &dwType, (LPBYTE) pTemp, &cbData) != ERROR_SUCCESS)
90 goto done;
91
92 if (dwType != dwExpectedType)
93 goto done;
94
95 memcpy(pvResult, pTemp, cbData);
96 bSuccess = TRUE;
97
98 done:
99 if (pTemp)
100 HeapFree(GetProcessHeap(), 0, pTemp);
101 return bSuccess;
102 }
103
104 static BOOL QueryDword(HKEY hKey, LPCSTR pszValueName, DWORD *pdwResult)
105 {
106 return QueryGeneric(hKey, pszValueName, REG_DWORD, pdwResult, sizeof(*pdwResult));
107 }
108
109 static BOOL QueryByte(HKEY hKey, LPCSTR pszValueName, BYTE *pbResult)
110 {
111 DWORD dwResult;
112 if (!QueryGeneric(hKey, pszValueName, REG_DWORD, &dwResult, sizeof(dwResult)))
113 return FALSE;
114 if (dwResult >= 0x100)
115 return FALSE;
116 *pbResult = (BYTE) dwResult;
117 return TRUE;
118 }
119
120 static BOOL QueryBool(HKEY hKey, LPCSTR pszValueName, BOOL *pbResult)
121 {
122 DWORD dwResult;
123 if (!QueryDword(hKey, pszValueName, &dwResult))
124 return FALSE;
125 *pbResult = dwResult ? TRUE : FALSE;
126 return TRUE;
127 }
128
129 static BOOL QueryString(HKEY hKey, LPCSTR pszValueName, LPTSTR pszResult, DWORD dwResultSize)
130 {
131 return QueryGeneric(hKey, pszValueName, REG_SZ, pszResult, dwResultSize * sizeof(*pszResult));
132 }
133
134 void LoadSettings(void)
135 {
136 HKEY hKey = NULL;
137 HFONT hFont;
138 DWORD dwPointSize = 0;
139
140 if (RegOpenKey(HKEY_CURRENT_USER, s_szRegistryKey, &hKey) == ERROR_SUCCESS)
141 {
142 QueryByte(hKey, "lfCharSet", &Globals.lfFont.lfCharSet);
143 QueryByte(hKey, "lfClipPrecision", &Globals.lfFont.lfClipPrecision);
144 QueryDword(hKey, "lfEscapement", (DWORD*)&Globals.lfFont.lfEscapement);
145 QueryString(hKey, "lfFaceName", Globals.lfFont.lfFaceName, sizeof(Globals.lfFont.lfFaceName) / sizeof(Globals.lfFont.lfFaceName[0]));
146 QueryByte(hKey, "lfItalic", &Globals.lfFont.lfItalic);
147 QueryDword(hKey, "lfOrientation", (DWORD*)&Globals.lfFont.lfOrientation);
148 QueryByte(hKey, "lfOutPrecision", &Globals.lfFont.lfOutPrecision);
149 QueryByte(hKey, "lfPitchAndFamily", &Globals.lfFont.lfPitchAndFamily);
150 QueryByte(hKey, "lfQuality", &Globals.lfFont.lfQuality);
151 QueryByte(hKey, "lfStrikeOut", &Globals.lfFont.lfStrikeOut);
152 QueryByte(hKey, "lfUnderline", &Globals.lfFont.lfUnderline);
153 QueryDword(hKey, "lfWeight", (DWORD*)&Globals.lfFont.lfWeight);
154 QueryDword(hKey, "iPointSize", &dwPointSize);
155 QueryBool(hKey, "fWrap", &Globals.bWrapLongLines);
156
157 if (dwPointSize != 0)
158 Globals.lfFont.lfHeight = HeightFromPointSize(dwPointSize);
159
160 hFont = CreateFontIndirect(&Globals.lfFont);
161 if (hFont)
162 {
163 if (Globals.hFont)
164 DeleteObject(Globals.hFont);
165 Globals.hFont = hFont;
166 }
167
168 RegCloseKey(hKey);
169 }
170 }
171
172 static BOOL SaveDword(HKEY hKey, LPCSTR pszValueName, DWORD dwValue)
173 {
174 WCHAR szValueW[32];
175 LPCTSTR pszValueNameT;
176
177 #ifdef UNICODE
178 MultiByteToWideChar(CP_ACP, 0, pszValueName, -1, szValueW, sizeof(szValueW) / sizeof(szValueW[0]));
179 pszValueNameT = szValueW;
180 #else
181 pszValueNameT = pszValueName;
182 #endif
183
184 return RegSetValueEx(hKey, pszValueNameT, 0, REG_DWORD, (LPBYTE) &dwValue, sizeof(dwValue)) == ERROR_SUCCESS;
185 }
186
187 static BOOL SaveString(HKEY hKey, LPCSTR pszValueName, LPCTSTR pszValue)
188 {
189 WCHAR szValueW[32];
190 LPCTSTR pszValueNameT;
191
192 #ifdef UNICODE
193 MultiByteToWideChar(CP_ACP, 0, pszValueName, -1, szValueW, sizeof(szValueW) / sizeof(szValueW[0]));
194 pszValueNameT = szValueW;
195 #else
196 pszValueNameT = pszValueName;
197 #endif
198
199 return RegSetValueEx(hKey, pszValueNameT, 0, REG_SZ, (LPBYTE) pszValue, _tcslen(pszValue) * sizeof(*pszValue)) == ERROR_SUCCESS;
200 }
201
202 void SaveSettings(void)
203 {
204 HKEY hKey;
205 DWORD dwDisposition;
206
207 if (RegCreateKeyEx(HKEY_CURRENT_USER, s_szRegistryKey, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition)
208 == ERROR_SUCCESS)
209 {
210 SaveDword(hKey, "lfCharSet", Globals.lfFont.lfCharSet);
211 SaveDword(hKey, "lfClipPrecision", Globals.lfFont.lfClipPrecision);
212 SaveDword(hKey, "lfEscapement", Globals.lfFont.lfEscapement);
213 SaveString(hKey, "lfFaceName", Globals.lfFont.lfFaceName);
214 SaveDword(hKey, "lfItalic", Globals.lfFont.lfItalic);
215 SaveDword(hKey, "lfOrientation", Globals.lfFont.lfOrientation);
216 SaveDword(hKey, "lfOutPrecision", Globals.lfFont.lfOutPrecision);
217 SaveDword(hKey, "lfPitchAndFamily", Globals.lfFont.lfPitchAndFamily);
218 SaveDword(hKey, "lfQuality", Globals.lfFont.lfQuality);
219 SaveDword(hKey, "lfStrikeOut", Globals.lfFont.lfStrikeOut);
220 SaveDword(hKey, "lfUnderline", Globals.lfFont.lfUnderline);
221 SaveDword(hKey, "lfWeight", Globals.lfFont.lfWeight);
222 SaveDword(hKey, "iPointSize", PointSizeFromHeight(Globals.lfFont.lfHeight));
223 SaveDword(hKey, "fWrap", Globals.bWrapLongLines ? 1 : 0);
224
225 RegCloseKey(hKey);
226 }
227
228 }