-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptions.cs
More file actions
246 lines (222 loc) · 7.73 KB
/
Copy pathOptions.cs
File metadata and controls
246 lines (222 loc) · 7.73 KB
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
/*
* Copyright (c) 2007-2008 wqNotes Project
* License: BSD
* Windows: Options.cs, $Revision$
* $HeadURL$
* $Date$
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Drawing;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace wqNotes
{
#region Сортировка
public class PropertySorter : ExpandableObjectConverter
{
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}
/// <summary>
/// Возвращает упорядоченный список свойств
/// </summary>
public override PropertyDescriptorCollection GetProperties(
ITypeDescriptorContext context, object value, Attribute[] attributes)
{
PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(value, attributes);
ArrayList orderedProperties = new ArrayList();
foreach (PropertyDescriptor pd in pdc)
{
Attribute attribute = pd.Attributes[typeof(PropertyOrderAttribute)];
if (attribute != null)
{
// атрибут есть - используем номер п/п из него
PropertyOrderAttribute poa = (PropertyOrderAttribute)attribute;
orderedProperties.Add(new PropertyOrderPair(pd.Name, poa.Order));
}
else
{
// атрибута нет - считаем что 0
orderedProperties.Add(new PropertyOrderPair(pd.Name, 0));
}
}
// сортируем по Order-у
orderedProperties.Sort();
// формируем список имен свойств
ArrayList propertyNames = new ArrayList();
foreach (PropertyOrderPair pop in orderedProperties)
{
propertyNames.Add(pop.Name);
}
// возвращаем
return pdc.Sort((string[])propertyNames.ToArray(typeof(string)));
}
}
/// <summary>
/// Атрибут для задания сортировки
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public class PropertyOrderAttribute : Attribute
{
private int _order;
public PropertyOrderAttribute(int order)
{
_order = order;
}
public int Order
{
get { return _order; }
}
}
/// <summary>
/// Пара имя/номер п/п с сортировкой по номеру
/// </summary>
public class PropertyOrderPair : IComparable
{
private int _order;
private string _name;
public string Name
{
get { return _name; }
}
public PropertyOrderPair(string name, int order)
{
_order = order;
_name = name;
}
/// <summary>
/// Собственно метод сравнения
/// </summary>
public int CompareTo(object obj)
{
int otherOrder = ((PropertyOrderPair)obj)._order;
if (otherOrder == _order)
{
// если Order одинаковый - сортируем по именам
string otherName = ((PropertyOrderPair)obj)._name;
return string.Compare(_name, otherName);
}
else if (otherOrder > _order)
{
return -1;
}
return 1;
}
}
#endregion
#region Преобразования boolean
interface IBooleanString
{
string True { get; }
string False { get; }
}
class BooleanYesNo : IBooleanString
{
public string True
{
get { return "Да"; }
}
public string False
{
get { return "Нет"; }
}
}
class BooleanTypeConverter<T> : BooleanConverter where T : IBooleanString, new()
{
private T bclass = new T();
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destType)
{
return (bool)value ? bclass.True : bclass.False;
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
return (string)value == bclass.True;
}
}
#endregion
[Serializable]
[TypeConverter(typeof(PropertySorter))]
public class Options
{
public Options() { }
public Options Clone()
{
IFormatter serializer = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
serializer.Serialize(stream, this);
stream.Seek(0, SeekOrigin.Begin);
return (Options)serializer.Deserialize(stream);
}
static public Options Load()
{
if (Properties.Settings.Default.GeneralOptions == "")
return new Options();
Options opt;
try
{
IFormatter serializer = new BinaryFormatter();
MemoryStream stream = new MemoryStream(
Convert.FromBase64String(
Properties.Settings.Default.GeneralOptions));
opt = (Options)serializer.Deserialize(stream);
}
catch { opt = new Options(); }
return opt;
}
public void Save()
{
IFormatter serializer = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
serializer.Serialize(stream, this);
stream.Seek(0, SeekOrigin.Begin);
Properties.Settings.Default.GeneralOptions =
Convert.ToBase64String(stream.ToArray());
//Properties.Settings.Default.Save();
}
private bool _IsHideOnMinimize = false;
[DisplayName("Скрывать при сворачивании")]
[Description("Скрывать окно программы с панели задач при сворачивании")]
[Category("1. Основные")]
[TypeConverter(typeof(BooleanTypeConverter<BooleanYesNo>))]
public bool IsHideOnMinimize
{
get { return _IsHideOnMinimize; }
set { _IsHideOnMinimize = value; }
}
private bool _LoadLastFile = false;
[DisplayName("Загружать последний файл")]
[Description("Загружать последний открытый файл при загрузке программы")]
[Category("1. Основные")]
[TypeConverter(typeof(BooleanTypeConverter<BooleanYesNo>))]
public bool LoadLastFile
{
get { return _LoadLastFile; }
set { _LoadLastFile = value; }
}
private Int32 _CountRecentFiles = 4;
[DisplayName("Количество недавних файлов")]
[Description("Указывает, сколько последних открытых файлов будет отображаться в меню Файл")]
[Category("1. Основные")]
public Int32 CountRecentFiles
{
get { return _CountRecentFiles; }
set { if (value >= 0) _CountRecentFiles = value; }
}
private Font _FontRichEdit = new Font("Lucida Console", 8.25F, FontStyle.Regular);
[DisplayName("Шрифт окна заметок")]
[Description("Шрифт, используемый по умолчанию для текста заметок")]
[Category("2. Оформление")]
public Font FontRichEdit
{
get { return _FontRichEdit; }
set { _FontRichEdit = value; }
}
}
}