using System.Collections.Generic; using System.IO; using System.Windows.Controls; using System.Windows.Input; using System.Xml; using ICSharpCode.AvalonEdit.CodeCompletion; using ICSharpCode.AvalonEdit.Highlighting; using ICSharpCode.AvalonEdit.Highlighting.Xshd; namespace HeuristicLab.Problems.GPDL.Views { /// /// Interaction logic for CodeEditor.xaml /// public partial class CodeEditor : UserControl { public string syntaxHighlighting { get; set; } private bool controlHeld = false; private CompletionWindow completionWindow; private ColorizeErrors errorLineTransformer; public CodeEditor() { InitializeComponent(); LoadSyntaxHighlighting(); // for auto-completion textEditor.TextArea.TextEntered += textEditor_TextArea_TextEntered; // show errors visually errorLineTransformer = new ColorizeErrors(); textEditor.TextArea.TextView.LineTransformers.Add(errorLineTransformer); } private void textEditor_TextArea_TextEntered(object sender, TextCompositionEventArgs e) { if (e.Text == ".") { // Open code completion after the user has pressed dot: completionWindow = new CompletionWindow(textEditor.TextArea); IList data = completionWindow.CompletionList.CompletionData; data.Add(new MyCompletionData("Item1")); data.Add(new MyCompletionData("Item2")); data.Add(new MyCompletionData("Item3")); completionWindow.Show(); completionWindow.Closed += delegate { completionWindow = null; }; } } private void LoadSyntaxHighlighting() { textEditor.ShowLineNumbers = true; using (var s = new StringReader(Properties.Resources.GPDL)) { using (XmlTextReader reader = new XmlTextReader(s)) { textEditor.SyntaxHighlighting = HighlightingLoader.Load(reader, HighlightingManager.Instance); } } } private void textEditor_MouseWheel(object sender, MouseWheelEventArgs e) { } private void textEditor_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl) controlHeld = true; } private void textEditor_KeyUp(object sender, KeyEventArgs e) { controlHeld = false; } internal void SetLexError(string msg, int line, int col) { errorLineTransformer.MarkLexError(msg, line, col); textEditor.TextArea.TextView.Redraw(); } internal void SetSynError(string msg, int line, int col) { errorLineTransformer.MarkSynError(msg, line, col); textEditor.TextArea.TextView.Redraw(); } internal void ClearErrors() { errorLineTransformer.ClearErrors(); textEditor.TextArea.TextView.Redraw(); } private void textEditor_PreviewMouseWheel(object sender, MouseWheelEventArgs e) { if (!controlHeld) return; if (e.Delta > 0) textEditor.FontSize += 1; else textEditor.FontSize -= 1; e.Handled = true; } } }