- PCHify notepad and add missing header from regedit.
[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 #include <notepad.h>
24
25 static const TCHAR s_szRegistryKey[] = { 'S','o','f','t','w','a','r','e',
26 '\\','M','i','c','r','o','s','o','f','t',
27 '\\','N','o','t','e','p','a','d',0 };
28
29
30 static LONG HeightFromPointSize(DWORD dwPointSize)
31 {
32 LONG lHeight;
33 HDC hDC;
34
35 hDC = GetDC(NULL);
36 lHeight = -MulDiv(dwPointSize, GetDeviceCaps(hDC, LOGPIXELSY), 720);
37 ReleaseDC(NULL, hDC);
38
39 return lHeight;
40 }
41
42 static DWORD PointSizeFromHeight(LONG lHeight)
43 {
44 DWORD dwPointSize;
45 HDC hDC;
46
47 hDC = GetDC(NULL);
48 dwPointSize = -MulDiv(lHeight, 720, GetDeviceCaps(hDC, LOGPIXELSY));
49 ReleaseDC(NULL, hDC);
50
51 /* round to nearest multiple of 10 */
52 dwPointSize += 5;
53 dwPointSize -= dwPointSize % 10;
54
55 return dwPointSize;
56 }
57
58 static BOOL QueryGeneric(HKEY hKey, LPCSTR pszValueName, DWORD dwExpectedType,
59 LPVOID pvResult, DWORD dwResultSize)
60 {
61 WCHAR szValueW[32];
62 LPCTSTR pszValueNameT;
63 DWORD dwType, cbData;
64 LPVOID *pTemp;
65 BOOL bSuccess = FALSE;
66
67 #ifdef UNICODE
68 MultiByteToWideChar(CP_ACP, 0, pszValueName, -1, szValueW, sizeof(szValueW) / sizeof(szValueW[0]));
69 pszValueNameT = szValueW;
70 #else
71 pszValueNameT = pszValueName;
72 #endif
73
74 pTemp = HeapAlloc(GetProcessHeap(), 0, dwResultSize);
75 if (!pTemp)
76 goto done;
77 memset(pTemp, 0, dwResultSize);
78
79 cbData = dwResultSize;
80 if (RegQueryValueEx(hKey, pszValueNameT, NULL, &dwType, (LPBYTE) pTemp, &cbData) != ERROR_SUCCESS)
81 goto done;
82
83 if (dwType != dwExpectedType)
84 goto done;
85
86 memcpy(pvResult, pTemp, cbData);
87 bSuccess = TRUE;
88
89 done:
90 if (pTemp)
91 HeapFree(GetProcessHeap(), 0, pTemp);
92 return bSuccess;
93 }
94
95 static BOOL QueryDword(HKEY hKey, LPCSTR pszValueName, DWORD *pdwResult)
96 {
97 return QueryGeneric(hKey, pszValueName, REG_DWORD, pdwResult, sizeof(*pdwResult));
98 }
99
100 static BOOL QueryByte(HKEY hKey, LPCSTR pszValueName, BYTE *pbResult)
101 {
102 DWORD dwResult;
103 if (!QueryGeneric(hKey, pszValueName, REG_DWORD, &dwResult, sizeof(dwResult)))
104 return FALSE;
105 if (dwResult >= 0x100)
106 return FALSE;
107 *pbResult = (BYTE) dwResult;
108 return TRUE;
109 }
110
111 static BOOL QueryBool(HKEY hKey, LPCSTR pszValueName, BOOL *pbResult)
112 {
113 DWORD dwResult;
114 if (!QueryDword(hKey, pszValueName, &dwResult))
115 return FALSE;
116 *pbResult = dwResult ? TRUE : FALSE;
117 return TRUE;
118 }
119
120 static BOOL QueryString(HKEY hKey, LPCSTR pszValueName, LPTSTR pszResult, DWORD dwResultSize)
121 {
122 return QueryGeneric(hKey, pszValueName, REG_SZ, pszResult, dwResultSize * sizeof(*pszResult));
123 }
124
125 void LoadSettings(void)
126 {
127 HKEY hKey = NULL;
128 HFONT hFont;
129 DWORD dwPointSize = 0;
130
131 if (RegOpenKey(HKEY_CURRENT_USER, s_szRegistryKey, &hKey) == ERROR_SUCCESS)
132 {
133 QueryByte(hKey, "lfCharSet", &Globals.lfFont.lfCharSet);
134 QueryByte(hKey, "lfClipPrecision", &Globals.lfFont.lfClipPrecision);
135 QueryDword(hKey, "lfEscapement", (DWORD*)&Globals.lfFont.lfEscapement);
136 QueryString(hKey, "lfFaceName", Globals.lfFont.lfFaceName, sizeof(Globals.lfFont.lfFaceName) / sizeof(Globals.lfFont.lfFaceName[0]));
137 QueryByte(hKey, "lfItalic", &Globals.lfFont.lfItalic);
138 QueryDword(hKey, "lfOrientation", (DWORD*)&Globals.lfFont.lfOrientation);
139 QueryByte(hKey, "lfOutPrecision", &Globals.lfFont.lfOutPrecision);
140 QueryByte(hKey, "lfPitchAndFamily", &Globals.lfFont.lfPitchAndFamily);
141 QueryByte(hKey, "lfQuality", &Globals.lfFont.lfQuality);
142 QueryByte(hKey, "lfStrikeOut", &Globals.lfFont.lfStrikeOut);
143 QueryByte(hKey, "lfUnderline", &Globals.lfFont.lfUnderline);
144 QueryDword(hKey, "lfWeight", (DWORD*)&Globals.lfFont.lfWeight);
145 QueryDword(hKey, "iPointSize", &dwPointSize);
146 QueryBool(hKey, "fWrap", &Globals.bWrapLongLines);
147
148 if (dwPointSize != 0)
149 Globals.lfFont.lfHeight = HeightFromPointSize(dwPointSize);
150
151 hFont = CreateFontIndirect(&Globals.lfFont);
152 if (hFont)
153 {
154 if (Globals.hFont)
155 DeleteObject(Globals.hFont);
156 Globals.hFont = hFont;
157 }
158
159 RegCloseKey(hKey);
160 }
161 }
162
163 static BOOL SaveDword(HKEY hKey, LPCSTR pszValueName, DWORD dwValue)
164 {
165 WCHAR szValueW[32];
166 LPCTSTR pszValueNameT;
167
168 #ifdef UNICODE
169 MultiByteToWideChar(CP_ACP, 0, pszValueName, -1, szValueW, sizeof(szValueW) / sizeof(szValueW[0]));
170 pszValueNameT = szValueW;
171 #else
172 pszValueNameT = pszValueName;
173 #endif
174
175 return RegSetValueEx(hKey, pszValueNameT, 0, REG_DWORD, (LPBYTE) &dwValue, sizeof(dwValue)) == ERROR_SUCCESS;
176 }
177
178 static BOOL SaveString(HKEY hKey, LPCSTR pszValueName, LPCTSTR pszValue)
179 {
180 WCHAR szValueW[32];
181 LPCTSTR pszValueNameT;
182
183 #ifdef UNICODE
184 MultiByteToWideChar(CP_ACP, 0, pszValueName, -1, szValueW, sizeof(szValueW) / sizeof(szValueW[0]));
185 pszValueNameT = szValueW;
186 #else
187 pszValueNameT = pszValueName;
188 #endif
189
190 return RegSetValueEx(hKey, pszValueNameT, 0, REG_SZ, (LPBYTE) pszValue, _tcslen(pszValue) * sizeof(*pszValue)) == ERROR_SUCCESS;
191 }
192
193 void SaveSettings(void)
194 {
195 HKEY hKey;
196 DWORD dwDisposition;
197
198 if (RegCreateKeyEx(HKEY_CURRENT_USER, s_szRegistryKey, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition)
199 == ERROR_SUCCESS)
200 {
201 SaveDword(hKey, "lfCharSet", Globals.lfFont.lfCharSet);
202 SaveDword(hKey, "lfClipPrecision", Globals.lfFont.lfClipPrecision);
203 SaveDword(hKey, "lfEscapement", Globals.lfFont.lfEscapement);
204 SaveString(hKey, "lfFaceName", Globals.lfFont.lfFaceName);
205 SaveDword(hKey, "lfItalic", Globals.lfFont.lfItalic);
206 SaveDword(hKey, "lfOrientation", Globals.lfFont.lfOrientation);
207 SaveDword(hKey, "lfOutPrecision", Globals.lfFont.lfOutPrecision);
208 SaveDword(hKey, "lfPitchAndFamily", Globals.lfFont.lfPitchAndFamily);
209 SaveDword(hKey, "lfQuality", Globals.lfFont.lfQuality);
210 SaveDword(hKey, "lfStrikeOut", Globals.lfFont.lfStrikeOut);
211 SaveDword(hKey, "lfUnderline", Globals.lfFont.lfUnderline);
212 SaveDword(hKey, "lfWeight", Globals.lfFont.lfWeight);
213 SaveDword(hKey, "iPointSize", PointSizeFromHeight(Globals.lfFont.lfHeight));
214 SaveDword(hKey, "fWrap", Globals.bWrapLongLines ? 1 : 0);
215
216 RegCloseKey(hKey);
217 }
218
219 }