#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; using System.Drawing; using System.Windows.Forms; using HeuristicLab.Common; using HeuristicLab.Common.Resources; using HeuristicLab.Core.Views; using HeuristicLab.MainForm; namespace HeuristicLab.Scripting.Python.Views { [View("Python-Script View")] [Content(typeof(PythonScript), true)] public partial class PythonScriptView : NamedItemView { private bool running; private bool suppressEvents; public new PythonScript Content { get { return (PythonScript)base.Content; } set { base.Content = value; } } public PythonScriptView() { InitializeComponent(); errorListView.SmallImageList.Images.AddRange(new Image[] { VSImageLibrary.Warning, VSImageLibrary.Error }); AdjustErrorListViewColumnSizes(); } protected override void RegisterContentEvents() { base.RegisterContentEvents(); Content.CodeChanged += Content_CodeChanged; Content.ScriptExecutionStarted += Content_ScriptExecutionStarted; Content.ScriptExecutionFinished += Content_ScriptExecutionFinished; Content.ConsoleOutputChanged += Content_ConsoleOutputChanged; } protected override void DeregisterContentEvents() { Content.CodeChanged -= Content_CodeChanged; Content.ScriptExecutionStarted -= Content_ScriptExecutionStarted; Content.ScriptExecutionFinished -= Content_ScriptExecutionFinished; Content.ConsoleOutputChanged -= Content_ConsoleOutputChanged; base.DeregisterContentEvents(); } #region Content event handlers private void Content_CodeChanged(object sender, EventArgs e) { try { suppressEvents = true; codeTextBox.Text = Content.Code; } finally { suppressEvents = false; } } private void Content_ScriptExecutionStarted(object sender, EventArgs e) { if (InvokeRequired) Invoke((Action)Content_ScriptExecutionStarted, sender, e); else { Locked = true; ReadOnly = true; startStopButton.Image = VSImageLibrary.Stop; infoTabControl.SelectedTab = outputTabPage; } } private void Content_ScriptExecutionFinished(object sender, EventArgs e) { if (InvokeRequired) Invoke((Action>)Content_ScriptExecutionFinished, sender, e); else { Locked = false; ReadOnly = false; startStopButton.Image = VSImageLibrary.Play; running = false; var ex = e.Value; if (ex != null) PluginInfrastructure.ErrorHandling.ShowErrorDialog(this, ex); } } private void Content_ConsoleOutputChanged(object sender, EventArgs e) { if (InvokeRequired) Invoke((Action>)Content_ConsoleOutputChanged, sender, e); else { outputTextBox.AppendText(e.Value); } } #endregion protected override void OnContentChanged() { base.OnContentChanged(); if (Content == null) { codeTextBox.Text = string.Empty; variableStoreView.Content = null; } else { codeTextBox.Text = Content.Code; variableStoreView.Content = Content.VariableStore; /*if (Content.CompileErrors == null) { compilationLabel.ForeColor = SystemColors.ControlDarkDark; compilationLabel.Text = "Not compiled"; } else if (Content.CompileErrors.HasErrors) { compilationLabel.ForeColor = Color.DarkRed; compilationLabel.Text = "Compilation failed"; } else { compilationLabel.ForeColor = Color.DarkGreen; compilationLabel.Text = "Compilation successful"; }*/ } } protected override void SetEnabledStateOfControls() { base.SetEnabledStateOfControls(); compileButton.Enabled = Content != null && !Locked && !ReadOnly; startStopButton.Enabled = Content != null && (!Locked || running); codeTextBox.Enabled = Content != null && !Locked && !ReadOnly; } #region Child Control event handlers private void compileButton_Click(object sender, EventArgs e) { //Compile(); } private void startStopButton_Click(object sender, EventArgs e) { if (running) { Content.Kill(); } else { outputTextBox.Clear(); Content.Execute(); running = true; } } private void CodeTextBoxOnTextChanged(object sender, EventArgs e) { if (suppressEvents || Content == null) return; Content.Code = codeTextBox.Text; } #endregion #region global HotKeys protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { switch (keyData) { case Keys.F5: if (Content != null && !Locked) { outputTextBox.Clear(); Content.Execute(); running = true; } break; case Keys.F5 | Keys.Shift: if (running) Content.Kill(); break; /*case Keys.F6: if (!Compile() || Content.CompileErrors.HasWarnings) infoTabControl.SelectedTab = errorListTabPage; break;*/ } return base.ProcessCmdKey(ref msg, keyData); } #endregion private void ShowCompilationResults() { /*if (Content.CompileErrors.Count == 0) return; var msgs = Content.CompileErrors.OfType() .OrderBy(x => x.IsWarning) .ThenBy(x => x.Line) .ThenBy(x => x.Column); foreach (var m in msgs) { var item = new ListViewItem(); item.SubItems.AddRange(new[] { m.IsWarning ? "Warning" : "Error", m.ErrorNumber, m.Line.ToString(CultureInfo.InvariantCulture), m.Column.ToString(CultureInfo.InvariantCulture), m.ErrorText }); item.ImageIndex = m.IsWarning ? 0 : 1; errorListView.Items.Add(item); } AdjustErrorListViewColumnSizes(); * */ } private void AdjustErrorListViewColumnSizes() { foreach (ColumnHeader ch in errorListView.Columns) // adjusts the column width to the width of the column // header or the column content, whichever is greater ch.Width = -2; } } }