using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Xml;
using System.IO;
namespace Basic_Debugger
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
RegistryKey r = Registry.CurrentUser.OpenSubKey("Software", true).CreateSubKey("QBasic.Net");
string[] s = r.GetValueNames();
for (int I = 0; I < s.Length; I++)
{
int l = 0;
if (int.TryParse(s[I].Replace("RecentProject", ""), out l))
_recentProjects.Items.Add(r.GetValue(s[I]));
if (s[I] == "DefaultDirectory")
defaultFolder = (string)r.GetValue(s[I]);
}
_start_Resize(null, null);
_project = new BasicProject();
}
public string defaultFolder = "";
public BasicProject _project;
private void _menuNewProject_Click(object sender, EventArgs e)
{
Initialise();
}
private void _menuSave_Click(object sender, EventArgs e)
{
if (_project.Name == null && _project.Location == null)
{
ProjectDialog p = new ProjectDialog(this);
p.ShowDialog(this);
if (!p.Finished)
return;
p.Dispose();
}
StreamWriter w = new StreamWriter(_project.Location + "\\" + _project.Name + ".bat", false);
w.Write(_main.Text);
w.Flush();
w.Close();
}
private void _menuDebug_Click(object sender, EventArgs e)
{
_main.SelectAll();
_main.SelectionColor = Color.Black;
_main.Select(0, 0);
_project.Code.MainCode = _main.Text;
int[] errors;
if (!_project.Debugger.Check(out errors))
{
string[] lines = _main.Text.Split("\n".ToCharArray());
for (int I = 0; I < lines.Length; I++)
{
for (int A = 0; A < errors.Length; A++)
{
if (I == errors[A])
{
int start = 0;
for (int C = 0; C < I; C++)
start += lines[C].Length + 1;
_main.Select(start, lines[I].Length);
_main.SelectionColor = Color.Red;
A = errors.Length;
}
}
}
MessageBox.Show("There were some errors in code");
}
else
{
int line = 0;
if (!_project.Debugger.Debug(out line))
{
string[] lines = _main.Text.Split("\n".ToCharArray());
int start = 0;
for (int C = 0; C < line; C++)
start += lines[C].Length + 1;
_main.Select(start, lines[line].Length);
_main.SelectionColor = Color.Red;
MessageBox.Show("Runtime Error");
}
}
}
void _start_Resize(object sender, System.EventArgs e)
{
//_recentProjectsGroup.Location = new Point((_start.Size.Width - _recentProjectsGroup.Width) / 2, _recentProjectsGroup.Location.Y);
_OpenProject.Location = new Point((_start.Size.Width - _OpenProject.Width) / 2, _OpenProject.Location.Y);
_newSave.Location = new Point((_start.Size.Width - _newSave.Width - 8 - _new.Width) / 2, _newSave.Location.Y);
_new.Location = new Point((_start.Size.Width - _newSave.Width - 8 - _new.Width) / 2 + _newSave.Width + 8, _new.Location.Y);
}
#region Basic commands
private void _menuUndo_Click(object sender, EventArgs e)
{
_main.Undo();
}
private void _menuRedo_Click(object sender, EventArgs e)
{
_main.Redo();
}
private void _menuCut_Click(object sender, EventArgs e)
{
_main.Cut();
}
private void _menuCopy_Click(object sender, EventArgs e)
{
_main.Copy();
}
private void _menuPaste_Click(object sender, EventArgs e)
{
_main.Paste();
}
private void _menuSelectAll_Click(object sender, EventArgs e)
{
_main.SelectAll();
}
#endregion
private void _new_Click(object sender, EventArgs e)
{
Initialise();
}
private void _newSave_Click(object sender, EventArgs e)
{
ProjectDialog p = new ProjectDialog(this);
p.ShowDialog(this);
if (p.Finished)
OpenProject(_project.Location + "\\" + _project.Name + ".qproj");
p.Dispose();
}
private void Initialise()
{
_start.Visible = false;
_main.Visible = true;
_main.Enabled = true;
_listSubs.Items.Clear();
if (_project.Name != null)
{
_listSubs.Items.Add(_project.Name);
RegistryKey r = Registry.CurrentUser.OpenSubKey("Software", true).CreateSubKey("QBasic.Net");
int l = 1, temp;
string[] s = r.GetValueNames();
for (int I = 0; I < s.Length; I++)
{
if (int.TryParse(s[I].Replace("RecentProject", ""), out temp))
{
if ((string)r.GetValue(s[I]) != (_project.Location + "\\" + _project.Name + ".qproj"))
l++;
else
{
I = s.Length;
l = -1;
}
}
}
if (l > 0)
r.SetValue("RecentProject" + l, _project.Location + "\\" + _project.Name + ".qproj");
}
else
_listSubs.Items.Add("Main");
}
#region Open Project part
void _recentProjects_DoubleClick(object sender, System.EventArgs e)
{
if (!string.IsNullOrEmpty((string)_recentProjects.SelectedItem))
OpenProject((string)_recentProjects.SelectedItem);
}
private void _OpenProject_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty((string)_recentProjects.SelectedItem))
OpenProject((string)_recentProjects.SelectedItem);
else
_menuOpenProject_Click(null, null);
}
private void _menuOpenProject_Click(object sender, EventArgs e)
{
if (defaultFolder == "")
_openDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
else
_openDialog.InitialDirectory = defaultFolder;
_openDialog.Multiselect = false;
_openDialog.FileName = "";
if (_openDialog.ShowDialog() != DialogResult.Cancel)
OpenProject(_openDialog.FileName);
}
private void OpenProject(string ProjectFile)
{
_project.Location = new FileInfo(ProjectFile).DirectoryName;
_project.Code = new Code();
XmlDocument x = new XmlDocument();
x.PreserveWhitespace = true;
x.Load(ProjectFile);
XmlNodeReader r = new XmlNodeReader(x);
StreamReader read;
while (r.Read())
{
if (r.NodeType == XmlNodeType.Element)
{
switch (r.Name)
{
case "Name":
_project.Name = r.ReadElementContentAsString();
break;
case "Sub":
_project.Code.Subs.Add(new Sub(r.ReadElementContentAsString()));
read = new StreamReader(_project.Location + "\\" + r.ReadElementContentAsString() + ".bat");
_project.Code.Subs[r.ReadElementContentAsString()].Content = read.ReadToEnd();
read.Close();
break;
}
}
}
r.Close();
read = new StreamReader(_project.Location + "\\" + _project.Name + ".bat");
_project.Code.MainCode = read.ReadToEnd();
read.Close();
_main.Text = _project.Code.MainCode;
Initialise();
}
#endregion
private void _menuExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}