using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Sloth
{
using Settings = Properties.Settings;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static readonly String helpFileRelativePath = @"\Help\help.html";
static String HelpFile
{
get { return Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + helpFileRelativePath; }
}
static String HelpUrl
{
get { return "file:///" + HelpFile.Replace(@"\", "/"); }
}
Interpreter interpreter;
String currentFileName;
Boolean sourceSaved, sourceModified;
EmulatorForm emulatorForm;
public void LoadCode(String fileName)
{
txtCode.Text = File.ReadAllText(fileName);
sourceSaved = true;
currentFileName = fileName;
}
private void UpdatePositionStatus()
{
if (txtCode.SelectionStart < 0)
return;
Int32 currentline = txtCode.GetLineFromCharIndex(txtCode.SelectionStart);
strStatus.Text = String.Format("Linea {0}, colonna {1}", currentline + 1, txtCode.SelectionStart - txtCode.GetFirstCharIndexFromLine(currentline) + 1);
}
private void BindSettings()
{
txtCode.KeywordColor = Settings.Default.KeywordColor;
txtCode.CommentColor = Settings.Default.CommentColor;
txtCode.NumbersColor = Settings.Default.NumberColor;
txtCode.RegisterColor = Settings.Default.RegisterColor;
txtCode.Font = Settings.Default.CodeFont;
}
private void Form1_Load(object sender, EventArgs e)
{
interpreter
= new Interpreter
();
emulatorForm
= new EmulatorForm
(1024
);
this.BindSettings();
AppDomain.CurrentDomain.UnhandledException += AppDomain_UnhandledException;
}
private void txtCode_KeyPress(object sender, KeyPressEventArgs e)
{
this.UpdatePositionStatus();
sourceModified = true;
}
private void strInterpret_Click(object sender, EventArgs e)
{
try
{
interpreter.Parse(txtCode.Text);
strStatus.Text += ", Codice interpretato senza errori";
}
catch (InterpreterException ex)
{
errorBox.ErrorName = ex.GetType().Name;
errorBox.Description = ex.Message;
errorBox.LineNumber = ex.LineNumber;
if (ex.LineNumber > 0)
{
txtCode.Select(txtCode.GetFirstCharIndexFromLine(ex.LineNumber - 1), txtCode.Lines[ex.LineNumber - 1].Length);
txtCode.ScrollToCaret();
}
errorBox.Show();
}
}
private void txtCode_SelectionChanged(object sender, EventArgs e)
{
this.UpdatePositionStatus();
}
private void strSave_Click(object sender, EventArgs e)
{
if (sourceSaved)
{
File.WriteAllText(currentFileName, txtCode.Text);
sourceModified = false;
}
else
strSaveAs_Click(sender, e);
}
private void strSaveAs_Click(object sender, EventArgs e)
{
if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
currentFileName = saveFileDialog.FileName;
File.WriteAllText(currentFileName, txtCode.Text);
sourceSaved = true;
sourceModified = false;
}
}
private void strNew_Click(object sender, EventArgs e)
{
if (sourceModified)
{
var answer = MessageBox.Show("Salvare il sorgente prima di creare un nuovo documento?", "Salvataggio", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
switch (answer)
{
case DialogResult.Yes:
strSave_Click(sender, e);
break;
case DialogResult.Cancel:
return;
}
}
txtCode.Text = String.Empty;
sourceSaved = false;
currentFileName = String.Empty;
interpreter.RemoveTempData();
}
private void strOpen_Click(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
strNew_Click(sender, e);
txtCode.Text = File.ReadAllText(openFileDialog.FileName);
txtCode.ColorText();
currentFileName = openFileDialog.FileName;
sourceSaved = true;
}
}
private void strExit_Click(object sender, EventArgs e)
{
strNew_Click(sender, e);
this.Close();
}
private void strUndo_Click(object sender, EventArgs e)
{
txtCode.Undo();
}
private void strRedo_Click(object sender, EventArgs e)
{
txtCode.Redo();
}
private void strCut_Click(object sender, EventArgs e)
{
txtCode.Cut();
}
private void strCopy_Click(object sender, EventArgs e)
{
txtCode.Copy();
}
private void strPaste_Click(object sender, EventArgs e)
{
txtCode.Paste();
}
private void strSelectAll_Click(object sender, EventArgs e)
{
txtCode.SelectAll();
}
private void strRun_Click(object sender, EventArgs e)
{
if (interpreter.Output == null)
{
MessageBox.Show("E' necessario interpretare il codice prima di avviare l'emulatore.", "Interprete", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
emulatorForm.Emulator.Reset();
emulatorForm.Emulator.Load(interpreter.Output);
emulatorForm.Show();
}
private void strMemory1K_Click(object sender, EventArgs e)
{
emulatorForm.Emulator.Memory.Dispose();
emulatorForm.
Emulator.
Memory = new VirtualMemory
(1024
);
this.ResetMemorySizeCheks();
}
private void strMemory4K_Click(object sender, EventArgs e)
{
emulatorForm.Emulator.Memory.Dispose();
emulatorForm.
Emulator.
Memory = new VirtualMemory
(4096
);
this.ResetMemorySizeCheks();
}
private void strMemory16K_Click(object sender, EventArgs e)
{
emulatorForm.Emulator.Memory.Dispose();
emulatorForm.
Emulator.
Memory = new VirtualMemory
(16384
);
this.ResetMemorySizeCheks();
}
private void strMemoryCustomSize_Click(object sender, EventArgs e)
{
using (InputDialog inputBox
= new InputDialog
())
{
inputBox.RequireNumericValue = true;
inputBox.Description = "Inserire la nuova dimensione della memoria dedicata:";
if (inputBox.ShowDialog() == DialogResult.OK)
{
emulatorForm.Emulator.Memory.Dispose();
emulatorForm.
Emulator.
Memory = new VirtualMemory
(Math.
Abs(inputBox.
InputNumber));
strMemoryCustomSize.Text = Math.Abs(inputBox.InputNumber).ToString("N0") + " B ...";
this.ResetMemorySizeCheks();
strMemoryCustomSize.
Checked = true
;
}
}
}
private void ResetMemorySizeCheks()
{
}
private void strCustomize_Click(object sender, EventArgs e)
{
using (CustomizeCodeDialog dialog
= new CustomizeCodeDialog
())
{
if (dialog.ShowDialog() == DialogResult.OK)
{
this.BindSettings();
txtCode.ColorText();
}
}
}
private void Form1_Shown(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(txtCode.Text))
txtCode.ColorText();
}
private void strAbout_Click(object sender, EventArgs e)
{
using (AboutDialog aboutDialog
= new AboutDialog
())
{
aboutDialog.ShowDialog();
}
}
private void AppDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
using (UnhandledExceptionDialog errorDialog
= new UnhandledExceptionDialog
())
{
errorDialog.UnhandledException = (Exception)e.ExceptionObject;
errorDialog.ShowDialog();
}
}
private void strContent_Click(object sender, EventArgs e)
{
if (File.Exists(HelpFile))
System.Diagnostics.Process.Start(HelpUrl);
}
}
}