|
| 1 | +using System; |
| 2 | +using System.ComponentModel; |
| 3 | +using System.Reflection; |
| 4 | +using System.Windows; |
| 5 | +using System.Windows.Media; |
| 6 | +using System.Xml; |
| 7 | +using ICSharpCode.AvalonEdit; |
| 8 | +using ICSharpCode.AvalonEdit.Highlighting; |
| 9 | +using ICSharpCode.AvalonEdit.Highlighting.Xshd; |
| 10 | + |
| 11 | +namespace Rubberduck.UI.Controls |
| 12 | +{ |
| 13 | + //see http://stackoverflow.com/a/20823917/4088852 |
| 14 | + public class BindableTextEditor : TextEditor, INotifyPropertyChanged |
| 15 | + { |
| 16 | + public BindableTextEditor() |
| 17 | + { |
| 18 | + WordWrap = false; |
| 19 | + |
| 20 | + var highlighter = LoadHighlighter("Rubberduck.UI.Controls.vba.xshd"); |
| 21 | + SyntaxHighlighting = highlighter; |
| 22 | + |
| 23 | + //Style hyperlinks so they look like comments. Note - this needs to move if used for user code. |
| 24 | + TextArea.TextView.LinkTextUnderline = false; |
| 25 | + TextArea.TextView.LinkTextForegroundBrush = new SolidColorBrush(Colors.Green); |
| 26 | + Options.RequireControlModifierForHyperlinkClick = false; |
| 27 | + Options.EnableHyperlinks = true; |
| 28 | + Options.EnableEmailHyperlinks = true; |
| 29 | + } |
| 30 | + |
| 31 | + public new string Text |
| 32 | + { |
| 33 | + get { return base.Text; } |
| 34 | + set { base.Text = value; } |
| 35 | + } |
| 36 | + |
| 37 | + public static readonly DependencyProperty TextProperty = |
| 38 | + DependencyProperty.Register("Text", typeof(string), typeof(BindableTextEditor), new PropertyMetadata((obj, args) => |
| 39 | + { |
| 40 | + var target = (BindableTextEditor)obj; |
| 41 | + target.Text = (string)args.NewValue; |
| 42 | + })); |
| 43 | + |
| 44 | + protected override void OnTextChanged(EventArgs e) |
| 45 | + { |
| 46 | + RaisePropertyChanged("Text"); |
| 47 | + base.OnTextChanged(e); |
| 48 | + } |
| 49 | + |
| 50 | + public void RaisePropertyChanged(string property) |
| 51 | + { |
| 52 | + if (PropertyChanged != null) |
| 53 | + { |
| 54 | + PropertyChanged(this, new PropertyChangedEventArgs(property)); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public event PropertyChangedEventHandler PropertyChanged; |
| 59 | + |
| 60 | + private static IHighlightingDefinition LoadHighlighter(string resource) |
| 61 | + { |
| 62 | + var assembly = Assembly.GetExecutingAssembly(); |
| 63 | + using (var stream = assembly.GetManifestResourceStream(resource)) |
| 64 | + { |
| 65 | + if (stream == null) |
| 66 | + { |
| 67 | + return null; |
| 68 | + } |
| 69 | + using (var reader = new XmlTextReader(stream)) |
| 70 | + { |
| 71 | + return HighlightingLoader.Load(reader, HighlightingManager.Instance); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments