/*
* Created by SharpDevelop.
* User: 06pfjn
* Date: 30/06/2010
* Time: 12:15
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace LassyPad
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm
: Form
{
public class word
{
public Color colour
;
public List
<string> words
;
public word
(Color colourN,
string wordsN
)
{
colour
= colourN
;
words
= new List
<string>();
string[] tempWords
= wordsN
.Split(',');
for (int i
= 0; i
< tempWords
.Length; i
++)
words
.Add(tempWords
[i
]);
}
}
public class comment
{
public Color colour
;
public string start
;
public string end
;
public comment
(Color colourN,
string startN,
string endN
)
{
colour
= colourN
;
start
= startN
;
end
= endN
;
}
public comment
(Color colourN,
string startN
)
{
colour
= colourN
;
start
= startN
;
end
= "\n";
}
}
public class rtfFile
{
public string loc
;
public RichTextBox rtfBox
;
public bool saved
;
public bool isRtf
;
public int syntax
;
public rtfFile
(string locN
)
{
loc
= locN
;
saved
= true;
rtfBox
= new RichTextBox
();
}
public void save
()
{
if (loc
.EndsWith(".rtf"))
rtfBox
.SaveFile(loc
);
else
{
string str
= rtfBox
.Rtf;
rtfBox
.Rtf = rtfBox
.Text;
rtfBox
.SaveFile(loc
);
rtfBox
.Rtf = str
;
}
saved
= true;
}
public bool open
()
{
try
{
saved
= true;
if (loc
.EndsWith(".rtf"))
{
rtfBox
.LoadFile(loc, RichTextBoxStreamType
.TextTextOleObjs);
isRtf
= true;
}
else
{
rtfBox
.LoadFile(loc, RichTextBoxStreamType
.PlainText);
isRtf
= false;
}
}
catch (Exception ex
)
{
MessageBox
.Show(ex
.Message,
"Load Error", MessageBoxButtons
.OK, MessageBoxIcon
.Error);
return false;
}
return true;
}
}
public class syntax
{
public string id
;
public List
<word
> words
;
public List
<comment
> comments
;
public syntax
(string idN
)
{
id
= idN
;
words
= new List
<MainForm
.word>();
comments
= new List
<MainForm
.comment>();
}
}
public List
<rtfFile
> files
= new List
<rtfFile
>();
public int curFileID
;
public List
<syntax
> syntaxes
= new List
<syntax
>();
public int selectionStart
;
public int selectionLength
;
public MainForm
()
{
InitializeComponent
();
}
void MainFormLoad
(object sender, EventArgs e
)
{
welcomeView
.Url = new Uri
("http://tim32.org/~freddie/LassyPad/Welcome.htm");
initAll
();
bool openRes
;
string[] envArgs
= System.Environment.GetCommandLineArgs();
for (int i
= 0; i
< envArgs
.Length; i
++)
{
if (envArgs
[i
].EndsWith(".rtf") || envArgs
[i
].EndsWith(".txt"))
{
files
.Add(new rtfFile
(envArgs
[i
]));
openRes
= files
[files
.Count - 1].open();
if (openRes
)
{
treeView
.Nodes["Files"].Nodes.Add(envArgs
[i
], envArgs
[i
]);
showFile
(files
.Count - 1);
}
else
files
.RemoveAt(files
.Count - 1);
}
}
}
public void initAll
()
{
initTreeView
();
loadSyntaxes
();
}
public void initTreeView
()
{
// welcome
treeView
.Nodes.Add("Welcome",
"Welcome");
treeView
.Nodes["Welcome"].Nodes.Add("Introduction",
"Introduction");
treeView
.Nodes["Welcome"].Nodes.Add("Help",
"Help");
//
treeView
.Nodes.Add("Files",
"Files");
}
public void loadSyntaxes
()
{
System.IO.StreamReader reader
= new System.IO.StreamReader("data/syntaxes.dat");
string r
= reader
.ReadToEnd();
string[] data
= r
.Split('\n');
string[] d, d2
;
for (int i
= 0; i
< data
.Length; i
++)
{
data
[i
] = data
[i
].Replace("\r",
"");
if (data
[i
].EndsWith(":"))
{
syntaxes
.Add(new syntax
(data
[i
].Substring(0, data
[i
].Length - 1)));
syntaxHighlightingToolStripMenuItem
.Items.Add(data
[i
].Substring(0, data
[i
].Length - 1));
}
else
{
d
= data
[i
].Split(' ');
switch (d
[0])
{
case ("word"):
{
if (d
.Length == 3)
syntaxes
[syntaxes
.Count - 1].words.Add(new word
(Color
.FromName(d
[1]), d
[2]));
else if (d
.Length == 5)
{
d2
= d
[1].Split(',');
syntaxes
[syntaxes
.Count - 1].words.Add(new word
(Color
.FromArgb(int.Parse(d2
[0]),
int.Parse(d2
[1]),
int.Parse(d2
[2])), d
[2]));
}
break;
}
}
}
}
}
void TreeViewNodeMouseClick
(object sender, TreeNodeMouseClickEventArgs e
)
{
if (e
.Node.Level == 0)
{
switch (e
.Node.Name)
{
case ("Welcome"):
{
welcomeView
.Url = new Uri
("http://tim32.org/~freddie/LassyPad/Welcome.htm");
break;
}
}
}
else
{
switch (e
.Node.Parent.Name)
{
case ("Welcome"):
{
if (e
.Node.Name == "Introduction")
welcomeView
.Url = new Uri
("http://tim32.org/~freddie/LassyPad/Introduction.htm");
else if (e
.Node.Name == "Help")
welcomeView
.Url = new Uri
("http://tim32.org/~freddie/LassyPad/Help.htm");
break;
}
case ("Files"):
{
showFile
(e
.Node.Name);
break;
}
}
}
}
// other functions
public void insertImageFromFile
()
{
DialogResult res
= openFileDialog1
.ShowDialog();
if (openFileDialog1
.FileName != "" && res
!= DialogResult
.Cancel)
{
// HELL - Get help from somewhere....
}
}
public void zoomIn
()
{
editorView
.ZoomFactor *= 1
.2f
;
}
public void zoomOut
()
{
editorView
.ZoomFactor /= 1
.2f
;
}
public void setSelection
()
{
editorView
.Select(selectionStart, selectionLength
);
}
public void showFile
(int fileID
)
{
tabControl
.SelectedIndex = 1;
editorView
.Rtf = files
[fileID
].rtfBox.Rtf;
curFileID
= fileID
;
if (files
[curFileID
].isRtf)
syntaxHighlightingToolStripMenuItem
.Enabled = false;
else
syntaxHighlightingToolStripMenuItem
.Enabled = true;
}
public void showFile
(string filename
)
{
tabControl
.SelectedIndex = 1;
for (int i
= 0; i
< files
.Count; i
++)
{
if (files
[i
].loc == filename
)
{
editorView
.Rtf = files
[i
].rtfBox.Rtf;
curFileID
= i
;
if (files
[curFileID
].isRtf)
syntaxHighlightingToolStripMenuItem
.Enabled = false;
else
syntaxHighlightingToolStripMenuItem
.Enabled = true;
return;
}
}
}
// file functions
public void newFile
()
{
int i
= 0;
bool exists
;
while (true)
{
exists
= false;
for (int j
= 0; j
< files
.Count; j
++)
{
if ((files
[j
].loc == "NewFile" + i
.ToString() + ".rtf"))
exists
= true;
}
if (!exists
)
{
files
.Add(new rtfFile
("NewFile" + i
.ToString() + ".rtf"));
treeView
.Nodes["Files"].Nodes.Add("NewFile" + i
.ToString() + ".rtf",
"NewFile" + i
.ToString() + ".rtf");
files
[files
.Count - 1].isRtf = true;
showFile
(files
.Count - 1);
return;
}
i
++;
}
}
public void renameFile
(string filename,
string newFilename
)
{
treeView
.Nodes["Files"].Nodes[filename
].Text = newFilename
;
treeView
.Nodes["Files"].Nodes[filename
].Name = newFilename
;
for (int i
= 0; i
< files
.Count; i
++)
{
if (files
[i
].loc == filename
)
files
[i
].loc = newFilename
;
}
}
public void renameFile
(int fileID,
string newFilename
)
{
treeView
.Nodes["Files"].Nodes[files
[fileID
].loc].Text = newFilename
;
treeView
.Nodes["Files"].Nodes[files
[fileID
].loc].Name = newFilename
;
files
[fileID
].loc = newFilename
;
}
public void openFile
()
{
bool openRes
;
DialogResult res
= openFileDialog1
.ShowDialog();
if (openFileDialog1
.FileName != "" && res
!= DialogResult
.Cancel)
{
files
.Add(new rtfFile
(openFileDialog1
.FileName));
openRes
= files
[files
.Count - 1].open();
if (openRes
)
{
treeView
.Nodes["Files"].Nodes.Add(openFileDialog1
.FileName, openFileDialog1
.FileName);
showFile
(files
.Count - 1);
}
else
files
.RemoveAt(files
.Count - 1);
}
}
public void closeFile
()
{
if (curFileID
< files
.Count && curFileID
> -1)
{
treeView
.Nodes["Files"].Nodes.RemoveByKey(files
[curFileID
].loc);
files
.RemoveAt(curFileID
);
if (curFileID
>= files
.Count)
curFileID
--;
if (files
.Count > 0)
showFile
(curFileID
);
else
tabControl
.SelectedIndex = 0;
}
}
public void saveAll
()
{
for (int i
= 0; i
< files
.Count; i
++)
files
[i
].save();
}
// format functions
public void addStyle
(FontStyle newStyles
)
{
FontStyle fStyle
= editorView
.SelectionFont.Style;
editorView
.SelectionFont = new Font
(editorView
.SelectionFont, fStyle
| newStyles
);
}
public void remStyle
(FontStyle remStyles
)
{
FontStyle fStyle
= editorView
.SelectionFont.Style;
fStyle
-= remStyles
;
editorView
.SelectionFont = new Font
(editorView
.SelectionFont, fStyle
);
}
// form events
void EditorViewTextChanged
(object sender, EventArgs e
)
{
if (curFileID
< files
.Count && curFileID
> -1)
{
files
[curFileID
].rtfBox.Rtf = editorView
.Rtf;
files
[curFileID
].saved = false;
}
}
void EditorViewSelectionChanged
(object sender, EventArgs e
)
{
selectionStart
= editorView
.SelectionStart;
selectionLength
= editorView
.SelectionLength;
textColourF
.ForeColor = editorView
.SelectionColor;
// sizeF.Text = editorView.SelectionFont.Size.ToString();
}
#region File stuff
void ToolStripButton1Click
(object sender, EventArgs e
)
{
newFile
();
}
void NewToolStripMenuItemClick
(object sender, EventArgs e
)
{
newFile
();
}
void ToolStripButton2Click
(object sender, EventArgs e
)
{
openFile
();
}
void OpenToolStripMenuItemClick
(object sender, EventArgs e
)
{
openFile
();
}
void ToolStripButton3Click
(object sender, EventArgs e
)
{
files
[curFileID
].save();
}
void SaveToolStripMenuItemClick
(object sender, EventArgs e
)
{
files
[curFileID
].save();
}
void ToolStripButton4Click
(object sender, EventArgs e
)
{
closeFile
();
}
void SaveAsToolStripMenuItemClick
(object sender, EventArgs e
)
{
DialogResult res
= saveFileDialog1
.ShowDialog();
if (saveFileDialog1
.FileName != "" && res
!= DialogResult
.Cancel)
{
renameFile
(curFileID, saveFileDialog1
.FileName);
files
[curFileID
].loc = saveFileDialog1
.FileName;
files
[curFileID
].save();
}
}
#endregion
#region Format stuff
void BoldFClick
(object sender, EventArgs e
)
{
if (editorView
.SelectionFont.Bold)
remStyle
(FontStyle
.Bold);
else
addStyle
(FontStyle
.Bold);
}
void ItalicFClick
(object sender, EventArgs e
)
{
if (editorView
.SelectionFont.Italic)
remStyle
(FontStyle
.Italic);
else
addStyle
(FontStyle
.Italic);
}
void UnderlineFClick
(object sender, EventArgs e
)
{
if (editorView
.SelectionFont.Underline)
remStyle
(FontStyle
.Underline);
else
addStyle
(FontStyle
.Underline);
}
void ToolStripButton5Click
(object sender, EventArgs e
)
{
editorView
.SelectionAlignment = HorizontalAlignment
.Left;
}
void ToolStripButton6Click
(object sender, EventArgs e
)
{
editorView
.SelectionAlignment = HorizontalAlignment
.Center;
}
void ToolStripButton7Click
(object sender, EventArgs e
)
{
editorView
.SelectionAlignment = HorizontalAlignment
.Right;
}
void SizeFTextChanged
(object sender, EventArgs e
)
{
setSelection
();
FontStyle fStyle
= editorView
.SelectionFont.Style;
float size
= 0
.0f
;
if (float.TryParse(sizeF
.Text,
out size
))
{
if (size
> 0)
editorView
.SelectionFont = new Font
(editorView
.SelectionFont.FontFamily, size, fStyle
);
}
}
#endregion
void WordWrapToolStripMenuItemClick
(object sender, EventArgs e
)
{
editorView
.WordWrap = ! editorView
.WordWrap;
wordWrapToolStripMenuItem
.Checked = editorView
.WordWrap;
}
void ToolStripButton8Click
(object sender, EventArgs e
)
{
insertImageFromFile
();
}
#region editorMenuStrip
void CloseFileToolStripMenuItemClick
(object sender, EventArgs e
)
{
closeFile
();
}
void SaveFileToolStripMenuItemClick
(object sender, EventArgs e
)
{
files
[curFileID
].save();
}
void InToolStripMenuItemClick
(object sender, EventArgs e
)
{
zoomIn
();
}
void OutToolStripMenuItemClick
(object sender, EventArgs e
)
{
zoomOut
();
}
#endregion
void TextColourFClick
(object sender, EventArgs e
)
{
DialogResult res
= colorDialog1
.ShowDialog();
if (res
!= DialogResult
.Cancel)
{
textColourF
.ForeColor = colorDialog1
.Color;
editorView
.SelectionColor = colorDialog1
.Color;
}
}
void ToolStripButton9Click
(object sender, EventArgs e
)
{
saveAll
();
}
void TabControlSelectedIndexChanged
(object sender, EventArgs e
)
{
if (tabControl
.SelectedIndex == 1 && files
.Count == 0)
{
newFile
();
}
}
void SyntaxHighlightingToolStripMenuItemSelectedIndexChanged
(object sender, EventArgs e
)
{
// clear own highlighting
files
[curFileID
].rtfBox.Rtf = editorView
.Text;
files
[curFileID
].syntax = -1;
// sort out new stuff
if (syntaxHighlightingToolStripMenuItem
.SelectedIndex == 1)
{
for (int i
= 0; i
< syntaxes
.Count; i
++)
{
if (files
[curFileID
].loc.EndsWith(syntaxes
[i
].id))
{
files
[curFileID
].syntax = i
;
break;
}
}
}
else
files
[curFileID
].syntax = syntaxHighlightingToolStripMenuItem
.SelectedIndex - 2;
applySyntax
(curFileID
);
showFile
(curFileID
);
}
public void applySyntax
(int fileID
)
{
if (files
[fileID
].isRtf)
return;
if (files
[fileID
].syntax == -1)
files
[fileID
].rtfBox.Rtf = files
[fileID
].rtfBox.Text;
else
{
int pos
;
string[] data
= files
[fileID
].rtfBox.Text.Split(new char[] { ' ',
'\n' });
for (int i
= 0; i
< syntaxes
[files
[fileID
].syntax].words.Count; i
++)
{
pos
= 0;
for (int j
= 0; j
< data
.Length; j
++)
{
if (syntaxes
[files
[fileID
].syntax].words[i
].words.Contains(data
[j
]))
{
files
[fileID
].rtfBox.Select(pos, data
[j
].Length);
files
[fileID
].rtfBox.SelectionColor = syntaxes
[files
[fileID
].syntax].words[i
].colour;
}
pos
+= data
[j
].Length + 1;
}
}
}
}
}
}