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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 static LPCTSTR s_szRegistryKey
= _T("Software\\Microsoft\\Notepad");
28 static LONG
HeightFromPointSize(DWORD dwPointSize
)
34 lHeight
= -MulDiv(dwPointSize
, GetDeviceCaps(hDC
, LOGPIXELSY
), 720);
40 static DWORD
PointSizeFromHeight(LONG lHeight
)
46 dwPointSize
= -MulDiv(lHeight
, 720, GetDeviceCaps(hDC
, LOGPIXELSY
));
49 /* round to nearest multiple of 10 */
51 dwPointSize
-= dwPointSize
% 10;
56 static BOOL
QueryGeneric(HKEY hKey
, LPCTSTR pszValueNameT
, DWORD dwExpectedType
,
57 LPVOID pvResult
, DWORD dwResultSize
)
60 LPVOID
*pTemp
= _alloca(dwResultSize
);
62 ZeroMemory(pTemp
, dwResultSize
);
64 cbData
= dwResultSize
;
65 if (RegQueryValueEx(hKey
, pszValueNameT
, NULL
, &dwType
, (LPBYTE
) pTemp
, &cbData
) != ERROR_SUCCESS
)
68 if (dwType
!= dwExpectedType
)
71 memcpy(pvResult
, pTemp
, cbData
);
75 static BOOL
QueryDword(HKEY hKey
, LPCTSTR pszValueName
, DWORD
*pdwResult
)
77 return QueryGeneric(hKey
, pszValueName
, REG_DWORD
, pdwResult
, sizeof(*pdwResult
));
80 static BOOL
QueryByte(HKEY hKey
, LPCTSTR pszValueName
, BYTE
*pbResult
)
83 if (!QueryGeneric(hKey
, pszValueName
, REG_DWORD
, &dwResult
, sizeof(dwResult
)))
85 if (dwResult
>= 0x100)
87 *pbResult
= (BYTE
) dwResult
;
91 static BOOL
QueryBool(HKEY hKey
, LPCTSTR pszValueName
, BOOL
*pbResult
)
94 if (!QueryDword(hKey
, pszValueName
, &dwResult
))
96 *pbResult
= dwResult
? TRUE
: FALSE
;
100 static BOOL
QueryString(HKEY hKey
, LPCTSTR pszValueName
, LPTSTR pszResult
, DWORD dwResultSize
)
102 return QueryGeneric(hKey
, pszValueName
, REG_SZ
, pszResult
, dwResultSize
* sizeof(TCHAR
));
105 void LoadSettings(void)
109 DWORD dwPointSize
= 0;
110 INT base_length
, dx
, dy
;
112 base_length
= (GetSystemMetrics(SM_CXSCREEN
) > GetSystemMetrics(SM_CYSCREEN
))?
113 GetSystemMetrics(SM_CYSCREEN
) : GetSystemMetrics(SM_CXSCREEN
);
115 dx
= base_length
* .95;
117 SetRect( &Globals
.main_rect
, 0, 0, dx
, dy
);
119 if (RegOpenKey(HKEY_CURRENT_USER
, s_szRegistryKey
, &hKey
) == ERROR_SUCCESS
)
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
);
137 QueryByte(hKey
, _T("iWindowPosX"), (LPBYTE
)&Globals
.main_rect
.left
);
138 QueryByte(hKey
, _T("iWindowPosX"), (LPBYTE
)&Globals
.main_rect
.top
);
139 QueryByte(hKey
, _T("iWindowPosDX"), (LPBYTE
)&dx
);
140 QueryByte(hKey
, _T("iWindowPosDY"), (LPBYTE
)&dy
);
142 Globals
.main_rect
.right
= Globals
.main_rect
.left
+ dx
;
143 Globals
.main_rect
.bottom
= Globals
.main_rect
.top
+ dy
;
145 Globals
.bShowStatusBar
= !Globals
.bShowStatusBar
; /* invert value becuase DIALOG_ViewStatusBar will be called to show it*/
147 if (dwPointSize
!= 0)
148 Globals
.lfFont
.lfHeight
= HeightFromPointSize(dwPointSize
);
150 hFont
= CreateFontIndirect(&Globals
.lfFont
);
154 DeleteObject(Globals
.hFont
);
155 Globals
.hFont
= hFont
;
162 static BOOL
SaveDword(HKEY hKey
, LPCTSTR pszValueNameT
, DWORD dwValue
)
164 return RegSetValueEx(hKey
, pszValueNameT
, 0, REG_DWORD
, (LPBYTE
) &dwValue
, sizeof(dwValue
)) == ERROR_SUCCESS
;
167 static BOOL
SaveString(HKEY hKey
, LPCTSTR pszValueNameT
, LPCTSTR pszValue
)
169 return RegSetValueEx(hKey
, pszValueNameT
, 0, REG_SZ
, (LPBYTE
) pszValue
, (DWORD
) _tcslen(pszValue
) * sizeof(*pszValue
)) == ERROR_SUCCESS
;
172 void SaveSettings(void)
177 GetWindowRect(Globals
.hMainWnd
, &Globals
.main_rect
);
179 if (RegCreateKeyEx(HKEY_CURRENT_USER
, s_szRegistryKey
, 0, NULL
, 0, KEY_ALL_ACCESS
, NULL
, &hKey
, &dwDisposition
)
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
);