4 * Copyright 1998,99 Marcel Baur <mbaur@g26.ethz.ch>
5 * Copyright 2002 Sylvain Petreolle <spetreolle@yahoo.fr>
6 * Copyright 2002 Andriy Palamarchuk
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.
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.
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
27 static LPCTSTR s_szRegistryKey
= _T("Software\\Microsoft\\Notepad");
30 static LONG
HeightFromPointSize(DWORD dwPointSize
)
36 lHeight
= -MulDiv(dwPointSize
, GetDeviceCaps(hDC
, LOGPIXELSY
), 720);
42 static DWORD
PointSizeFromHeight(LONG lHeight
)
48 dwPointSize
= -MulDiv(lHeight
, 720, GetDeviceCaps(hDC
, LOGPIXELSY
));
51 /* round to nearest multiple of 10 */
53 dwPointSize
-= dwPointSize
% 10;
58 static BOOL
QueryGeneric(HKEY hKey
, LPCTSTR pszValueNameT
, DWORD dwExpectedType
,
59 LPVOID pvResult
, DWORD dwResultSize
)
62 LPVOID
*pTemp
= _alloca(dwResultSize
);
64 ZeroMemory(pTemp
, dwResultSize
);
66 cbData
= dwResultSize
;
67 if (RegQueryValueEx(hKey
, pszValueNameT
, NULL
, &dwType
, (LPBYTE
) pTemp
, &cbData
) != ERROR_SUCCESS
)
70 if (dwType
!= dwExpectedType
)
73 memcpy(pvResult
, pTemp
, cbData
);
77 static BOOL
QueryDword(HKEY hKey
, LPCTSTR pszValueName
, DWORD
*pdwResult
)
79 return QueryGeneric(hKey
, pszValueName
, REG_DWORD
, pdwResult
, sizeof(*pdwResult
));
82 static BOOL
QueryByte(HKEY hKey
, LPCTSTR pszValueName
, BYTE
*pbResult
)
85 if (!QueryGeneric(hKey
, pszValueName
, REG_DWORD
, &dwResult
, sizeof(dwResult
)))
87 if (dwResult
>= 0x100)
89 *pbResult
= (BYTE
) dwResult
;
93 static BOOL
QueryBool(HKEY hKey
, LPCTSTR pszValueName
, BOOL
*pbResult
)
96 if (!QueryDword(hKey
, pszValueName
, &dwResult
))
98 *pbResult
= dwResult
? TRUE
: FALSE
;
102 static BOOL
QueryString(HKEY hKey
, LPCTSTR pszValueName
, LPTSTR pszResult
, DWORD dwResultSize
)
104 return QueryGeneric(hKey
, pszValueName
, REG_SZ
, pszResult
, dwResultSize
* sizeof(TCHAR
));
107 void LoadSettings(void)
111 DWORD dwPointSize
= 0;
112 INT base_length
, dx
, dy
;
114 base_length
= (GetSystemMetrics(SM_CXSCREEN
) > GetSystemMetrics(SM_CYSCREEN
))?
115 GetSystemMetrics(SM_CYSCREEN
) : GetSystemMetrics(SM_CXSCREEN
);
117 dx
= (INT
)(base_length
* .95);
119 SetRect( &Globals
.main_rect
, 0, 0, dx
, dy
);
121 if (RegOpenKey(HKEY_CURRENT_USER
, s_szRegistryKey
, &hKey
) == ERROR_SUCCESS
)
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
);
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
);
144 Globals
.main_rect
.right
= Globals
.main_rect
.left
+ dx
;
145 Globals
.main_rect
.bottom
= Globals
.main_rect
.top
+ dy
;
147 Globals
.bShowStatusBar
= !Globals
.bShowStatusBar
; /* invert value becuase DIALOG_ViewStatusBar will be called to show it*/
149 if (dwPointSize
!= 0)
150 Globals
.lfFont
.lfHeight
= HeightFromPointSize(dwPointSize
);
155 hFont
= CreateFontIndirect(&Globals
.lfFont
);
159 DeleteObject(Globals
.hFont
);
160 Globals
.hFont
= hFont
;
164 static BOOL
SaveDword(HKEY hKey
, LPCTSTR pszValueNameT
, DWORD dwValue
)
166 return RegSetValueEx(hKey
, pszValueNameT
, 0, REG_DWORD
, (LPBYTE
) &dwValue
, sizeof(dwValue
)) == ERROR_SUCCESS
;
169 static BOOL
SaveString(HKEY hKey
, LPCTSTR pszValueNameT
, LPCTSTR pszValue
)
171 return RegSetValueEx(hKey
, pszValueNameT
, 0, REG_SZ
, (LPBYTE
) pszValue
, (DWORD
) _tcslen(pszValue
) * sizeof(*pszValue
)) == ERROR_SUCCESS
;
174 void SaveSettings(void)
179 GetWindowRect(Globals
.hMainWnd
, &Globals
.main_rect
);
181 if (RegCreateKeyEx(HKEY_CURRENT_USER
, s_szRegistryKey
, 0, NULL
, 0, KEY_ALL_ACCESS
, NULL
, &hKey
, &dwDisposition
)
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
);