[NOTEPAD] Set Lucida Console as the default font. By Ismael Ferreras Morezuelas....
[reactos.git] / reactos / base / applications / 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include "notepad.h"
24
25 #include <winreg.h>
26
27 static LPCTSTR s_szRegistryKey = _T("Software\\Microsoft\\Notepad");
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
59 QueryGeneric(HKEY hKey, LPCTSTR pszValueNameT, DWORD dwExpectedType,
60 LPVOID pvResult, DWORD dwResultSize)
61 {
62 DWORD dwType, cbData;
63 LPVOID *pTemp = _alloca(dwResultSize);
64
65 ZeroMemory(pTemp, dwResultSize);
66
67 cbData = dwResultSize;
68 if (RegQueryValueEx(hKey, pszValueNameT, NULL, &dwType, (LPBYTE) pTemp, &cbData) != ERROR_SUCCESS)
69 return FALSE;
70
71 if (dwType != dwExpectedType)
72 return FALSE;
73
74 memcpy(pvResult, pTemp, cbData);
75 return TRUE;
76 }
77
78 static BOOL QueryDword(HKEY hKey, LPCTSTR pszValueName, DWORD *pdwResult)
79 {
80 return QueryGeneric(hKey, pszValueName, REG_DWORD, pdwResult, sizeof(*pdwResult));
81 }
82
83 static BOOL QueryByte(HKEY hKey, LPCTSTR pszValueName, BYTE *pbResult)
84 {
85 DWORD dwResult;
86 if (!QueryGeneric(hKey, pszValueName, REG_DWORD, &dwResult, sizeof(dwResult)))
87 return FALSE;
88 if (dwResult >= 0x100)
89 return FALSE;
90 *pbResult = (BYTE) dwResult;
91 return TRUE;
92 }
93
94 static BOOL QueryBool(HKEY hKey, LPCTSTR pszValueName, BOOL *pbResult)
95 {
96 DWORD dwResult;
97 if (!QueryDword(hKey, pszValueName, &dwResult))
98 return FALSE;
99 *pbResult = dwResult ? TRUE : FALSE;
100 return TRUE;
101 }
102
103 static BOOL QueryString(HKEY hKey, LPCTSTR pszValueName, LPTSTR pszResult, DWORD dwResultSize)
104 {
105 return QueryGeneric(hKey, pszValueName, REG_SZ, pszResult, dwResultSize * sizeof(TCHAR));
106 }
107
108 /***********************************************************************
109 *
110 * NOTEPAD_LoadSettingsFromRegistry
111 *
112 * Load settings from registry HKCU\Software\Microsoft\Notepad.
113 */
114 void NOTEPAD_LoadSettingsFromRegistry(void)
115 {
116 HKEY hKey = NULL;
117 HFONT hFont;
118 DWORD dwPointSize = 0;
119 INT base_length, dx, dy;
120
121 base_length = (GetSystemMetrics(SM_CXSCREEN) > GetSystemMetrics(SM_CYSCREEN)) ?
122 GetSystemMetrics(SM_CYSCREEN) : GetSystemMetrics(SM_CXSCREEN);
123
124 dx = (INT)(base_length * .95);
125 dy = dx * 3 / 4;
126 SetRect(&Globals.main_rect, 0, 0, dx, dy);
127
128 if (RegOpenKey(HKEY_CURRENT_USER, s_szRegistryKey, &hKey) == ERROR_SUCCESS)
129 {
130 QueryByte(hKey, _T("lfCharSet"), &Globals.lfFont.lfCharSet);
131 QueryByte(hKey, _T("lfClipPrecision"), &Globals.lfFont.lfClipPrecision);
132 QueryDword(hKey, _T("lfEscapement"), (DWORD*)&Globals.lfFont.lfEscapement);
133 QueryString(hKey, _T("lfFaceName"), Globals.lfFont.lfFaceName, ARRAY_SIZE(Globals.lfFont.lfFaceName));
134 QueryByte(hKey, _T("lfItalic"), &Globals.lfFont.lfItalic);
135 QueryDword(hKey, _T("lfOrientation"), (DWORD*)&Globals.lfFont.lfOrientation);
136 QueryByte(hKey, _T("lfOutPrecision"), &Globals.lfFont.lfOutPrecision);
137 QueryByte(hKey, _T("lfPitchAndFamily"), &Globals.lfFont.lfPitchAndFamily);
138 QueryByte(hKey, _T("lfQuality"), &Globals.lfFont.lfQuality);
139 QueryByte(hKey, _T("lfStrikeOut"), &Globals.lfFont.lfStrikeOut);
140 QueryByte(hKey, _T("lfUnderline"), &Globals.lfFont.lfUnderline);
141 QueryDword(hKey, _T("lfWeight"), (DWORD*)&Globals.lfFont.lfWeight);
142 QueryDword(hKey, _T("iPointSize"), &dwPointSize);
143 QueryBool(hKey, _T("fWrap"), &Globals.bWrapLongLines);
144 QueryBool(hKey, _T("fStatusBar"), &Globals.bShowStatusBar);
145 QueryString(hKey, _T("szHeader"), Globals.szHeader, ARRAY_SIZE(Globals.szHeader));
146 QueryString(hKey, _T("szTrailer"), Globals.szFooter, ARRAY_SIZE(Globals.szFooter));
147 QueryDword(hKey, _T("iMarginLeft"), (DWORD*)&Globals.lMargins.left);
148 QueryDword(hKey, _T("iMarginTop"), (DWORD*)&Globals.lMargins.top);
149 QueryDword(hKey, _T("iMarginRight"), (DWORD*)&Globals.lMargins.right);
150 QueryDword(hKey, _T("iMarginBottom"), (DWORD*)&Globals.lMargins.bottom);
151
152 QueryDword(hKey, _T("iWindowPosX"), (DWORD*)&Globals.main_rect.left);
153 QueryDword(hKey, _T("iWindowPosY"), (DWORD*)&Globals.main_rect.top);
154 QueryDword(hKey, _T("iWindowPosDX"), (DWORD*)&dx);
155 QueryDword(hKey, _T("iWindowPosDY"), (DWORD*)&dy);
156
157 Globals.main_rect.right = Globals.main_rect.left + dx;
158 Globals.main_rect.bottom = Globals.main_rect.top + dy;
159
160 /* invert value because DIALOG_ViewStatusBar will be called to show it */
161 Globals.bShowStatusBar = !Globals.bShowStatusBar;
162
163 if (dwPointSize != 0)
164 Globals.lfFont.lfHeight = HeightFromPointSize(dwPointSize);
165 else
166 Globals.lfFont.lfHeight = HeightFromPointSize(100);
167
168 RegCloseKey(hKey);
169 }
170 else
171 {
172 /* If no settings are found in the registry, then use default values */
173 Globals.lfFont.lfCharSet = 163;
174 Globals.lfFont.lfClipPrecision = 2;
175 Globals.lfFont.lfEscapement = 0;
176 _tcscpy(Globals.lfFont.lfFaceName, _T("Lucida Console"));
177 Globals.lfFont.lfItalic = 0;
178 Globals.lfFont.lfOrientation = 0;
179 Globals.lfFont.lfOutPrecision = 3;
180 Globals.lfFont.lfPitchAndFamily = 34;
181 Globals.lfFont.lfQuality = 1;
182 Globals.lfFont.lfStrikeOut = 0;
183 Globals.lfFont.lfUnderline = 0;
184 Globals.lfFont.lfWeight = 400;
185 Globals.lfFont.lfHeight = HeightFromPointSize(100);
186 }
187
188 hFont = CreateFontIndirect(&Globals.lfFont);
189 SendMessage(Globals.hEdit, WM_SETFONT, (WPARAM)hFont, (LPARAM)TRUE);
190 if (hFont)
191 {
192 if (Globals.hFont)
193 DeleteObject(Globals.hFont);
194 Globals.hFont = hFont;
195 }
196 }
197
198 static BOOL SaveDword(HKEY hKey, LPCTSTR pszValueNameT, DWORD dwValue)
199 {
200 return RegSetValueEx(hKey, pszValueNameT, 0, REG_DWORD, (LPBYTE) &dwValue, sizeof(dwValue)) == ERROR_SUCCESS;
201 }
202
203 static BOOL SaveString(HKEY hKey, LPCTSTR pszValueNameT, LPCTSTR pszValue)
204 {
205 return RegSetValueEx(hKey, pszValueNameT, 0, REG_SZ, (LPBYTE) pszValue, (DWORD) _tcslen(pszValue) * sizeof(*pszValue)) == ERROR_SUCCESS;
206 }
207
208 /***********************************************************************
209 *
210 * NOTEPAD_SaveSettingsToRegistry
211 *
212 * Save settings to registry HKCU\Software\Microsoft\Notepad.
213 */
214 void NOTEPAD_SaveSettingsToRegistry(void)
215 {
216 HKEY hKey;
217 DWORD dwDisposition;
218
219 GetWindowRect(Globals.hMainWnd, &Globals.main_rect);
220
221 if (RegCreateKeyEx(HKEY_CURRENT_USER, s_szRegistryKey,
222 0, NULL, 0, KEY_SET_VALUE, NULL,
223 &hKey, &dwDisposition) == ERROR_SUCCESS)
224 {
225 SaveDword(hKey, _T("lfCharSet"), Globals.lfFont.lfCharSet);
226 SaveDword(hKey, _T("lfClipPrecision"), Globals.lfFont.lfClipPrecision);
227 SaveDword(hKey, _T("lfEscapement"), Globals.lfFont.lfEscapement);
228 SaveString(hKey, _T("lfFaceName"), Globals.lfFont.lfFaceName);
229 SaveDword(hKey, _T("lfItalic"), Globals.lfFont.lfItalic);
230 SaveDword(hKey, _T("lfOrientation"), Globals.lfFont.lfOrientation);
231 SaveDword(hKey, _T("lfOutPrecision"), Globals.lfFont.lfOutPrecision);
232 SaveDword(hKey, _T("lfPitchAndFamily"), Globals.lfFont.lfPitchAndFamily);
233 SaveDword(hKey, _T("lfQuality"), Globals.lfFont.lfQuality);
234 SaveDword(hKey, _T("lfStrikeOut"), Globals.lfFont.lfStrikeOut);
235 SaveDword(hKey, _T("lfUnderline"), Globals.lfFont.lfUnderline);
236 SaveDword(hKey, _T("lfWeight"), Globals.lfFont.lfWeight);
237 SaveDword(hKey, _T("iPointSize"), PointSizeFromHeight(Globals.lfFont.lfHeight));
238 SaveDword(hKey, _T("fWrap"), Globals.bWrapLongLines ? 1 : 0);
239 SaveDword(hKey, _T("fStatusBar"), Globals.bShowStatusBar ? 1 : 0);
240 SaveString(hKey, _T("szHeader"), Globals.szHeader);
241 SaveString(hKey, _T("szTrailer"), Globals.szFooter);
242 SaveDword(hKey, _T("iMarginLeft"), Globals.lMargins.left);
243 SaveDword(hKey, _T("iMarginTop"), Globals.lMargins.top);
244 SaveDword(hKey, _T("iMarginRight"), Globals.lMargins.right);
245 SaveDword(hKey, _T("iMarginBottom"), Globals.lMargins.bottom);
246 SaveDword(hKey, _T("iWindowPosX"), Globals.main_rect.left);
247 SaveDword(hKey, _T("iWindowPosY"), Globals.main_rect.top);
248 SaveDword(hKey, _T("iWindowPosDX"), Globals.main_rect.right - Globals.main_rect.left);
249 SaveDword(hKey, _T("iWindowPosDY"), Globals.main_rect.bottom - Globals.main_rect.top);
250
251 RegCloseKey(hKey);
252 }
253 }