Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2026 added rudimentary error checking to GPDL parser ATG. Added GPL license headers to all files. Created a first set of unit tests.

File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Collections.Generic;
23using System.IO;
24using System.Windows.Controls;
25using System.Windows.Input;
26using System.Xml;
27using ICSharpCode.AvalonEdit.CodeCompletion;
28using ICSharpCode.AvalonEdit.Highlighting;
29using ICSharpCode.AvalonEdit.Highlighting.Xshd;
30
31namespace HeuristicLab.Problems.GPDL.Views {
32  /// <summary>
33  /// Interaction logic for CodeEditor.xaml
34  /// </summary>
35  public partial class CodeEditor : UserControl {
36    public string syntaxHighlighting { get; set; }
37    private bool controlHeld = false;
38    private CompletionWindow completionWindow;
39    private ColorizeErrors errorLineTransformer;
40
41    public CodeEditor() {
42      InitializeComponent();
43
44      LoadSyntaxHighlighting();
45
46      // for auto-completion
47      textEditor.TextArea.TextEntered += textEditor_TextArea_TextEntered;
48      // show errors visually
49      errorLineTransformer = new ColorizeErrors();
50      textEditor.TextArea.TextView.LineTransformers.Add(errorLineTransformer);
51    }
52
53    private void textEditor_TextArea_TextEntered(object sender, TextCompositionEventArgs e) {
54      if (e.Text == ".") {
55        // Open code completion after the user has pressed dot:
56        completionWindow = new CompletionWindow(textEditor.TextArea);
57        IList<ICompletionData> data = completionWindow.CompletionList.CompletionData;
58        data.Add(new MyCompletionData("Item1"));
59        data.Add(new MyCompletionData("Item2"));
60        data.Add(new MyCompletionData("Item3"));
61        completionWindow.Show();
62        completionWindow.Closed += delegate {
63          completionWindow = null;
64        };
65      }
66    }
67
68
69    private void LoadSyntaxHighlighting() {
70      textEditor.ShowLineNumbers = true;
71
72      using (var s = new StringReader(Properties.Resources.GPDL)) {
73        using (XmlTextReader reader = new XmlTextReader(s)) {
74          textEditor.SyntaxHighlighting = HighlightingLoader.Load(reader, HighlightingManager.Instance);
75
76        }
77      }
78    }
79
80    private void textEditor_MouseWheel(object sender, MouseWheelEventArgs e) {
81    }
82
83    private void textEditor_KeyDown(object sender, KeyEventArgs e) {
84      if (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl) controlHeld = true;
85    }
86
87    private void textEditor_KeyUp(object sender, KeyEventArgs e) {
88      controlHeld = false;
89    }
90
91    internal void SetLexError(string msg, int line, int col) {
92      errorLineTransformer.MarkLexError(msg, line, col);
93      textEditor.TextArea.TextView.Redraw();
94    }
95
96    internal void SetSynError(string msg, int line, int col) {
97      errorLineTransformer.MarkSynError(msg, line, col);
98      textEditor.TextArea.TextView.Redraw();
99    }
100
101    internal void ClearErrors() {
102      errorLineTransformer.ClearErrors();
103      textEditor.TextArea.TextView.Redraw();
104    }
105
106    private void textEditor_PreviewMouseWheel(object sender, MouseWheelEventArgs e) {
107      if (!controlHeld) return;
108      if (e.Delta > 0)
109        textEditor.FontSize += 1;
110      else textEditor.FontSize -= 1;
111      e.Handled = true;
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.