ReactOS  0.4.12-dev-14-gd0c8636
settings.c
Go to the documentation of this file.
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 #include <winreg.h>
26 
27 static LPCTSTR s_szRegistryKey = _T("Software\\Microsoft\\Notepad");
28 
29 
30 static LONG HeightFromPointSize(DWORD dwPointSize)
31 {
32  LONG lHeight;
33  HDC hDC;
34 
35  hDC = GetDC(NULL);
36  lHeight = -MulDiv(dwPointSize, GetDeviceCaps(hDC, LOGPIXELSY), 720);
37  ReleaseDC(NULL, hDC);
38 
39  return lHeight;
40 }
41 
43 {
44  DWORD dwPointSize;
45  HDC hDC;
46 
47  hDC = GetDC(NULL);
48  dwPointSize = -MulDiv(lHeight, 720, GetDeviceCaps(hDC, LOGPIXELSY));
49  ReleaseDC(NULL, hDC);
50 
51  /* round to nearest multiple of 10 */
52  dwPointSize += 5;
53  dwPointSize -= dwPointSize % 10;
54 
55  return dwPointSize;
56 }
57 
58 static BOOL
59 QueryGeneric(HKEY hKey, LPCTSTR pszValueNameT, DWORD dwExpectedType,
60  LPVOID pvResult, DWORD dwResultSize)
61 {
62  DWORD dwType, cbData;
63  LPVOID *pTemp = _alloca(dwResultSize);
64 
65  ZeroMemory(pTemp, dwResultSize);
66 
67  cbData = dwResultSize;
68  if (RegQueryValueEx(hKey, pszValueNameT, NULL, &dwType, (LPBYTE) pTemp, &cbData) != ERROR_SUCCESS)
69  return FALSE;
70 
71  if (dwType != dwExpectedType)
72  return FALSE;
73 
74  memcpy(pvResult, pTemp, cbData);
75  return TRUE;
76 }
77 
78 static BOOL QueryDword(HKEY hKey, LPCTSTR pszValueName, DWORD *pdwResult)
79 {
80  return QueryGeneric(hKey, pszValueName, REG_DWORD, pdwResult, sizeof(*pdwResult));
81 }
82 
83 static BOOL QueryByte(HKEY hKey, LPCTSTR pszValueName, BYTE *pbResult)
84 {
85  DWORD dwResult;
86  if (!QueryGeneric(hKey, pszValueName, REG_DWORD, &dwResult, sizeof(dwResult)))
87  return FALSE;
88  if (dwResult >= 0x100)
89  return FALSE;
90  *pbResult = (BYTE) dwResult;
91  return TRUE;
92 }
93 
94 static BOOL QueryBool(HKEY hKey, LPCTSTR pszValueName, BOOL *pbResult)
95 {
96  DWORD dwResult;
97  if (!QueryDword(hKey, pszValueName, &dwResult))
98  return FALSE;
99  *pbResult = dwResult ? TRUE : FALSE;
100  return TRUE;
101 }
102 
103 static BOOL QueryString(HKEY hKey, LPCTSTR pszValueName, LPTSTR pszResult, DWORD dwResultSize)
104 {
105  return QueryGeneric(hKey, pszValueName, REG_SZ, pszResult, dwResultSize * sizeof(TCHAR));
106 }
107 
108 /***********************************************************************
109  *
110  * NOTEPAD_LoadSettingsFromRegistry
111  *
112  * Load settings from registry HKCU\Software\Microsoft\Notepad.
113  */
115 {
116  HKEY hKey = NULL;
117  HFONT hFont;
118  DWORD dwPointSize = 0;
119  INT base_length, dx, dy;
120 
123 
124  dx = (INT)(base_length * .95);
125  dy = dx * 3 / 4;
126  SetRect(&Globals.main_rect, 0, 0, dx, dy);
127 
129  {
130  QueryByte(hKey, _T("lfCharSet"), &Globals.lfFont.lfCharSet);
131  QueryByte(hKey, _T("lfClipPrecision"), &Globals.lfFont.lfClipPrecision);
132  QueryDword(hKey, _T("lfEscapement"), (DWORD*)&Globals.lfFont.lfEscapement);
133  QueryString(hKey, _T("lfFaceName"), Globals.lfFont.lfFaceName, ARRAY_SIZE(Globals.lfFont.lfFaceName));
134  QueryByte(hKey, _T("lfItalic"), &Globals.lfFont.lfItalic);
135  QueryDword(hKey, _T("lfOrientation"), (DWORD*)&Globals.lfFont.lfOrientation);
136  QueryByte(hKey, _T("lfOutPrecision"), &Globals.lfFont.lfOutPrecision);
137  QueryByte(hKey, _T("lfPitchAndFamily"), &Globals.lfFont.lfPitchAndFamily);
138  QueryByte(hKey, _T("lfQuality"), &Globals.lfFont.lfQuality);
139  QueryByte(hKey, _T("lfStrikeOut"), &Globals.lfFont.lfStrikeOut);
140  QueryByte(hKey, _T("lfUnderline"), &Globals.lfFont.lfUnderline);
141  QueryDword(hKey, _T("lfWeight"), (DWORD*)&Globals.lfFont.lfWeight);
142  QueryDword(hKey, _T("iPointSize"), &dwPointSize);
143  QueryBool(hKey, _T("fWrap"), &Globals.bWrapLongLines);
144  QueryBool(hKey, _T("fStatusBar"), &Globals.bShowStatusBar);
145  QueryString(hKey, _T("szHeader"), Globals.szHeader, ARRAY_SIZE(Globals.szHeader));
146  QueryString(hKey, _T("szTrailer"), Globals.szFooter, ARRAY_SIZE(Globals.szFooter));
147  QueryDword(hKey, _T("iMarginLeft"), (DWORD*)&Globals.lMargins.left);
148  QueryDword(hKey, _T("iMarginTop"), (DWORD*)&Globals.lMargins.top);
149  QueryDword(hKey, _T("iMarginRight"), (DWORD*)&Globals.lMargins.right);
150  QueryDword(hKey, _T("iMarginBottom"), (DWORD*)&Globals.lMargins.bottom);
151 
152  QueryDword(hKey, _T("iWindowPosX"), (DWORD*)&Globals.main_rect.left);
153  QueryDword(hKey, _T("iWindowPosY"), (DWORD*)&Globals.main_rect.top);
154  QueryDword(hKey, _T("iWindowPosDX"), (DWORD*)&dx);
155  QueryDword(hKey, _T("iWindowPosDY"), (DWORD*)&dy);
156 
157  Globals.main_rect.right = Globals.main_rect.left + dx;
158  Globals.main_rect.bottom = Globals.main_rect.top + dy;
159 
160  /* invert value because DIALOG_ViewStatusBar will be called to show it */
161  Globals.bShowStatusBar = !Globals.bShowStatusBar;
162 
163  if (dwPointSize != 0)
164  Globals.lfFont.lfHeight = HeightFromPointSize(dwPointSize);
165  else
166  Globals.lfFont.lfHeight = HeightFromPointSize(100);
167 
168  RegCloseKey(hKey);
169  }
170  else
171  {
172  /* If no settings are found in the registry, then use default values */
173  Globals.bShowStatusBar = FALSE;
174  Globals.bWrapLongLines = FALSE;
175  SetRect(&Globals.lMargins, 750, 1000, 750, 1000);
176 
177  /* FIXME: Globals.fSaveWindowPositions = FALSE; */
178  /* FIXME: Globals.fMLE_is_broken = FALSE; */
179 
181  ARRAY_SIZE(Globals.szHeader));
183  ARRAY_SIZE(Globals.szFooter));
184 
185  ZeroMemory(&Globals.lfFont, sizeof(Globals.lfFont));
186  Globals.lfFont.lfCharSet = ANSI_CHARSET;
187  Globals.lfFont.lfClipPrecision = CLIP_STROKE_PRECIS;
188  Globals.lfFont.lfEscapement = 0;
189  _tcscpy(Globals.lfFont.lfFaceName, _T("Lucida Console"));
190  Globals.lfFont.lfItalic = FALSE;
191  Globals.lfFont.lfOrientation = 0;
192  Globals.lfFont.lfOutPrecision = OUT_STRING_PRECIS;
193  Globals.lfFont.lfPitchAndFamily = FIXED_PITCH | FF_MODERN;
194  Globals.lfFont.lfQuality = PROOF_QUALITY;
195  Globals.lfFont.lfStrikeOut = FALSE;
196  Globals.lfFont.lfUnderline = FALSE;
197  Globals.lfFont.lfWeight = FW_NORMAL;
198  Globals.lfFont.lfHeight = HeightFromPointSize(100);
199  }
200 
201  hFont = CreateFontIndirect(&Globals.lfFont);
202  SendMessage(Globals.hEdit, WM_SETFONT, (WPARAM)hFont, (LPARAM)TRUE);
203  if (hFont)
204  {
205  if (Globals.hFont)
206  DeleteObject(Globals.hFont);
207  Globals.hFont = hFont;
208  }
209 }
210 
211 static BOOL SaveDword(HKEY hKey, LPCTSTR pszValueNameT, DWORD dwValue)
212 {
213  return RegSetValueEx(hKey, pszValueNameT, 0, REG_DWORD, (LPBYTE) &dwValue, sizeof(dwValue)) == ERROR_SUCCESS;
214 }
215 
216 static BOOL SaveString(HKEY hKey, LPCTSTR pszValueNameT, LPCTSTR pszValue)
217 {
218  return RegSetValueEx(hKey, pszValueNameT, 0, REG_SZ, (LPBYTE) pszValue, (DWORD) _tcslen(pszValue) * sizeof(*pszValue)) == ERROR_SUCCESS;
219 }
220 
221 /***********************************************************************
222  *
223  * NOTEPAD_SaveSettingsToRegistry
224  *
225  * Save settings to registry HKCU\Software\Microsoft\Notepad.
226  */
228 {
229  HKEY hKey;
230  DWORD dwDisposition;
231 
232  GetWindowRect(Globals.hMainWnd, &Globals.main_rect);
233 
235  0, NULL, 0, KEY_SET_VALUE, NULL,
236  &hKey, &dwDisposition) == ERROR_SUCCESS)
237  {
238  SaveDword(hKey, _T("lfCharSet"), Globals.lfFont.lfCharSet);
239  SaveDword(hKey, _T("lfClipPrecision"), Globals.lfFont.lfClipPrecision);
240  SaveDword(hKey, _T("lfEscapement"), Globals.lfFont.lfEscapement);
241  SaveString(hKey, _T("lfFaceName"), Globals.lfFont.lfFaceName);
242  SaveDword(hKey, _T("lfItalic"), Globals.lfFont.lfItalic);
243  SaveDword(hKey, _T("lfOrientation"), Globals.lfFont.lfOrientation);
244  SaveDword(hKey, _T("lfOutPrecision"), Globals.lfFont.lfOutPrecision);
245  SaveDword(hKey, _T("lfPitchAndFamily"), Globals.lfFont.lfPitchAndFamily);
246  SaveDword(hKey, _T("lfQuality"), Globals.lfFont.lfQuality);
247  SaveDword(hKey, _T("lfStrikeOut"), Globals.lfFont.lfStrikeOut);
248  SaveDword(hKey, _T("lfUnderline"), Globals.lfFont.lfUnderline);
249  SaveDword(hKey, _T("lfWeight"), Globals.lfFont.lfWeight);
250  SaveDword(hKey, _T("iPointSize"), PointSizeFromHeight(Globals.lfFont.lfHeight));
251  SaveDword(hKey, _T("fWrap"), Globals.bWrapLongLines ? 1 : 0);
252  SaveDword(hKey, _T("fStatusBar"), Globals.bShowStatusBar ? 1 : 0);
253  SaveString(hKey, _T("szHeader"), Globals.szHeader);
254  SaveString(hKey, _T("szTrailer"), Globals.szFooter);
255  SaveDword(hKey, _T("iMarginLeft"), Globals.lMargins.left);
256  SaveDword(hKey, _T("iMarginTop"), Globals.lMargins.top);
257  SaveDword(hKey, _T("iMarginRight"), Globals.lMargins.right);
258  SaveDword(hKey, _T("iMarginBottom"), Globals.lMargins.bottom);
259  SaveDword(hKey, _T("iWindowPosX"), Globals.main_rect.left);
260  SaveDword(hKey, _T("iWindowPosY"), Globals.main_rect.top);
261  SaveDword(hKey, _T("iWindowPosDX"), Globals.main_rect.right - Globals.main_rect.left);
262  SaveDword(hKey, _T("iWindowPosDY"), Globals.main_rect.bottom - Globals.main_rect.top);
263 
264  RegCloseKey(hKey);
265  }
266 }
static BOOL QueryByte(HKEY hKey, LPCTSTR pszValueName, BYTE *pbResult)
Definition: settings.c:83
#define RegQueryValueEx
Definition: winreg.h:524
#define HDC
Definition: msvc.h:22
void NOTEPAD_SaveSettingsToRegistry(void)
Definition: settings.c:227
#define TRUE
Definition: types.h:120
const CHAR * LPCTSTR
Definition: xmlstorage.h:193
#define ERROR_SUCCESS
Definition: deptool.c:10
#define KEY_SET_VALUE
Definition: nt_native.h:1017
HDC WINAPI GetDC(_In_opt_ HWND)
#define HKEY_CURRENT_USER
Definition: winreg.h:11
_TCHAR * _tcscpy(_TCHAR *to, const _TCHAR *from)
Definition: tcscpy.h:8
#define ZeroMemory
Definition: winbase.h:1635
BOOL WINAPI DeleteObject(_In_ HGDIOBJ)
#define PROOF_QUALITY
Definition: wingdi.h:436
int32_t INT
Definition: typedefs.h:56
HFONT hFont
Definition: main.c:53
CHAR * LPTSTR
Definition: xmlstorage.h:192
#define STRING_PAGESETUP_HEADERVALUE
Definition: notepad_res.h:67
UINT_PTR WPARAM
Definition: windef.h:207
local int base_length[LENGTH_CODES]
Definition: trees.c:107
CLIPBOARD_GLOBALS Globals
Definition: clipbrd.c:13
#define SM_CXSCREEN
Definition: winuser.h:949
LONG WINAPI RegCloseKey(HKEY hKey)
Definition: reg.c:423
unsigned char * LPBYTE
Definition: typedefs.h:52
long LONG
Definition: pedump.c:60
LONG_PTR LPARAM
Definition: windef.h:208
#define FIXED_PITCH
Definition: wingdi.h:442
int WINAPI ReleaseDC(_In_opt_ HWND, _In_ HDC)
#define OUT_STRING_PRECIS
Definition: wingdi.h:414
#define INT(a)
Definition: assyntax.h:463
size_t __cdecl _tcslen(const _TCHAR *str)
Definition: tcslen.h:9
smooth NULL
Definition: ftsmooth.c:416
static BOOL QueryBool(HKEY hKey, LPCTSTR pszValueName, BOOL *pbResult)
Definition: settings.c:94
static BOOL QueryGeneric(HKEY hKey, LPCTSTR pszValueNameT, DWORD dwExpectedType, LPVOID pvResult, DWORD dwResultSize)
Definition: settings.c:59
char TCHAR
Definition: xmlstorage.h:189
#define SM_CYSCREEN
Definition: winuser.h:950
#define _T(x)
Definition: vfdio.h:22
static BOOL QueryDword(HKEY hKey, LPCTSTR pszValueName, DWORD *pdwResult)
Definition: settings.c:78
unsigned int BOOL
Definition: ntddk_ex.h:94
static BOOL QueryString(HKEY hKey, LPCTSTR pszValueName, LPTSTR pszResult, DWORD dwResultSize)
Definition: settings.c:103
#define STRING_PAGESETUP_FOOTERVALUE
Definition: notepad_res.h:68
int WINAPI GetSystemMetrics(_In_ int)
unsigned long DWORD
Definition: ntddk_ex.h:95
static BOOL SaveDword(HKEY hKey, LPCTSTR pszValueNameT, DWORD dwValue)
Definition: settings.c:211
static LPCTSTR s_szRegistryKey
Definition: settings.c:27
void NOTEPAD_LoadSettingsFromRegistry(void)
Definition: settings.c:114
int WINAPI GetDeviceCaps(_In_opt_ HDC, _In_ int)
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
static DWORD PointSizeFromHeight(LONG lHeight)
Definition: settings.c:42
unsigned char BYTE
Definition: ntddk_ex.h:96
static HDC hDC
Definition: 3dtext.c:33
BOOL WINAPI SetRect(_Out_ LPRECT, _In_ int, _In_ int, _In_ int, _In_ int)
INT INT dy
Definition: msvc.h:65
_In_ UINT _In_ DWORD _Out_ DWORD * pdwResult
Definition: ntgdi.h:248
#define ARRAY_SIZE(a)
Definition: main.h:24
#define FF_MODERN
Definition: wingdi.h:447
#define CreateFontIndirect
Definition: wingdi.h:4421
#define WM_SETFONT
Definition: winuser.h:1632
#define FW_NORMAL
Definition: wingdi.h:371
static DWORD *static HFONT(WINAPI *pCreateFontIndirectExA)(const ENUMLOGFONTEXDVA *)
#define SendMessage
Definition: winuser.h:5709
#define LOGPIXELSY
Definition: wingdi.h:717
BOOL WINAPI GetWindowRect(_In_ HWND, _Out_ LPRECT)
static LONG HeightFromPointSize(DWORD dwPointSize)
Definition: settings.c:30
#define ANSI_CHARSET
Definition: wingdi.h:381
HINSTANCE hInstance
Definition: precomp.h:43
static BOOL SaveString(HKEY hKey, LPCTSTR pszValueNameT, LPCTSTR pszValue)
Definition: settings.c:216
#define RegOpenKey
Definition: winreg.h:519
#define REG_DWORD
Definition: sdbapi.c:539
#define CLIP_STROKE_PRECIS
Definition: wingdi.h:426
#define MulDiv(x, y, z)
Definition: gdifloat.h:86
#define RegSetValueEx
Definition: winreg.h:533
#define RegCreateKeyEx
Definition: winreg.h:501
#define LoadString
Definition: winuser.h:5685
#define REG_SZ
Definition: layer.c:22
INT dx
Definition: msvc.h:65