Notepad:
[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 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", &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", &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", &Globals.lfFont.lfWeight);
145 QueryDword(hKey, "iPointSize", &dwPointSize);
146
147 if (dwPointSize != 0)
148 Globals.lfFont.lfHeight = HeightFromPointSize(dwPointSize);
149
150 hFont = CreateFontIndirect(&Globals.lfFont);
151 if (hFont)
152 {
153 if (Globals.hFont)
154 DeleteObject(Globals.hFont);
155 Globals.hFont = hFont;
156 }
157
158 RegCloseKey(hKey);
159 }
160 }
161
162 static BOOL SaveDword(HKEY hKey, LPCSTR pszValueName, DWORD dwValue)
163 {
164 WCHAR szValueW[32];
165 LPCTSTR pszValueNameT;
166
167 #ifdef UNICODE
168 MultiByteToWideChar(CP_ACP, 0, pszValueName, -1, szValueW, sizeof(szValueW) / sizeof(szValueW[0]));
169 pszValueNameT = szValueW;
170 #else
171 pszValueNameT = pszValueName;
172 #endif
173
174 return RegSetValueEx(hKey, pszValueNameT, 0, REG_DWORD, (LPBYTE) &dwValue, sizeof(dwValue)) == ERROR_SUCCESS;
175 }
176
177 static BOOL SaveString(HKEY hKey, LPCSTR pszValueName, LPCTSTR pszValue)
178 {
179 WCHAR szValueW[32];
180 LPCTSTR pszValueNameT;
181
182 #ifdef UNICODE
183 MultiByteToWideChar(CP_ACP, 0, pszValueName, -1, szValueW, sizeof(szValueW) / sizeof(szValueW[0]));
184 pszValueNameT = szValueW;
185 #else
186 pszValueNameT = pszValueName;
187 #endif
188
189 return RegSetValueEx(hKey, pszValueNameT, 0, REG_SZ, (LPBYTE) pszValue, _tcslen(pszValue) * sizeof(*pszValue)) == ERROR_SUCCESS;
190 }
191
192 void SaveSettings(void)
193 {
194 HKEY hKey;
195 DWORD dwDisposition;
196
197 if (RegCreateKeyEx(HKEY_CURRENT_USER, s_szRegistryKey, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition)
198 == ERROR_SUCCESS)
199 {
200 SaveDword(hKey, "lfCharSet", Globals.lfFont.lfCharSet);
201 SaveDword(hKey, "lfClipPrecision", Globals.lfFont.lfClipPrecision);
202 SaveDword(hKey, "lfEscapement", Globals.lfFont.lfEscapement);
203 SaveString(hKey, "lfFaceName", Globals.lfFont.lfFaceName);
204 SaveDword(hKey, "lfItalic", Globals.lfFont.lfItalic);
205 SaveDword(hKey, "lfOrientation", Globals.lfFont.lfOrientation);
206 SaveDword(hKey, "lfOutPrecision", Globals.lfFont.lfOutPrecision);
207 SaveDword(hKey, "lfPitchAndFamily", Globals.lfFont.lfPitchAndFamily);
208 SaveDword(hKey, "lfQuality", Globals.lfFont.lfQuality);
209 SaveDword(hKey, "lfStrikeOut", Globals.lfFont.lfStrikeOut);
210 SaveDword(hKey, "lfUnderline", Globals.lfFont.lfUnderline);
211 SaveDword(hKey, "lfWeight", Globals.lfFont.lfWeight);
212 SaveDword(hKey, "iPointSize", PointSizeFromHeight(Globals.lfFont.lfHeight));
213
214 RegCloseKey(hKey);
215 }
216
217 }