/
Settings.cs
339 lines (271 loc) · 9.63 KB
/
Settings.cs
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace EduNotepad
{
public static class Settings
{
// Блокнот хранит настройки в HKCU\Software\Microsoft\Notepad
// Наши настройки в HKCU\Software\EduNotepad
private const string SETTINGS_SUBKEY = "Software\\EduNotepad";
#region Размер окна
private const string REGISTRY_WIDTH = "Width";
private const string REGISTRY_HEIGHT = "Height";
// Размер окна: умолчания
// Высчитываются автоматически из размера экрана
// Размер окна: минимум
private const int DEFAULT_WIDTH_MINIMUM = 400;
private const int DEFAULT_HEIGHT_MINIMUM = 300;
#endregion
// Положение окна
private const string REGISTRY_X = "X";
private const string REGISTRY_Y = "Y";
// Положение окна: умолчания
private const int DEFAULT_X = 42;
private const int DEFAULT_Y = 42;
// Положение окна: минимум
private const int DEFAULT_VISIBLE_X = 100;
private const int DEFAULT_VISIBLE_Y = 100;
// Шрифт
private const string REGISTRY_FONT = "Font";
// Шрифт: умолчания
private const string DEFAULT_FONT = "Lucida Console; 9,75pt";
// Перенос по словам
private const string REGISTRY_WRAP = "Wrap";
// Перенос по словам: умолчания
// Функция для чтения вернет false, если нет ключа в реестре
// Строка состояния
private const string REGISTRY_STATUSBAR = "StatusBar";
// Строка состояния: умолчания
// Функция для чтения вернет false, если нет ключа в реестре
// Отступ слева
private const string REGISTRY_MARGIN_LEFT = "MarginLeft";
// Отступ слева: умолчания
private const int DEFAULT_MARGIN_LEFT = 200;
// Отступ справа
private const string REGISTRY_MARGIN_RIGHT = "MarginRight";
// Отступ справа: умолчания
private const int DEFAULT_MARGIN_RIGHT = 200;
// Отступ сверху
private const string REGISTRY_MARGIN_TOP = "MarginTop";
// Отступ сверху: умолчания
private const int DEFAULT_MARGIN_TOP = 250;
// Отступ снизу
private const string REGISTRY_MARGIN_BOTTOM = "MarginBottom";
// Отступ снизу: умолчания
private const int DEFAULT_MARGIN_BOTTOM = 250;
private static RegistryKey CreateOrOpenSettingsSubKey()
{
RegistryKey returnValue = null;
RegistryKey keyCurrentUser = Registry.CurrentUser;
try
{
returnValue = keyCurrentUser.CreateSubKey(SETTINGS_SUBKEY);
}
catch (Exception e)
{
Debug.Print(e.Message);
returnValue = null;
}
return returnValue;
}
private static T ReadValue<T>(string valueName, out bool success)
{
success = false;
object returnValue = null;
RegistryValueKind rvk = RegistryValueKind.Unknown;
if (typeof(T) == typeof(string))
{
// Пользователь хочет прочитать настройку типа string
returnValue = string.Empty;
rvk = RegistryValueKind.String;
}
else if (typeof(T) == typeof(int))
{
// Пользователь хочет прочитать настройку типа int
returnValue = 0;
rvk = RegistryValueKind.DWord;
}
else if (typeof(T) == typeof(bool))
{
// Пользователь хочет прочитать настройку типа bool
returnValue = false;
rvk = RegistryValueKind.DWord;
}
RegistryKey keyEdu = Settings.CreateOrOpenSettingsSubKey();
try
{
if (keyEdu != null
&& keyEdu.GetValueNames().Contains(valueName, StringComparer.OrdinalIgnoreCase)
&& keyEdu.GetValueKind(valueName) == rvk)
{
returnValue = keyEdu.GetValue(valueName);
success = true;
}
}
catch (Exception e)
{
Debug.Print(e.Message);
}
if (typeof(T) == typeof(bool))
{
returnValue = returnValue.ToString() == "1" ? (object)true : (object)false;
}
return (T)(object)returnValue;
}
private static void WriteValue(string valueName, object value)
{
RegistryValueKind rvk = RegistryValueKind.Unknown;
if (value is Int32 || value is Boolean)
{
rvk = RegistryValueKind.DWord;
}
else if (value is String)
{
rvk = RegistryValueKind.String;
}
if (rvk != RegistryValueKind.Unknown)
{
// Нам передали известное нам значение, мы знаем, как его обработать
RegistryKey keyEdu = CreateOrOpenSettingsSubKey();
if (keyEdu != null)
{
// Ключ реестра корректно открылся
try
{
keyEdu.SetValue(valueName, value, rvk);
}
catch (Exception e) { Debug.WriteLine(e.Message); }
}
}
}
public static Size MinimumWindowSize
{
get
{
return new Size(DEFAULT_WIDTH_MINIMUM, DEFAULT_HEIGHT_MINIMUM);
}
}
public static Size WindowSize
{
get
{
bool success;
double widthRatio = 0.75;
double heightRatio = 0.7;
Size returnValue = new Size(0, 0);
returnValue.Width = ReadValue<int>(REGISTRY_WIDTH, out success);
returnValue.Width = success ? returnValue.Width : (int)(Screen.PrimaryScreen.Bounds.Width * widthRatio);
returnValue.Width = returnValue.Width < Screen.PrimaryScreen.WorkingArea.Width ? returnValue.Width : Screen.PrimaryScreen.WorkingArea.Width;
returnValue.Width = returnValue.Width > DEFAULT_WIDTH_MINIMUM ? returnValue.Width : DEFAULT_WIDTH_MINIMUM;
int tempHeight = ReadValue<int>(REGISTRY_HEIGHT, out success);
returnValue.Height = success ? tempHeight : (int)(Screen.PrimaryScreen.Bounds.Height * heightRatio);
returnValue.Height = returnValue.Height < Screen.PrimaryScreen.WorkingArea.Height ? returnValue.Height : Screen.PrimaryScreen.WorkingArea.Height;
returnValue.Height = returnValue.Height > DEFAULT_HEIGHT_MINIMUM ? returnValue.Height : DEFAULT_HEIGHT_MINIMUM;
return returnValue;
}
set
{
WriteValue(REGISTRY_WIDTH, value.Width);
WriteValue(REGISTRY_HEIGHT, value.Height);
}
}
public static Point WindowLocation
{
get
{
Point returnValue = new Point(0, 0);
bool success;
int maximumX = Screen.PrimaryScreen.WorkingArea.Width - DEFAULT_VISIBLE_X;
int maximumY = Screen.PrimaryScreen.WorkingArea.Height - DEFAULT_VISIBLE_Y;
returnValue.X = ReadValue<int>(REGISTRY_X, out success);
returnValue.X = success ? returnValue.X : DEFAULT_X;
returnValue.X = returnValue.X < maximumX ? returnValue.X : maximumX;
returnValue.Y = ReadValue<int>(REGISTRY_Y, out success);
returnValue.Y = success ? returnValue.Y : DEFAULT_Y;
returnValue.Y = returnValue.Y < maximumY ? returnValue.Y : maximumY;
return returnValue;
}
set
{
WriteValue(REGISTRY_X, value.X);
WriteValue(REGISTRY_Y, value.Y);
}
}
public static Font TextBoxFont
{
get
{
bool success;
Font returnValue = null;
string stringFont = ReadValue<string>(REGISTRY_FONT, out success);
stringFont = success ? stringFont : DEFAULT_FONT;
returnValue = (Font)(new FontConverter()).ConvertFromString(stringFont);
return returnValue;
}
set
{
WriteValue(REGISTRY_FONT, (new FontConverter()).ConvertToString(value));
}
}
public static bool WordWrap
{
get
{
bool success;
return ReadValue<bool>(REGISTRY_WRAP, out success);
}
set
{
WriteValue(REGISTRY_WRAP, value);
}
}
public static bool StatusBar
{
get
{
bool success;
return ReadValue<bool>(REGISTRY_STATUSBAR, out success);
}
set
{
WriteValue(REGISTRY_STATUSBAR, value);
}
}
private static PageSettings _pageSettings = null;
public static PageSettings PageSettings
{
get
{
bool success;
// Суем стандартные настройки
if (_pageSettings == null) _pageSettings = new PageSettings();
// Читаем наши значения из реестра
_pageSettings.Margins.Left = ReadValue<int>(REGISTRY_MARGIN_LEFT, out success);
_pageSettings.Margins.Left = success ? _pageSettings.Margins.Left : DEFAULT_MARGIN_LEFT;
_pageSettings.Margins.Right = ReadValue<int>(REGISTRY_MARGIN_RIGHT, out success);
_pageSettings.Margins.Right = success ? _pageSettings.Margins.Right : DEFAULT_MARGIN_RIGHT;
_pageSettings.Margins.Top = ReadValue<int>(REGISTRY_MARGIN_TOP, out success);
_pageSettings.Margins.Top = success ? _pageSettings.Margins.Top : DEFAULT_MARGIN_TOP;
_pageSettings.Margins.Bottom = ReadValue<int>(REGISTRY_MARGIN_BOTTOM, out success);
_pageSettings.Margins.Bottom = success ? _pageSettings.Margins.Bottom : DEFAULT_MARGIN_BOTTOM;
return _pageSettings;
}
set
{
_pageSettings = value;
// Сохраняем значения отступов
WriteValue(REGISTRY_MARGIN_LEFT, value.Margins.Left);
WriteValue(REGISTRY_MARGIN_RIGHT, value.Margins.Right);
WriteValue(REGISTRY_MARGIN_TOP, value.Margins.Top);
WriteValue(REGISTRY_MARGIN_BOTTOM, value.Margins.Bottom);
}
}
}
}