Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GPDL/HeuristicLab.Problems.GPDL.Views/3.4/CodeEditor.xaml.cs @ 9674

Last change on this file since 9674 was 9674, checked in by gkronber, 11 years ago

#2026: Worked on integration of ICSharpCode.AvalonEdit control for GPDL syntax-highlighting and error reporting.

File size: 3.1 KB
Line 
1using System.Collections.Generic;
2using System.IO;
3using System.Windows.Controls;
4using System.Windows.Input;
5using System.Xml;
6using ICSharpCode.AvalonEdit.CodeCompletion;
7using ICSharpCode.AvalonEdit.Highlighting;
8using ICSharpCode.AvalonEdit.Highlighting.Xshd;
9
10namespace HeuristicLab.Problems.GPDL.Views {
11  /// <summary>
12  /// Interaction logic for CodeEditor.xaml
13  /// </summary>
14  public partial class CodeEditor : UserControl {
15    public string syntaxHighlighting { get; set; }
16    private bool controlHeld = false;
17    private CompletionWindow completionWindow;
18    private ColorizeErrors errorLineTransformer;
19
20    public CodeEditor() {
21      InitializeComponent();
22
23      LoadSyntaxHighlighting();
24
25      // for auto-completion
26      textEditor.TextArea.TextEntered += textEditor_TextArea_TextEntered;
27      // show errors visually
28      errorLineTransformer = new ColorizeErrors();
29      textEditor.TextArea.TextView.LineTransformers.Add(errorLineTransformer);
30    }
31
32    private void textEditor_TextArea_TextEntered(object sender, TextCompositionEventArgs e) {
33      if (e.Text == ".") {
34        // Open code completion after the user has pressed dot:
35        completionWindow = new CompletionWindow(textEditor.TextArea);
36        IList<ICompletionData> data = completionWindow.CompletionList.CompletionData;
37        data.Add(new MyCompletionData("Item1"));
38        data.Add(new MyCompletionData("Item2"));
39        data.Add(new MyCompletionData("Item3"));
40        completionWindow.Show();
41        completionWindow.Closed += delegate {
42          completionWindow = null;
43        };
44      }
45    }
46
47
48    private void LoadSyntaxHighlighting() {
49      textEditor.ShowLineNumbers = true;
50
51      using (var s = new StringReader(Properties.Resources.GPDL)) {
52        using (XmlTextReader reader = new XmlTextReader(s)) {
53          textEditor.SyntaxHighlighting = HighlightingLoader.Load(reader, HighlightingManager.Instance);
54
55        }
56      }
57    }
58
59    private void textEditor_MouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e) {
60    }
61
62    private void textEditor_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) {
63      if (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl) controlHeld = true;
64    }
65
66    private void textEditor_KeyUp(object sender, KeyEventArgs e) {
67      controlHeld = false;
68    }
69
70    internal void SetLexError(string msg, int line, int col) {
71      errorLineTransformer.MarkLexError(msg, line, col);
72      textEditor.InvalidateVisual();
73    }
74
75    internal void SetSynError(string msg, int line, int col) {
76      errorLineTransformer.MarkSynError(msg, line, col);
77      textEditor.InvalidateVisual();
78    }
79
80    internal void ClearErrors() {
81      errorLineTransformer.ClearErrors();
82      textEditor.InvalidateVisual();
83    }
84
85    private void textEditor_PreviewMouseWheel(object sender, MouseWheelEventArgs e) {
86      if (!controlHeld) return;
87      if (e.Delta > 0)
88        textEditor.FontSize += 1;
89      else textEditor.FontSize -= 1;
90      e.Handled = true;
91    }
92  }
93}
Note: See TracBrowser for help on using the repository browser.