#region License Information /* HeuristicLab * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion 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; } } }