-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConverter.cs
More file actions
209 lines (187 loc) · 6.86 KB
/
Converter.cs
File metadata and controls
209 lines (187 loc) · 6.86 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
using System;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Data;
using FooEditEngine;
using Windows.ApplicationModel.Resources;
using System.IO;
using System.Runtime.Serialization;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI;
namespace FooEditor.WinUI
{
public class ObjectToXmlConverter : IValueConverter
{
public object Convert(object obj, Type targetType, object param, string language)
{
using (MemoryStream memoryStream = new MemoryStream())
using (StreamReader reader = new StreamReader(memoryStream))
{
DataContractSerializer serializer = new DataContractSerializer(obj.GetType());
serializer.WriteObject(memoryStream, obj);
memoryStream.Position = 0;
return reader.ReadToEnd();
}
}
public object ConvertBack(object value, Type toType, object param, string language)
{
string xml = (string)value;
using (Stream stream = new MemoryStream())
{
byte[] data = System.Text.Encoding.UTF8.GetBytes(xml);
stream.Write(data, 0, data.Length);
stream.Position = 0;
DataContractSerializer deserializer = new DataContractSerializer(toType);
return deserializer.ReadObject(stream);
}
}
}
public class TextPointConverter : IValueConverter
{
ResourceLoader loader = new ResourceLoader();
public object Convert(object value, System.Type targetType, object parameter, string language)
{
TextPoint tp = (TextPoint)value;
return string.Format(loader.GetString("TextPointFormat"), tp.row + 1, tp.col + 1);
}
public object ConvertBack(object value, System.Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
public sealed class LineFeedConverter : IValueConverter
{
public object Convert(object value, System.Type targetType, object parameter, string language)
{
LineFeedType type = (LineFeedType)value;
switch (type)
{
case LineFeedType.CR:
return "CR";
case LineFeedType.CRLF:
return "CRLF";
case LineFeedType.LF:
return "LF";
}
return string.Empty;
}
public object ConvertBack(object value, System.Type targetType, object parameter, string language)
{
string str = (string)value;
switch (str)
{
case "CR":
return LineFeedType.CR;
case "LF":
return LineFeedType.LF;
case "CRLF":
return LineFeedType.CRLF;
}
throw new ArgumentOutOfRangeException();
}
}
public class RateConverter : IValueConverter
{
public object Convert(object value, System.Type targetType, object parameter, string language)
{
double v = (double)value;
return (double)(v * 100);
}
public object ConvertBack(object value, System.Type targetType, object parameter, string language)
{
double v = (double)value;
return (double)( v / 100);
}
}
public class MagnificationPower : IValueConverter
{
ResourceLoader loader = new ResourceLoader();
public object Convert(object value, System.Type targetType, object parameter, string language)
{
double v = (double)value;
return string.Format(loader.GetString("MagnificationPowerFormat"), (int)(v * 100));
}
public object ConvertBack(object value, System.Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
public class LineBreakMethodConverter : IValueConverter
{
ResourceLoader loader = new ResourceLoader();
public object Convert(object value, Type targetType, object parameter, string language)
{
LineBreakMethod method = (LineBreakMethod)value;
switch (method)
{
case LineBreakMethod.None:
return loader.GetString("LineBreakMethodNone");
case LineBreakMethod.CharUnit:
return loader.GetString("LineBreakMethodCharUnit");
case LineBreakMethod.PageBound:
return loader.GetString("LineBreakMethodPageBound");
}
throw new ArgumentOutOfRangeException();
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
string name = (string)value;
if (name == loader.GetString("LineBreakMethodNone"))
return LineBreakMethod.None;
if (name == loader.GetString("LineBreakMethodCharUnit"))
return LineBreakMethod.CharUnit;
if (name == loader.GetString("LineBreakMethodPageBound"))
return LineBreakMethod.PageBound;
throw new ArgumentOutOfRangeException();
}
}
/// <summary>
/// Converts a Boolean into a Visibility.
/// </summary>
public class BooleanToVisibilityConverter : IValueConverter
{
/// <summary>
/// If set to True, conversion is reversed: True will become Collapsed.
/// </summary>
public bool IsReversed { get; set; }
public object Convert(object value, System.Type targetType, object parameter, string language)
{
var val = System.Convert.ToBoolean(value);
if (this.IsReversed)
{
val = !val;
}
if (val)
{
return Visibility.Visible;
}
return Visibility.Collapsed;
}
public object ConvertBack(object value, System.Type targetType, object parameter, string language)
{
throw new System.NotImplementedException();
}
}
public class Color2BrushConverter : IValueConverter
{
/// <summary>
/// Color⇒brush
/// </summary>
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value == null)
{
return new SolidColorBrush(Colors.Blue);
}
if (value is Windows.UI.Color color)
{
return new SolidColorBrush(color);
}
else
throw new Exception("Can't get SolidColorBrush");
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
}