-
Notifications
You must be signed in to change notification settings - Fork 7
/
EncodingMargin.xaml.cs
270 lines (241 loc) · 9.67 KB
/
EncodingMargin.xaml.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
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text;
using System.Windows.Input;
using System.Text;
using System.Diagnostics;
using System.Threading;
namespace FileEncoding
{
/// <summary>
/// Margin's canvas and visual definition including both size and content
/// </summary>
public partial class EncodingMargin : Button, IWpfTextViewMargin
{
/// <summary>
/// Margin name.
/// </summary>
public const string MarginName = "FileEncodeMargin";
/// <summary>
/// A value indicating whether the object is disposed.
/// </summary>
private bool isDisposed;
/// <summary>
/// The document of textview.
/// </summary>
private readonly ITextDocument document = null;
/// <summary>
/// The document encoding name.
/// </summary>
private string documentEncodingName;
private static bool HasBom(Encoding encoding)
{
return encoding.GetPreamble().Length != 0;
}
/// <summary>
/// Rewrite some encoding name.
/// </summary>
/// <param name="document">The document to get encoding.</param>
/// <returns>Encoding name</returns>
private static string GetDocumentEncoding(ITextDocument document)
{
int codepage = document.Encoding.CodePage;
if (codepage == Encoding.UTF8.CodePage) {
return HasBom(document.Encoding) ? "UTF-8 (BOM)" : "UTF-8";
} else if (codepage == Encoding.Unicode.CodePage) {
return "Unicode";
} else if (codepage == Encoding.BigEndianUnicode.CodePage) {
// default name is too long
return "Unicode BE";
} else {
return document.Encoding.EncodingName;
}
}
internal class ConvertCommand : ICommand
{
private readonly Encoding encoding;
public ConvertCommand(Encoding encoding)
{
this.encoding = encoding;
}
event EventHandler ICommand.CanExecuteChanged
{
add { }
remove { }
}
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
EncodingMargin _this = parameter as EncodingMargin;
if (_this.document.Encoding.CodePage != encoding.CodePage ||
(HasBom(_this.document.Encoding) != HasBom(encoding))) {
_this.document.Encoding = encoding;
_this.document.UpdateDirtyState(true, DateTime.Now);
_this.UpdateContent();
}
}
}
/// <summary>
/// Initializes a new instance.
/// </summary>
/// <param name="textView">The textView to attach the margin to.</param>
public EncodingMargin(IWpfTextView textView, IWpfTextViewMargin marginContainer)
{
InitializeComponent();
// display
Focusable = false;
// Text
if (!textView.TextBuffer.Properties.TryGetProperty(typeof(ITextDocument), out document)) {
textView.TextDataModel.DocumentBuffer.Properties.TryGetProperty(typeof(ITextDocument), out document);
}
UpdateContent();
// Menu
Encoding[] encodings = {
Encoding.Unicode, Encoding.BigEndianUnicode,
Encoding.UTF8, new UTF8Encoding(false), Encoding.Default
};
for (int i = 0; i < ContextMenu.Items.Count; ++i)
{
MenuItem item = ContextMenu.Items[i] as MenuItem;
item.Command = new ConvertCommand(encodings[i]);
item.CommandParameter = this;
if (i == 4) item.Header = "SYS - " + Encoding.Default.EncodingName;
}
ContextMenu.PlacementTarget = this;
ContextMenu.Placement = PlacementMode.Custom;
ContextMenu.CustomPopupPlacementCallback = CustomPopupPlacementCallback;
ContextMenu.Opened += ContextMenuOpened;
document.FileActionOccurred += (sender, e) => UpdateContent();
}
private void ContextMenuOpened(object sender, RoutedEventArgs e)
{
ContextMenu menu = (ContextMenu)e.Source;
MenuItem item = (MenuItem)menu.Items[0];
item.Focus();
}
private CustomPopupPlacement[] CustomPopupPlacementCallback(Size popupSize, Size targetSize, Point offset)
{
Debug.WriteLine("placement");
CustomPopupPlacement[] result = new CustomPopupPlacement[1];
result[0] = new CustomPopupPlacement{
Point = new Point(targetSize.Width - popupSize.Width, -popupSize.Height),
PrimaryAxis = PopupPrimaryAxis.Horizontal
};
return result;
}
private void OnButtonClicked(object sender, EventArgs e)
{
string[] names = { "Unicode", "Unicode BE", "UTF-8 (BOM)", "UTF-8", Encoding.Default.EncodingName };
for (int i = 0; i < ContextMenu.Items.Count; ++i)
{
MenuItem item = ContextMenu.Items[i] as MenuItem;
item.IsChecked = documentEncodingName.Equals(names[i]);
}
ContextMenu.IsOpen = true;
}
/// <summary>
/// Update content with document.
/// </summary>
private void UpdateContent()
{
documentEncodingName = GetDocumentEncoding(document);
if (documentEncodingName.Equals(Encoding.Default.EncodingName)) {
Content = "SYS";
} else {
Content = documentEncodingName;
}
}
#region AutoGenerate
#region IWpfTextViewMargin
/// <summary>
/// Gets the <see cref="Sytem.Windows.FrameworkElement"/> that implements the visual representation of the margin.
/// </summary>
/// <exception cref="ObjectDisposedException">The margin is disposed.</exception>
public FrameworkElement VisualElement
{
// Since this margin implements Canvas, this is the object which renders
// the margin.
get
{
ThrowIfDisposed();
return this;
}
}
#endregion
#region ITextViewMargin
/// <summary>
/// Gets the size of the margin.
/// </summary>
/// <remarks>
/// For a horizontal margin this is the height of the margin,
/// since the width will be determined by the <see cref="ITextView"/>.
/// For a vertical margin this is the width of the margin,
/// since the height will be determined by the <see cref="ITextView"/>.
/// </remarks>
/// <exception cref="ObjectDisposedException">The margin is disposed.</exception>
public double MarginSize
{
get
{
ThrowIfDisposed();
// Since this is a horizontal margin, its width will be bound to the width of the text view.
// Therefore, its size is its height.
return ActualHeight;
}
}
/// <summary>
/// Gets a value indicating whether the margin is enabled.
/// </summary>
/// <exception cref="ObjectDisposedException">The margin is disposed.</exception>
public bool Enabled
{
get
{
ThrowIfDisposed();
// The margin should always be enabled
return true;
}
}
/// <summary>
/// Gets the <see cref="ITextViewMargin"/> with the given <paramref name="marginName"/> or null if no match is found
/// </summary>
/// <param name="marginName">The name of the <see cref="ITextViewMargin"/></param>
/// <returns>The <see cref="ITextViewMargin"/> named <paramref name="marginName"/>, or null if no match is found.</returns>
/// <remarks>
/// A margin returns itself if it is passed its own name. If the name does not match and it is a container margin, it
/// forwards the call to its children. Margin name comparisons are case-insensitive.
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="marginName"/> is null.</exception>
public ITextViewMargin GetTextViewMargin(string marginName)
{
return string.Equals(marginName, MarginName, StringComparison.OrdinalIgnoreCase) ? this : null;
}
/// <summary>
/// Disposes an instance of <see cref="FileEncodeMargin"/> class.
/// </summary>
public void Dispose()
{
if (!isDisposed) {
GC.SuppressFinalize(this);
isDisposed = true;
}
}
#endregion
/// <summary>
/// Checks and throws <see cref="ObjectDisposedException"/> if the object is disposed.
/// </summary>
private void ThrowIfDisposed()
{
if (isDisposed) {
throw new ObjectDisposedException(MarginName);
}
}
#endregion
}
}