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