Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Sloth - Form1.cs

Form1.cs

Caricato da: Totem
Scarica il programma completo

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10.  
  11. namespace Sloth
  12. {
  13.     using Settings = Properties.Settings;
  14.  
  15.     public partial class Form1 : Form
  16.     {
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.  
  22.         static readonly String helpFileRelativePath = @"\Help\help.html";
  23.  
  24.         static String HelpFile
  25.         {
  26.             get { return Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + helpFileRelativePath; }
  27.         }
  28.  
  29.         static String HelpUrl
  30.         {
  31.             get { return "file:///" + HelpFile.Replace(@"\", "/"); }
  32.         }
  33.  
  34.         Interpreter interpreter;
  35.         String currentFileName;
  36.         Boolean sourceSaved, sourceModified;
  37.  
  38.         EmulatorForm emulatorForm;
  39.  
  40.         public void LoadCode(String fileName)
  41.         {
  42.             txtCode.Text = File.ReadAllText(fileName);
  43.             sourceSaved = true;
  44.             currentFileName = fileName;
  45.         }
  46.  
  47.         private void UpdatePositionStatus()
  48.         {
  49.             if (txtCode.SelectionStart < 0)
  50.                 return;
  51.             Int32 currentline = txtCode.GetLineFromCharIndex(txtCode.SelectionStart);
  52.             strStatus.Text = String.Format("Linea {0}, colonna {1}", currentline + 1, txtCode.SelectionStart - txtCode.GetFirstCharIndexFromLine(currentline) + 1);
  53.         }
  54.  
  55.         private void BindSettings()
  56.         {
  57.             txtCode.KeywordColor = Settings.Default.KeywordColor;
  58.             txtCode.CommentColor = Settings.Default.CommentColor;
  59.             txtCode.NumbersColor = Settings.Default.NumberColor;
  60.             txtCode.RegisterColor = Settings.Default.RegisterColor;
  61.             txtCode.Font = Settings.Default.CodeFont;
  62.         }
  63.  
  64.         private void Form1_Load(object sender, EventArgs e)
  65.         {
  66.             interpreter = new Interpreter();
  67.             emulatorForm = new EmulatorForm(1024);
  68.             this.BindSettings();
  69.             AppDomain.CurrentDomain.UnhandledException += AppDomain_UnhandledException;
  70.         }
  71.  
  72.         private void txtCode_KeyPress(object sender, KeyPressEventArgs e)
  73.         {
  74.             this.UpdatePositionStatus();
  75.             sourceModified = true;
  76.         }
  77.  
  78.         private void strInterpret_Click(object sender, EventArgs e)
  79.         {
  80.             try
  81.             {
  82.                 interpreter.Parse(txtCode.Text);
  83.                 strStatus.Text += ", Codice interpretato senza errori";
  84.             }
  85.             catch (InterpreterException ex)
  86.             {
  87.                 errorBox.ErrorName = ex.GetType().Name;
  88.                 errorBox.Description = ex.Message;
  89.                 errorBox.LineNumber = ex.LineNumber;
  90.                 if (ex.LineNumber > 0)
  91.                 {
  92.                     txtCode.Select(txtCode.GetFirstCharIndexFromLine(ex.LineNumber - 1), txtCode.Lines[ex.LineNumber - 1].Length);
  93.                     txtCode.ScrollToCaret();
  94.                 }
  95.                 errorBox.Show();
  96.             }
  97.         }
  98.  
  99.         private void txtCode_SelectionChanged(object sender, EventArgs e)
  100.         {
  101.             this.UpdatePositionStatus();
  102.         }
  103.  
  104.         private void strSave_Click(object sender, EventArgs e)
  105.         {
  106.             if (sourceSaved)
  107.             {
  108.                 File.WriteAllText(currentFileName, txtCode.Text);
  109.                 sourceModified = false;
  110.             }
  111.             else
  112.                 strSaveAs_Click(sender, e);
  113.         }
  114.  
  115.         private void strSaveAs_Click(object sender, EventArgs e)
  116.         {
  117.             if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  118.             {
  119.                 currentFileName = saveFileDialog.FileName;
  120.                 File.WriteAllText(currentFileName, txtCode.Text);
  121.                 sourceSaved = true;
  122.                 sourceModified = false;
  123.             }
  124.         }
  125.  
  126.         private void strNew_Click(object sender, EventArgs e)
  127.         {
  128.             if (sourceModified)
  129.             {
  130.                 var answer = MessageBox.Show("Salvare il sorgente prima di creare un nuovo documento?", "Salvataggio", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
  131.  
  132.                 switch (answer)
  133.                 {
  134.                     case DialogResult.Yes:
  135.                         strSave_Click(sender, e);
  136.                         break;
  137.  
  138.                     case DialogResult.Cancel:
  139.                         return;
  140.                 }
  141.             }
  142.  
  143.             txtCode.Text = String.Empty;
  144.             sourceSaved = false;
  145.             currentFileName = String.Empty;
  146.             interpreter.RemoveTempData();
  147.         }
  148.  
  149.         private void strOpen_Click(object sender, EventArgs e)
  150.         {
  151.             if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  152.             {
  153.                 strNew_Click(sender, e);
  154.                 txtCode.Text = File.ReadAllText(openFileDialog.FileName);
  155.                 txtCode.ColorText();
  156.                 currentFileName = openFileDialog.FileName;
  157.                 sourceSaved = true;
  158.             }
  159.         }
  160.  
  161.         private void strExit_Click(object sender, EventArgs e)
  162.         {
  163.             strNew_Click(sender, e);
  164.             this.Close();
  165.         }
  166.  
  167.         private void strUndo_Click(object sender, EventArgs e)
  168.         {
  169.             txtCode.Undo();
  170.         }
  171.  
  172.         private void strRedo_Click(object sender, EventArgs e)
  173.         {
  174.             txtCode.Redo();
  175.         }
  176.  
  177.         private void strCut_Click(object sender, EventArgs e)
  178.         {
  179.             txtCode.Cut();
  180.         }
  181.  
  182.         private void strCopy_Click(object sender, EventArgs e)
  183.         {
  184.             txtCode.Copy();
  185.         }
  186.  
  187.         private void strPaste_Click(object sender, EventArgs e)
  188.         {
  189.             txtCode.Paste();
  190.         }
  191.  
  192.         private void strSelectAll_Click(object sender, EventArgs e)
  193.         {
  194.             txtCode.SelectAll();
  195.         }
  196.  
  197.         private void strRun_Click(object sender, EventArgs e)
  198.         {
  199.             if (interpreter.Output == null)
  200.             {
  201.                 MessageBox.Show("E' necessario interpretare il codice prima di avviare l'emulatore.", "Interprete", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  202.                 return;
  203.             }
  204.  
  205.             emulatorForm.Emulator.Reset();
  206.             emulatorForm.Emulator.Load(interpreter.Output);
  207.             emulatorForm.Show();
  208.         }
  209.  
  210.         private void strMemory1K_Click(object sender, EventArgs e)
  211.         {
  212.             emulatorForm.Emulator.Memory.Dispose();
  213.             emulatorForm.Emulator.Memory = new VirtualMemory(1024);
  214.             this.ResetMemorySizeCheks();
  215.             strMemory1K.Checked = true;
  216.         }
  217.  
  218.         private void strMemory4K_Click(object sender, EventArgs e)
  219.         {
  220.             emulatorForm.Emulator.Memory.Dispose();
  221.             emulatorForm.Emulator.Memory = new VirtualMemory(4096);
  222.             this.ResetMemorySizeCheks();
  223.             strMemory4K.Checked = true;
  224.         }
  225.  
  226.         private void strMemory16K_Click(object sender, EventArgs e)
  227.         {
  228.             emulatorForm.Emulator.Memory.Dispose();
  229.             emulatorForm.Emulator.Memory = new VirtualMemory(16384);
  230.             this.ResetMemorySizeCheks();
  231.             strMemory16K.Checked = true;
  232.         }
  233.  
  234.         private void strMemoryCustomSize_Click(object sender, EventArgs e)
  235.         {
  236.             using (InputDialog inputBox = new InputDialog())
  237.             {
  238.                 inputBox.RequireNumericValue = true;
  239.                 inputBox.Description = "Inserire la nuova dimensione della memoria dedicata:";
  240.                 if (inputBox.ShowDialog() == DialogResult.OK)
  241.                 {
  242.                     emulatorForm.Emulator.Memory.Dispose();
  243.                     emulatorForm.Emulator.Memory = new VirtualMemory(Math.Abs(inputBox.InputNumber));
  244.                     strMemoryCustomSize.Text = Math.Abs(inputBox.InputNumber).ToString("N0") + " B ...";
  245.                     this.ResetMemorySizeCheks();
  246.                     strMemoryCustomSize.Checked = true;
  247.                 }
  248.             }
  249.         }
  250.  
  251.         private void ResetMemorySizeCheks()
  252.         {
  253.             strMemory1K.Checked = strMemory4K.Checked = strMemory16K.Checked = strMemoryCustomSize.Checked = false;
  254.         }
  255.  
  256.         private void strCustomize_Click(object sender, EventArgs e)
  257.         {
  258.             using (CustomizeCodeDialog dialog = new CustomizeCodeDialog())
  259.             {
  260.                 if (dialog.ShowDialog() == DialogResult.OK)
  261.                 {
  262.                     this.BindSettings();
  263.                     txtCode.ColorText();
  264.                 }
  265.             }
  266.         }
  267.  
  268.         private void Form1_Shown(object sender, EventArgs e)
  269.         {
  270.             if (!String.IsNullOrEmpty(txtCode.Text))
  271.                 txtCode.ColorText();
  272.         }
  273.  
  274.         private void strAbout_Click(object sender, EventArgs e)
  275.         {
  276.             using (AboutDialog aboutDialog = new AboutDialog())
  277.             {
  278.                 aboutDialog.ShowDialog();
  279.             }
  280.         }
  281.  
  282.         private void AppDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  283.         {
  284.             using (UnhandledExceptionDialog errorDialog = new UnhandledExceptionDialog())
  285.             {
  286.                 errorDialog.UnhandledException = (Exception)e.ExceptionObject;
  287.                 errorDialog.ShowDialog();
  288.             }
  289.         }
  290.  
  291.         private void strContent_Click(object sender, EventArgs e)
  292.         {
  293.             if (File.Exists(HelpFile))
  294.                 System.Diagnostics.Process.Start(HelpUrl);
  295.         }
  296.     }
  297. }