Rev 2 | Go to most recent revision | Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | freddie | 1 | /* |
| 2 | * Created by SharpDevelop. |
||
| 3 | * User: 06pfjn |
||
| 4 | * Date: 30/06/2010 |
||
| 5 | * Time: 12:15 |
||
| 6 | * |
||
| 7 | * To change this template use Tools | Options | Coding | Edit Standard Headers. |
||
| 8 | */ |
||
| 9 | using System; |
||
| 10 | using System.Collections.Generic; |
||
| 11 | using System.Drawing; |
||
| 12 | using System.Windows.Forms; |
||
| 13 | |||
| 14 | namespace LassyPad |
||
| 15 | { |
||
| 16 | /// <summary> |
||
| 17 | /// Description of MainForm. |
||
| 18 | /// </summary> |
||
| 19 | public partial class MainForm : Form |
||
| 20 | { |
||
| 21 | |||
| 22 | public class rtfFile |
||
| 23 | { |
||
| 24 | public string loc; |
||
| 25 | public RichTextBox rtfBox; |
||
| 26 | public bool saved; |
||
| 27 | |||
| 28 | public rtfFile(string locN) |
||
| 29 | { |
||
| 30 | loc = locN; |
||
| 31 | saved = true; |
||
| 32 | rtfBox = new RichTextBox(); |
||
| 33 | } |
||
| 34 | |||
| 35 | public void save() |
||
| 36 | { |
||
| 37 | rtfBox.SaveFile(loc); |
||
| 38 | saved = true; |
||
| 39 | } |
||
| 40 | |||
| 41 | public void open() |
||
| 42 | { |
||
| 43 | try |
||
| 44 | { |
||
| 45 | rtfBox.LoadFile(loc); |
||
| 46 | saved = true; |
||
| 47 | } |
||
| 48 | catch (Exception ex) |
||
| 49 | { |
||
| 50 | MessageBox.Show(ex.Message, "Load Error", MessageBoxButtons.OK, MessageBoxIcon.Error); |
||
| 51 | } |
||
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | public List<rtfFile> files = new List<rtfFile>(); |
||
| 56 | public int curFileID; |
||
| 57 | |||
| 58 | public int selectionStart; |
||
| 59 | public int selectionLength; |
||
| 60 | |||
| 61 | public MainForm() |
||
| 62 | { |
||
| 63 | |||
| 64 | InitializeComponent(); |
||
| 65 | |||
| 66 | } |
||
| 67 | |||
| 68 | void MainFormLoad(object sender, EventArgs e) |
||
| 69 | { |
||
| 70 | welcomeView.Url = new Uri("http://tim32.org/~freddie/LassyPad/Welcome.htm"); |
||
| 71 | initAll(); |
||
| 72 | } |
||
| 73 | |||
| 74 | public void initAll() |
||
| 75 | { |
||
| 76 | initTreeView(); |
||
| 77 | } |
||
| 78 | |||
| 79 | public void initTreeView() |
||
| 80 | { |
||
| 81 | // welcome |
||
| 82 | treeView.Nodes.Add("Welcome", "Welcome"); |
||
| 83 | treeView.Nodes["Welcome"].Nodes.Add("Introduction", "Introduction"); |
||
| 84 | treeView.Nodes["Welcome"].Nodes.Add("Help", "Help"); |
||
| 85 | |||
| 86 | // |
||
| 87 | treeView.Nodes.Add("Files", "Files"); |
||
| 88 | } |
||
| 89 | |||
| 90 | void TreeViewNodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) |
||
| 91 | { |
||
| 92 | if (e.Node.Level == 0) |
||
| 93 | { |
||
| 94 | switch (e.Node.Name) |
||
| 95 | { |
||
| 96 | case ("Welcome"): |
||
| 97 | { |
||
| 98 | welcomeView.Url = new Uri("http://tim32.org/~freddie/LassyPad/Welcome.htm"); |
||
| 99 | break; |
||
| 100 | } |
||
| 101 | } |
||
| 102 | } |
||
| 103 | else |
||
| 104 | { |
||
| 105 | switch (e.Node.Parent.Name) |
||
| 106 | { |
||
| 107 | case ("Welcome"): |
||
| 108 | { |
||
| 109 | if (e.Node.Name == "Introduction") |
||
| 110 | welcomeView.Url = new Uri("http://tim32.org/~freddie/LassyPad/Introduction.htm"); |
||
| 111 | else if (e.Node.Name == "Help") |
||
| 112 | welcomeView.Url = new Uri("http://tim32.org/~freddie/LassyPad/Help.htm"); |
||
| 113 | |||
| 114 | break; |
||
| 115 | } |
||
| 116 | case ("Files"): |
||
| 117 | { |
||
| 118 | showFile(e.Node.Name); |
||
| 119 | break; |
||
| 120 | } |
||
| 121 | } |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | // other functions |
||
| 126 | |||
| 127 | public void setSelection() |
||
| 128 | { |
||
| 129 | editorView.Select(selectionStart, selectionLength); |
||
| 130 | } |
||
| 131 | |||
| 132 | public void showFile(int fileID) |
||
| 133 | { |
||
| 134 | tabControl.SelectedIndex = 1; |
||
| 135 | editorView.Rtf = files[fileID].rtfBox.Rtf; |
||
| 136 | curFileID = fileID; |
||
| 137 | } |
||
| 138 | |||
| 139 | public void showFile(string filename) |
||
| 140 | { |
||
| 141 | tabControl.SelectedIndex = 1; |
||
| 142 | for (int i = 0; i < files.Count; i++) |
||
| 143 | { |
||
| 144 | // editorView.Text = files[i].rtfBox.Text; |
||
| 145 | //editorView = files[i].rtfBox; |
||
| 146 | curFileID = i; |
||
| 147 | return; |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | // file functions |
||
| 152 | |||
| 153 | public void newFile() |
||
| 154 | { |
||
| 155 | int i = 0; |
||
| 156 | while (true) |
||
| 157 | { |
||
| 158 | for (int j = 0; j < files.Count; j++) |
||
| 159 | { |
||
| 160 | if (!(files[j].loc == "NewFile" + i.ToString() + ".rtf")) |
||
| 161 | { |
||
| 162 | files.Add(new rtfFile("NewFile" + i.ToString() + ".rtf")); |
||
| 163 | treeView.Nodes["Files"].Nodes.Add("NewFile" + i.ToString() + ".rtf", "NewFile" + i.ToString() + ".rtf"); |
||
| 164 | showFile(files.Count - 1); |
||
| 165 | return; |
||
| 166 | } |
||
| 167 | } |
||
| 168 | i++; |
||
| 169 | } |
||
| 170 | |||
| 171 | } |
||
| 172 | |||
| 173 | public void renameFile(string filename, string newFilename) |
||
| 174 | { |
||
| 175 | for (int i = 0; i < files.Count; i++) |
||
| 176 | { |
||
| 177 | if (files[i].loc == filename) |
||
| 178 | files[i].loc = newFilename; |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | public void openFile() |
||
| 183 | { |
||
| 184 | DialogResult res = openFileDialog1.ShowDialog(); |
||
| 185 | if (openFileDialog1.FileName != "" && res != DialogResult.Cancel) |
||
| 186 | { |
||
| 187 | files.Add(new rtfFile(openFileDialog1.FileName)); |
||
| 188 | files[files.Count - 1].open(); |
||
| 189 | treeView.Nodes["Files"].Nodes.Add(openFileDialog1.FileName, openFileDialog1.FileName); |
||
| 190 | } |
||
| 191 | showFile(files.Count - 1); |
||
| 192 | } |
||
| 193 | |||
| 194 | public void closeFile() |
||
| 195 | { |
||
| 196 | if (curFileID < files.Count && curFileID > -1) |
||
| 197 | { |
||
| 198 | treeView.Nodes["Files"].Nodes.RemoveByKey(files[curFileID].loc); |
||
| 199 | files.RemoveAt(curFileID); |
||
| 200 | if (curFileID >= files.Count) |
||
| 201 | curFileID--; |
||
| 202 | if (files.Count > 0) |
||
| 203 | showFile(curFileID); |
||
| 204 | else |
||
| 205 | tabControl.SelectedIndex = 0; |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | // format functions |
||
| 210 | |||
| 211 | public void addStyle(FontStyle newStyles) |
||
| 212 | { |
||
| 213 | FontStyle fStyle = editorView.SelectionFont.Style; |
||
| 214 | editorView.SelectionFont = new Font(editorView.SelectionFont, fStyle | newStyles); |
||
| 215 | } |
||
| 216 | |||
| 217 | public void remStyle(FontStyle remStyles) |
||
| 218 | { |
||
| 219 | FontStyle fStyle = editorView.SelectionFont.Style; |
||
| 220 | fStyle -= remStyles; |
||
| 221 | editorView.SelectionFont = new Font(editorView.SelectionFont, fStyle); |
||
| 222 | } |
||
| 223 | |||
| 224 | // form events |
||
| 225 | |||
| 226 | void EditorViewTextChanged(object sender, EventArgs e) |
||
| 227 | { |
||
| 228 | if (curFileID < files.Count && curFileID > -1) |
||
| 229 | { |
||
| 230 | files[curFileID].rtfBox.Rtf = editorView.Rtf; |
||
| 231 | files[curFileID].saved = false; |
||
| 232 | } |
||
| 233 | } |
||
| 234 | |||
| 235 | void EditorViewSelectionChanged(object sender, EventArgs e) |
||
| 236 | { |
||
| 237 | selectionStart = editorView.SelectionStart; |
||
| 238 | selectionLength = editorView.SelectionLength; |
||
| 239 | sizeF.Text = editorView.SelectionFont.Size.ToString(); |
||
| 240 | } |
||
| 241 | |||
| 242 | #region File stuff |
||
| 243 | |||
| 244 | void ToolStripButton1Click(object sender, EventArgs e) |
||
| 245 | { |
||
| 246 | newFile(); |
||
| 247 | } |
||
| 248 | |||
| 249 | void NewToolStripMenuItemClick(object sender, EventArgs e) |
||
| 250 | { |
||
| 251 | newFile(); |
||
| 252 | } |
||
| 253 | |||
| 254 | void ToolStripButton2Click(object sender, EventArgs e) |
||
| 255 | { |
||
| 256 | openFile(); |
||
| 257 | } |
||
| 258 | |||
| 259 | void OpenToolStripMenuItemClick(object sender, EventArgs e) |
||
| 260 | { |
||
| 261 | openFile(); |
||
| 262 | } |
||
| 263 | |||
| 264 | void ToolStripButton3Click(object sender, EventArgs e) |
||
| 265 | { |
||
| 266 | files[curFileID].save(); |
||
| 267 | } |
||
| 268 | |||
| 269 | void SaveToolStripMenuItemClick(object sender, EventArgs e) |
||
| 270 | { |
||
| 271 | files[curFileID].save(); |
||
| 272 | } |
||
| 273 | |||
| 274 | void ToolStripButton4Click(object sender, EventArgs e) |
||
| 275 | { |
||
| 276 | closeFile(); |
||
| 277 | } |
||
| 278 | |||
| 279 | #endregion |
||
| 280 | |||
| 281 | #region Format stuff |
||
| 282 | |||
| 283 | void BoldFClick(object sender, EventArgs e) |
||
| 284 | { |
||
| 285 | if (editorView.SelectionFont.Bold) |
||
| 286 | remStyle(FontStyle.Bold); |
||
| 287 | else |
||
| 288 | addStyle(FontStyle.Bold); |
||
| 289 | } |
||
| 290 | |||
| 291 | void ItalicFClick(object sender, EventArgs e) |
||
| 292 | { |
||
| 293 | if (editorView.SelectionFont.Italic) |
||
| 294 | remStyle(FontStyle.Italic); |
||
| 295 | else |
||
| 296 | addStyle(FontStyle.Italic); |
||
| 297 | } |
||
| 298 | |||
| 299 | void UnderlineFClick(object sender, EventArgs e) |
||
| 300 | { |
||
| 301 | if (editorView.SelectionFont.Underline) |
||
| 302 | remStyle(FontStyle.Underline); |
||
| 303 | else |
||
| 304 | addStyle(FontStyle.Underline); |
||
| 305 | } |
||
| 306 | |||
| 307 | void ToolStripButton5Click(object sender, EventArgs e) |
||
| 308 | { |
||
| 309 | editorView.SelectionAlignment = HorizontalAlignment.Left; |
||
| 310 | } |
||
| 311 | |||
| 312 | void ToolStripButton6Click(object sender, EventArgs e) |
||
| 313 | { |
||
| 314 | editorView.SelectionAlignment = HorizontalAlignment.Center; |
||
| 315 | } |
||
| 316 | |||
| 317 | void ToolStripButton7Click(object sender, EventArgs e) |
||
| 318 | { |
||
| 319 | editorView.SelectionAlignment = HorizontalAlignment.Right; |
||
| 320 | } |
||
| 321 | |||
| 322 | void SizeFTextChanged(object sender, EventArgs e) |
||
| 323 | { |
||
| 324 | setSelection(); |
||
| 325 | FontStyle fStyle = editorView.SelectionFont.Style; |
||
| 326 | float size = 0.0f; |
||
| 327 | if (float.TryParse(sizeF.Text, out size)) |
||
| 328 | { |
||
| 329 | if (size > 0) |
||
| 330 | editorView.SelectionFont = new Font(editorView.SelectionFont.FontFamily, size, fStyle); |
||
| 331 | } |
||
| 332 | } |
||
| 333 | |||
| 334 | #endregion |
||
| 335 | |||
| 336 | void WordWrapToolStripMenuItemClick(object sender, EventArgs e) |
||
| 337 | { |
||
| 338 | editorView.WordWrap = ! editorView.WordWrap; |
||
| 339 | wordWrapToolStripMenuItem.Checked = editorView.WordWrap; |
||
| 340 | } |
||
| 341 | } |
||
| 342 | } |