/*
* 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 rtfFile
{
public string loc
;
public RichTextBox rtfBox
;
public bool saved
;
public rtfFile
(string locN
)
{
loc
= locN
;
saved
= true;
rtfBox
= new RichTextBox
();
}
public void save
()
{
rtfBox
.SaveFile(loc
);
saved
= true;
}
public bool open
()
{
try
{
rtfBox
.LoadFile(loc
);
saved
= true;
}
catch (Exception ex
)
{
MessageBox
.Show(ex
.Message,
"Load Error", MessageBoxButtons
.OK, MessageBoxIcon
.Error);
return false;
}
return true;
}
}
public List
<rtfFile
> files
= new List
<rtfFile
>();
public int curFileID
;
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
();
}
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");
}
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
;
}
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
;
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");
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
();
}
}
}