-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsettings.c
268 lines (226 loc) · 9.96 KB
/
settings.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
* PROJECT: ReactOS Notepad
* LICENSE: LGPL-2.1-or-later (https://spdx.org/licenses/LGPL-2.1-or-later)
* PURPOSE: Providing a Windows-compatible simple text editor for ReactOS
* COPYRIGHT: Copyright 1998,99 Marcel Baur <[email protected]>
* Copyright 2002 Sylvain Petreolle <[email protected]>
* Copyright 2002 Andriy Palamarchuk
*/
#include "notepad.h"
#include <winreg.h>
static LPCTSTR s_szRegistryKey = _T("Software\\Microsoft\\Notepad");
static LONG HeightFromPointSize(DWORD dwPointSize)
{
LONG lHeight;
HDC hDC;
hDC = GetDC(NULL);
lHeight = -MulDiv(dwPointSize, GetDeviceCaps(hDC, LOGPIXELSY), 720);
ReleaseDC(NULL, hDC);
return lHeight;
}
static DWORD PointSizeFromHeight(LONG lHeight)
{
DWORD dwPointSize;
HDC hDC;
hDC = GetDC(NULL);
dwPointSize = -MulDiv(lHeight, 720, GetDeviceCaps(hDC, LOGPIXELSY));
ReleaseDC(NULL, hDC);
/* round to nearest multiple of 10 */
dwPointSize += 5;
dwPointSize -= dwPointSize % 10;
return dwPointSize;
}
static BOOL
QueryGeneric(HKEY hKey, LPCTSTR pszValueNameT, DWORD dwExpectedType,
LPVOID pvResult, DWORD dwResultSize)
{
DWORD dwType, cbData;
LPVOID *pTemp = _alloca(dwResultSize);
ZeroMemory(pTemp, dwResultSize);
cbData = dwResultSize;
if (RegQueryValueEx(hKey, pszValueNameT, NULL, &dwType, (LPBYTE) pTemp, &cbData) != ERROR_SUCCESS)
return FALSE;
if (dwType != dwExpectedType)
return FALSE;
memcpy(pvResult, pTemp, cbData);
return TRUE;
}
static BOOL QueryDword(HKEY hKey, LPCTSTR pszValueName, DWORD *pdwResult)
{
return QueryGeneric(hKey, pszValueName, REG_DWORD, pdwResult, sizeof(*pdwResult));
}
static BOOL QueryByte(HKEY hKey, LPCTSTR pszValueName, BYTE *pbResult)
{
DWORD dwResult;
if (!QueryGeneric(hKey, pszValueName, REG_DWORD, &dwResult, sizeof(dwResult)))
return FALSE;
if (dwResult >= 0x100)
return FALSE;
*pbResult = (BYTE) dwResult;
return TRUE;
}
static BOOL QueryBool(HKEY hKey, LPCTSTR pszValueName, BOOL *pbResult)
{
DWORD dwResult;
if (!QueryDword(hKey, pszValueName, &dwResult))
return FALSE;
*pbResult = dwResult ? TRUE : FALSE;
return TRUE;
}
static BOOL QueryString(HKEY hKey, LPCTSTR pszValueName, LPTSTR pszResult, DWORD dwResultLength)
{
if (dwResultLength == 0)
return FALSE;
if (!QueryGeneric(hKey, pszValueName, REG_SZ, pszResult, dwResultLength * sizeof(TCHAR)))
return FALSE;
pszResult[dwResultLength - 1] = 0; /* Avoid buffer overrun */
return TRUE;
}
/***********************************************************************
* NOTEPAD_LoadSettingsFromRegistry
*
* Load settings from registry HKCU\Software\Microsoft\Notepad.
*/
void NOTEPAD_LoadSettingsFromRegistry(void)
{
HKEY hKey;
HFONT hFont;
DWORD dwPointSize, cx, cy;
DWORD cxScreen = GetSystemMetrics(SM_CXSCREEN), cyScreen = GetSystemMetrics(SM_CYSCREEN);
/* Set the default values */
Globals.bShowStatusBar = TRUE;
Globals.bWrapLongLines = FALSE;
SetRect(&Globals.lMargins, 750, 1000, 750, 1000);
ZeroMemory(&Globals.lfFont, sizeof(Globals.lfFont));
Globals.lfFont.lfCharSet = DEFAULT_CHARSET;
dwPointSize = 100;
Globals.lfFont.lfWeight = FW_NORMAL;
Globals.lfFont.lfPitchAndFamily = FIXED_PITCH | FF_MODERN;
Globals.main_rect.left = CW_USEDEFAULT;
Globals.main_rect.top = CW_USEDEFAULT;
cx = min((cxScreen * 3) / 4, 640);
cy = min((cyScreen * 3) / 4, 480);
/* FIXME: Globals.fSaveWindowPositions = FALSE; */
/* FIXME: Globals.fMLE_is_broken = FALSE; */
/* Open the target registry key */
if (RegOpenKey(HKEY_CURRENT_USER, s_szRegistryKey, &hKey) != ERROR_SUCCESS)
hKey = NULL;
/* Load the values from registry */
if (hKey)
{
QueryByte(hKey, _T("lfCharSet"), &Globals.lfFont.lfCharSet);
QueryByte(hKey, _T("lfClipPrecision"), &Globals.lfFont.lfClipPrecision);
QueryDword(hKey, _T("lfEscapement"), (DWORD*)&Globals.lfFont.lfEscapement);
QueryByte(hKey, _T("lfItalic"), &Globals.lfFont.lfItalic);
QueryDword(hKey, _T("lfOrientation"), (DWORD*)&Globals.lfFont.lfOrientation);
QueryByte(hKey, _T("lfOutPrecision"), &Globals.lfFont.lfOutPrecision);
QueryByte(hKey, _T("lfPitchAndFamily"), &Globals.lfFont.lfPitchAndFamily);
QueryByte(hKey, _T("lfQuality"), &Globals.lfFont.lfQuality);
QueryByte(hKey, _T("lfStrikeOut"), &Globals.lfFont.lfStrikeOut);
QueryByte(hKey, _T("lfUnderline"), &Globals.lfFont.lfUnderline);
QueryDword(hKey, _T("lfWeight"), (DWORD*)&Globals.lfFont.lfWeight);
QueryDword(hKey, _T("iPointSize"), &dwPointSize);
QueryBool(hKey, _T("fWrap"), &Globals.bWrapLongLines);
QueryBool(hKey, _T("fStatusBar"), &Globals.bShowStatusBar);
QueryDword(hKey, _T("iMarginLeft"), (DWORD*)&Globals.lMargins.left);
QueryDword(hKey, _T("iMarginTop"), (DWORD*)&Globals.lMargins.top);
QueryDword(hKey, _T("iMarginRight"), (DWORD*)&Globals.lMargins.right);
QueryDword(hKey, _T("iMarginBottom"), (DWORD*)&Globals.lMargins.bottom);
QueryDword(hKey, _T("iWindowPosX"), (DWORD*)&Globals.main_rect.left);
QueryDword(hKey, _T("iWindowPosY"), (DWORD*)&Globals.main_rect.top);
QueryDword(hKey, _T("iWindowPosDX"), &cx);
QueryDword(hKey, _T("iWindowPosDY"), &cy);
QueryString(hKey, _T("searchString"), Globals.szFindText, _countof(Globals.szFindText));
QueryString(hKey, _T("replaceString"), Globals.szReplaceText, _countof(Globals.szReplaceText));
}
Globals.lfFont.lfHeight = HeightFromPointSize(dwPointSize);
Globals.main_rect.right = Globals.main_rect.left + cx;
Globals.main_rect.bottom = Globals.main_rect.top + cy;
if (!hKey || !QueryString(hKey, _T("lfFaceName"),
Globals.lfFont.lfFaceName, _countof(Globals.lfFont.lfFaceName)))
{
LoadString(Globals.hInstance, STRING_DEFAULTFONT, Globals.lfFont.lfFaceName,
_countof(Globals.lfFont.lfFaceName));
}
if (!hKey || !QueryString(hKey, _T("szHeader"), Globals.szHeader, _countof(Globals.szHeader)))
{
LoadString(Globals.hInstance, STRING_PAGESETUP_HEADERVALUE, Globals.szHeader,
_countof(Globals.szHeader));
}
if (!hKey || !QueryString(hKey, _T("szTrailer"), Globals.szFooter, _countof(Globals.szFooter)))
{
LoadString(Globals.hInstance, STRING_PAGESETUP_FOOTERVALUE, Globals.szFooter,
_countof(Globals.szFooter));
}
if (hKey)
RegCloseKey(hKey);
/* WORKAROUND: Far East Asian users may not have suitable fixed-pitch fonts. */
switch (PRIMARYLANGID(GetUserDefaultLangID()))
{
case LANG_CHINESE:
case LANG_JAPANESE:
case LANG_KOREAN:
Globals.lfFont.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
break;
}
hFont = CreateFontIndirect(&Globals.lfFont);
SendMessage(Globals.hEdit, WM_SETFONT, (WPARAM)hFont, TRUE);
if (hFont)
{
if (Globals.hFont)
DeleteObject(Globals.hFont);
Globals.hFont = hFont;
}
}
static BOOL SaveDword(HKEY hKey, LPCTSTR pszValueNameT, DWORD dwValue)
{
return RegSetValueEx(hKey, pszValueNameT, 0, REG_DWORD, (LPBYTE) &dwValue, sizeof(dwValue)) == ERROR_SUCCESS;
}
static BOOL SaveString(HKEY hKey, LPCTSTR pszValueNameT, LPCTSTR pszValue)
{
return RegSetValueEx(hKey, pszValueNameT, 0, REG_SZ, (LPBYTE) pszValue, (DWORD) _tcslen(pszValue) * sizeof(*pszValue)) == ERROR_SUCCESS;
}
/***********************************************************************
* NOTEPAD_SaveSettingsToRegistry
*
* Save settings to registry HKCU\Software\Microsoft\Notepad.
*/
void NOTEPAD_SaveSettingsToRegistry(void)
{
HKEY hKey;
DWORD dwDisposition;
GetWindowRect(Globals.hMainWnd, &Globals.main_rect);
if (RegCreateKeyEx(HKEY_CURRENT_USER, s_szRegistryKey,
0, NULL, 0, KEY_SET_VALUE, NULL,
&hKey, &dwDisposition) == ERROR_SUCCESS)
{
SaveDword(hKey, _T("lfCharSet"), Globals.lfFont.lfCharSet);
SaveDword(hKey, _T("lfClipPrecision"), Globals.lfFont.lfClipPrecision);
SaveDword(hKey, _T("lfEscapement"), Globals.lfFont.lfEscapement);
SaveString(hKey, _T("lfFaceName"), Globals.lfFont.lfFaceName);
SaveDword(hKey, _T("lfItalic"), Globals.lfFont.lfItalic);
SaveDword(hKey, _T("lfOrientation"), Globals.lfFont.lfOrientation);
SaveDword(hKey, _T("lfOutPrecision"), Globals.lfFont.lfOutPrecision);
SaveDword(hKey, _T("lfPitchAndFamily"), Globals.lfFont.lfPitchAndFamily);
SaveDword(hKey, _T("lfQuality"), Globals.lfFont.lfQuality);
SaveDword(hKey, _T("lfStrikeOut"), Globals.lfFont.lfStrikeOut);
SaveDword(hKey, _T("lfUnderline"), Globals.lfFont.lfUnderline);
SaveDword(hKey, _T("lfWeight"), Globals.lfFont.lfWeight);
SaveDword(hKey, _T("iPointSize"), PointSizeFromHeight(Globals.lfFont.lfHeight));
SaveDword(hKey, _T("fWrap"), Globals.bWrapLongLines ? 1 : 0);
SaveDword(hKey, _T("fStatusBar"), Globals.bShowStatusBar ? 1 : 0);
SaveString(hKey, _T("szHeader"), Globals.szHeader);
SaveString(hKey, _T("szTrailer"), Globals.szFooter);
SaveDword(hKey, _T("iMarginLeft"), Globals.lMargins.left);
SaveDword(hKey, _T("iMarginTop"), Globals.lMargins.top);
SaveDword(hKey, _T("iMarginRight"), Globals.lMargins.right);
SaveDword(hKey, _T("iMarginBottom"), Globals.lMargins.bottom);
SaveDword(hKey, _T("iWindowPosX"), Globals.main_rect.left);
SaveDword(hKey, _T("iWindowPosY"), Globals.main_rect.top);
SaveDword(hKey, _T("iWindowPosDX"), Globals.main_rect.right - Globals.main_rect.left);
SaveDword(hKey, _T("iWindowPosDY"), Globals.main_rect.bottom - Globals.main_rect.top);
SaveString(hKey, _T("searchString"), Globals.szFindText);
SaveString(hKey, _T("replaceString"), Globals.szReplaceText);
RegCloseKey(hKey);
}
}