1 | using System.Collections.Generic;
|
---|
2 | using System.IO;
|
---|
3 | using System.Windows.Controls;
|
---|
4 | using System.Windows.Input;
|
---|
5 | using System.Xml;
|
---|
6 | using ICSharpCode.AvalonEdit.CodeCompletion;
|
---|
7 | using ICSharpCode.AvalonEdit.Highlighting;
|
---|
8 | using ICSharpCode.AvalonEdit.Highlighting.Xshd;
|
---|
9 |
|
---|
10 | namespace 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 | }
|
---|