[NOTEPAD]
[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 QueryGeneric(HKEY hKey, LPCTSTR pszValueNameT, DWORD dwExpectedType,
59 LPVOID pvResult, DWORD dwResultSize)
60 {
61 DWORD dwType, cbData;
62 LPVOID *pTemp = _alloca(dwResultSize);
63
64 ZeroMemory(pTemp, dwResultSize);
65
66 cbData = dwResultSize;
67 if (RegQueryValueEx(hKey, pszValueNameT, NULL, &dwType, (LPBYTE) pTemp, &cbData) != ERROR_SUCCESS)
68 return FALSE;
69
70 if (dwType != dwExpectedType)
71 return FALSE;
72
73 memcpy(pvResult, pTemp, cbData);
74 return TRUE;
75 }
76
77 static BOOL QueryDword(HKEY hKey, LPCTSTR pszValueName, DWORD *pdwResult)
78 {
79 return QueryGeneric(hKey, pszValueName, REG_DWORD, pdwResult, sizeof(*pdwResult));
80 }
81
82 static BOOL QueryByte(HKEY hKey, LPCTSTR pszValueName, BYTE *pbResult)
83 {
84 DWORD dwResult;
85 if (!QueryGeneric(hKey, pszValueName, REG_DWORD, &dwResult, sizeof(dwResult)))
86 return FALSE;
87 if (dwResult >= 0x100)
88 return FALSE;
89 *pbResult = (BYTE) dwResult;
90 return TRUE;
91 }
92
93 static BOOL QueryBool(HKEY hKey, LPCTSTR pszValueName, BOOL *pbResult)
94 {
95 DWORD dwResult;
96 if (!QueryDword(hKey, pszValueName, &dwResult))
97 return FALSE;
98 *pbResult = dwResult ? TRUE : FALSE;
99 return TRUE;
100 }
101
102 static BOOL QueryString(HKEY hKey, LPCTSTR pszValueName, LPTSTR pszResult, DWORD dwResultSize)
103 {
104 return QueryGeneric(hKey, pszValueName, REG_SZ, pszResult, dwResultSize * sizeof(TCHAR));
105 }
106
107 void LoadSettings(void)
108 {
109 HKEY hKey = NULL;
110 HFONT hFont;
111 DWORD dwPointSize = 0;
112 INT base_length, dx, dy;
113
114 base_length = (GetSystemMetrics(SM_CXSCREEN) > GetSystemMetrics(SM_CYSCREEN))?
115 GetSystemMetrics(SM_CYSCREEN) : GetSystemMetrics(SM_CXSCREEN);
116
117 dx = (INT)(base_length * .95);
118 dy = dx * 3 / 4;
119 SetRect( &Globals.main_rect, 0, 0, dx, dy );
120
121 if (RegOpenKey(HKEY_CURRENT_USER, s_szRegistryKey, &hKey) == ERROR_SUCCESS)
122 {
123 QueryByte(hKey, _T("lfCharSet"), &Globals.lfFont.lfCharSet);
124 QueryByte(hKey, _T("lfClipPrecision"), &Globals.lfFont.lfClipPrecision);
125 QueryDword(hKey, _T("lfEscapement"), (DWORD*)&Globals.lfFont.lfEscapement);
126 QueryString(hKey, _T("lfFaceName"), Globals.lfFont.lfFaceName, sizeof(Globals.lfFont.lfFaceName) / sizeof(Globals.lfFont.lfFaceName[0]));
127 QueryByte(hKey, _T("lfItalic"), &Globals.lfFont.lfItalic);
128 QueryDword(hKey, _T("lfOrientation"), (DWORD*)&Globals.lfFont.lfOrientation);
129 QueryByte(hKey, _T("lfOutPrecision"), &Globals.lfFont.lfOutPrecision);
130 QueryByte(hKey, _T("lfPitchAndFamily"), &Globals.lfFont.lfPitchAndFamily);
131 QueryByte(hKey, _T("lfQuality"), &Globals.lfFont.lfQuality);
132 QueryByte(hKey, _T("lfStrikeOut"), &Globals.lfFont.lfStrikeOut);
133 QueryByte(hKey, _T("lfUnderline"), &Globals.lfFont.lfUnderline);
134 QueryDword(hKey, _T("lfWeight"), (DWORD*)&Globals.lfFont.lfWeight);
135 QueryDword(hKey, _T("iPointSize"), &dwPointSize);
136 QueryBool(hKey, _T("fWrap"), &Globals.bWrapLongLines);
137 QueryBool(hKey, _T("fStatusBar"), &Globals.bShowStatusBar);
138
139 QueryDword(hKey, _T("iWindowPosX"), (DWORD*)&Globals.main_rect.left);
140 QueryDword(hKey, _T("iWindowPosY"), (DWORD*)&Globals.main_rect.top);
141 QueryDword(hKey, _T("iWindowPosDX"), (DWORD*)&dx);
142 QueryDword(hKey, _T("iWindowPosDY"), (DWORD*)&dy);
143
144 Globals.main_rect.right = Globals.main_rect.left + dx;
145 Globals.main_rect.bottom = Globals.main_rect.top + dy;
146
147 Globals.bShowStatusBar = !Globals.bShowStatusBar; /* invert value becuase DIALOG_ViewStatusBar will be called to show it*/
148
149 if (dwPointSize != 0)
150 Globals.lfFont.lfHeight = HeightFromPointSize(dwPointSize);
151
152 RegCloseKey(hKey);
153 }
154
155 hFont = CreateFontIndirect(&Globals.lfFont);
156 if (hFont)
157 {
158 if (Globals.hFont)
159 DeleteObject(Globals.hFont);
160 Globals.hFont = hFont;
161 }
162 }
163
164 static BOOL SaveDword(HKEY hKey, LPCTSTR pszValueNameT, DWORD dwValue)
165 {
166 return RegSetValueEx(hKey, pszValueNameT, 0, REG_DWORD, (LPBYTE) &dwValue, sizeof(dwValue)) == ERROR_SUCCESS;
167 }
168
169 static BOOL SaveString(HKEY hKey, LPCTSTR pszValueNameT, LPCTSTR pszValue)
170 {
171 return RegSetValueEx(hKey, pszValueNameT, 0, REG_SZ, (LPBYTE) pszValue, (DWORD) _tcslen(pszValue) * sizeof(*pszValue)) == ERROR_SUCCESS;
172 }
173
174 void SaveSettings(void)
175 {
176 HKEY hKey;
177 DWORD dwDisposition;
178
179 GetWindowRect(Globals.hMainWnd, &Globals.main_rect);
180
181 if (RegCreateKeyEx(HKEY_CURRENT_USER, s_szRegistryKey, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition)
182 == ERROR_SUCCESS)
183 {
184 SaveDword(hKey, _T("lfCharSet"), Globals.lfFont.lfCharSet);
185 SaveDword(hKey, _T("lfClipPrecision"), Globals.lfFont.lfClipPrecision);
186 SaveDword(hKey, _T("lfEscapement"), Globals.lfFont.lfEscapement);
187 SaveString(hKey, _T("lfFaceName"), Globals.lfFont.lfFaceName);
188 SaveDword(hKey, _T("lfItalic"), Globals.lfFont.lfItalic);
189 SaveDword(hKey, _T("lfOrientation"), Globals.lfFont.lfOrientation);
190 SaveDword(hKey, _T("lfOutPrecision"), Globals.lfFont.lfOutPrecision);
191 SaveDword(hKey, _T("lfPitchAndFamily"), Globals.lfFont.lfPitchAndFamily);
192 SaveDword(hKey, _T("lfQuality"), Globals.lfFont.lfQuality);
193 SaveDword(hKey, _T("lfStrikeOut"), Globals.lfFont.lfStrikeOut);
194 SaveDword(hKey, _T("lfUnderline"), Globals.lfFont.lfUnderline);
195 SaveDword(hKey, _T("lfWeight"), Globals.lfFont.lfWeight);
196 SaveDword(hKey, _T("iPointSize"), PointSizeFromHeight(Globals.lfFont.lfHeight));
197 SaveDword(hKey, _T("fWrap"), Globals.bWrapLongLines ? 1 : 0);
198 SaveDword(hKey, _T("fStatusBar"), Globals.bShowStatusBar ? 1 : 0);
199 SaveDword(hKey, _T("iWindowPosX"), Globals.main_rect.left);
200 SaveDword(hKey, _T("iWindowPosY"), Globals.main_rect.top);
201 SaveDword(hKey, _T("iWindowPosDX"), Globals.main_rect.right - Globals.main_rect.left);
202 SaveDword(hKey, _T("iWindowPosDY"), Globals.main_rect.bottom - Globals.main_rect.top);
203 RegCloseKey(hKey);
204 }
205
206 }