--- /dev/null
+/*\r
+ * Notepad (settings.c)\r
+ *\r
+ * Copyright 1998,99 Marcel Baur <mbaur@g26.ethz.ch>\r
+ * Copyright 2002 Sylvain Petreolle <spetreolle@yahoo.fr>\r
+ * Copyright 2002 Andriy Palamarchuk\r
+ *\r
+ * This library is free software; you can redistribute it and/or\r
+ * modify it under the terms of the GNU Lesser General Public\r
+ * License as published by the Free Software Foundation; either\r
+ * version 2.1 of the License, or (at your option) any later version.\r
+ *\r
+ * This library is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
+ * Lesser General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU Lesser General Public\r
+ * License along with this library; if not, write to the Free Software\r
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r
+ */\r
+\r
+#define UNICODE\r
+#define _UNICODE\r
+\r
+#include <assert.h>\r
+#include <stdio.h>\r
+#include <windows.h>\r
+#include <commdlg.h>\r
+#include <tchar.h>\r
+\r
+#include "main.h"\r
+\r
+static const TCHAR s_szRegistryKey[] = { 'S','o','f','t','w','a','r','e',\r
+ '\\','M','i','c','r','o','s','o','f','t',\r
+ '\\','N','o','t','e','p','a','d',0 };\r
+\r
+\r
+static LONG HeightFromPointSize(DWORD dwPointSize)\r
+{\r
+ LONG lHeight;\r
+ HDC hDC;\r
+\r
+ hDC = GetDC(NULL);\r
+ lHeight = -MulDiv(dwPointSize, GetDeviceCaps(hDC, LOGPIXELSY), 720);\r
+ ReleaseDC(NULL, hDC);\r
+\r
+ return lHeight;\r
+}\r
+\r
+static DWORD PointSizeFromHeight(LONG lHeight)\r
+{\r
+ DWORD dwPointSize;\r
+ HDC hDC;\r
+\r
+ hDC = GetDC(NULL);\r
+ dwPointSize = -MulDiv(lHeight, 720, GetDeviceCaps(hDC, LOGPIXELSY));\r
+ ReleaseDC(NULL, hDC);\r
+\r
+ /* round to nearest multiple of 10 */\r
+ dwPointSize += 5;\r
+ dwPointSize -= dwPointSize % 10;\r
+\r
+ return dwPointSize;\r
+}\r
+\r
+static BOOL QueryGeneric(HKEY hKey, LPCSTR pszValueName, DWORD dwExpectedType,\r
+ LPVOID pvResult, DWORD dwResultSize)\r
+{\r
+ WCHAR szValueW[32];\r
+ LPCTSTR pszValueNameT;\r
+ DWORD dwType, cbData;\r
+ LPVOID *pTemp;\r
+ BOOL bSuccess = FALSE;\r
+\r
+#ifdef UNICODE\r
+ MultiByteToWideChar(CP_ACP, 0, pszValueName, -1, szValueW, sizeof(szValueW) / sizeof(szValueW[0]));\r
+ pszValueNameT = szValueW;\r
+#else\r
+ pszValueNameT = pszValueName;\r
+#endif\r
+\r
+ pTemp = HeapAlloc(GetProcessHeap(), 0, dwResultSize);\r
+ if (!pTemp)\r
+ goto done;\r
+ memset(pTemp, 0, dwResultSize);\r
+\r
+ cbData = dwResultSize;\r
+ if (RegQueryValueEx(hKey, pszValueNameT, NULL, &dwType, (LPBYTE) pTemp, &cbData) != ERROR_SUCCESS)\r
+ goto done;\r
+\r
+ if (dwType != dwExpectedType)\r
+ goto done;\r
+\r
+ memcpy(pvResult, pTemp, cbData);\r
+ bSuccess = TRUE;\r
+\r
+done:\r
+ if (pTemp)\r
+ HeapFree(GetProcessHeap(), 0, pTemp);\r
+ return bSuccess;\r
+}\r
+\r
+static BOOL QueryDword(HKEY hKey, LPCSTR pszValueName, DWORD *pdwResult)\r
+{\r
+ return QueryGeneric(hKey, pszValueName, REG_DWORD, pdwResult, sizeof(*pdwResult));\r
+}\r
+\r
+static BOOL QueryByte(HKEY hKey, LPCSTR pszValueName, BYTE *pbResult)\r
+{\r
+ DWORD dwResult;\r
+ if (!QueryGeneric(hKey, pszValueName, REG_DWORD, &dwResult, sizeof(dwResult)))\r
+ return FALSE;\r
+ if (dwResult >= 0x100)\r
+ return FALSE;\r
+ *pbResult = (BYTE) dwResult;\r
+ return TRUE;\r
+}\r
+\r
+static BOOL QueryString(HKEY hKey, LPCSTR pszValueName, LPTSTR pszResult, DWORD dwResultSize)\r
+{\r
+ return QueryGeneric(hKey, pszValueName, REG_SZ, pszResult, dwResultSize * sizeof(*pszResult));\r
+}\r
+\r
+void LoadSettings(void)\r
+{\r
+ HKEY hKey = NULL;\r
+ HFONT hFont;\r
+ DWORD dwPointSize = 0;\r
+\r
+ if (RegOpenKey(HKEY_CURRENT_USER, s_szRegistryKey, &hKey) == ERROR_SUCCESS)\r
+ {\r
+ QueryByte(hKey, "lfCharSet", &Globals.lfFont.lfCharSet);\r
+ QueryByte(hKey, "lfClipPrecision", &Globals.lfFont.lfClipPrecision);\r
+ QueryDword(hKey, "lfEscapement", &Globals.lfFont.lfEscapement);\r
+ QueryString(hKey, "lfFaceName", Globals.lfFont.lfFaceName, sizeof(Globals.lfFont.lfFaceName) / sizeof(Globals.lfFont.lfFaceName[0]));\r
+ QueryByte(hKey, "lfItalic", &Globals.lfFont.lfItalic);\r
+ QueryDword(hKey, "lfOrientation", &Globals.lfFont.lfOrientation);\r
+ QueryByte(hKey, "lfOutPrecision", &Globals.lfFont.lfOutPrecision);\r
+ QueryByte(hKey, "lfPitchAndFamily", &Globals.lfFont.lfPitchAndFamily);\r
+ QueryByte(hKey, "lfQuality", &Globals.lfFont.lfQuality);\r
+ QueryByte(hKey, "lfStrikeOut", &Globals.lfFont.lfStrikeOut);\r
+ QueryByte(hKey, "lfUnderline", &Globals.lfFont.lfUnderline);\r
+ QueryDword(hKey, "lfWeight", &Globals.lfFont.lfWeight);\r
+ QueryDword(hKey, "iPointSize", &dwPointSize);\r
+\r
+ if (dwPointSize != 0)\r
+ Globals.lfFont.lfHeight = HeightFromPointSize(dwPointSize);\r
+\r
+ hFont = CreateFontIndirect(&Globals.lfFont);\r
+ if (hFont)\r
+ {\r
+ if (Globals.hFont)\r
+ DeleteObject(Globals.hFont);\r
+ Globals.hFont = hFont;\r
+ }\r
+\r
+ RegCloseKey(hKey);\r
+ }\r
+}\r
+\r
+static BOOL SaveDword(HKEY hKey, LPCSTR pszValueName, DWORD dwValue)\r
+{\r
+ WCHAR szValueW[32];\r
+ LPCTSTR pszValueNameT;\r
+\r
+#ifdef UNICODE\r
+ MultiByteToWideChar(CP_ACP, 0, pszValueName, -1, szValueW, sizeof(szValueW) / sizeof(szValueW[0]));\r
+ pszValueNameT = szValueW;\r
+#else\r
+ pszValueNameT = pszValueName;\r
+#endif\r
+\r
+ return RegSetValueEx(hKey, pszValueNameT, 0, REG_DWORD, (LPBYTE) &dwValue, sizeof(dwValue)) == ERROR_SUCCESS;\r
+}\r
+\r
+static BOOL SaveString(HKEY hKey, LPCSTR pszValueName, LPCTSTR pszValue)\r
+{\r
+ WCHAR szValueW[32];\r
+ LPCTSTR pszValueNameT;\r
+\r
+#ifdef UNICODE\r
+ MultiByteToWideChar(CP_ACP, 0, pszValueName, -1, szValueW, sizeof(szValueW) / sizeof(szValueW[0]));\r
+ pszValueNameT = szValueW;\r
+#else\r
+ pszValueNameT = pszValueName;\r
+#endif\r
+\r
+ return RegSetValueEx(hKey, pszValueNameT, 0, REG_SZ, (LPBYTE) pszValue, _tcslen(pszValue) * sizeof(*pszValue)) == ERROR_SUCCESS;\r
+}\r
+\r
+void SaveSettings(void)\r
+{\r
+ HKEY hKey;\r
+ DWORD dwDisposition;\r
+\r
+ if (RegCreateKeyEx(HKEY_CURRENT_USER, s_szRegistryKey, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition)\r
+ == ERROR_SUCCESS)\r
+ {\r
+ SaveDword(hKey, "lfCharSet", Globals.lfFont.lfCharSet);\r
+ SaveDword(hKey, "lfClipPrecision", Globals.lfFont.lfClipPrecision);\r
+ SaveDword(hKey, "lfEscapement", Globals.lfFont.lfEscapement);\r
+ SaveString(hKey, "lfFaceName", Globals.lfFont.lfFaceName);\r
+ SaveDword(hKey, "lfItalic", Globals.lfFont.lfItalic);\r
+ SaveDword(hKey, "lfOrientation", Globals.lfFont.lfOrientation);\r
+ SaveDword(hKey, "lfOutPrecision", Globals.lfFont.lfOutPrecision);\r
+ SaveDword(hKey, "lfPitchAndFamily", Globals.lfFont.lfPitchAndFamily);\r
+ SaveDword(hKey, "lfQuality", Globals.lfFont.lfQuality);\r
+ SaveDword(hKey, "lfStrikeOut", Globals.lfFont.lfStrikeOut);\r
+ SaveDword(hKey, "lfUnderline", Globals.lfFont.lfUnderline);\r
+ SaveDword(hKey, "lfWeight", Globals.lfFont.lfWeight);\r
+ SaveDword(hKey, "iPointSize", PointSizeFromHeight(Globals.lfFont.lfHeight));\r
+\r
+ RegCloseKey(hKey);\r
+ }\r
+\r
+}\r