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

Last change on this file since 9724 was 9724, checked in by gkronber, 6 years ago

#2026 changed ATG to Coco/R syntax and use Coco/R (C#) to generate scanner and parser for GPDL

File size: 3.0 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, MouseWheelEventArgs e) {
60    }
61
62    private void textEditor_KeyDown(object sender, 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.TextArea.TextView.Redraw();
73    }
74
75    internal void SetSynError(string msg, int line, int col) {
76      errorLineTransformer.MarkSynError(msg, line, col);
77      textEditor.TextArea.TextView.Redraw();
78    }
79
80    internal void ClearErrors() {
81      errorLineTransformer.ClearErrors();
82      textEditor.TextArea.TextView.Redraw();
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.