Rev 7 | Rev 9 | Go to most recent revision | Details | Compare with Previous | 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 | |||
4 | freddie | 41 | public bool open() |
1 | freddie | 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); |
||
4 | freddie | 51 | return false; |
1 | freddie | 52 | } |
4 | freddie | 53 | return true; |
1 | freddie | 54 | } |
55 | } |
||
56 | |||
57 | public List<rtfFile> files = new List<rtfFile>(); |
||
58 | public int curFileID; |
||
59 | |||
60 | public int selectionStart; |
||
61 | public int selectionLength; |
||
62 | |||
63 | public MainForm() |
||
64 | { |
||
65 | |||
66 | InitializeComponent(); |
||
67 | |||
68 | } |
||
69 | |||
70 | void MainFormLoad(object sender, EventArgs e) |
||
71 | { |
||
72 | welcomeView.Url = new Uri("http://tim32.org/~freddie/LassyPad/Welcome.htm"); |
||
73 | initAll(); |
||
7 | freddie | 74 | |
75 | bool openRes; |
||
76 | string[] envArgs = System.Environment.GetCommandLineArgs(); |
||
77 | for (int i = 0; i < envArgs.Length; i++) |
||
78 | { |
||
79 | if (envArgs[i].EndsWith(".rtf") || envArgs[i].EndsWith(".txt")) |
||
80 | { |
||
81 | files.Add(new rtfFile(envArgs[i])); |
||
82 | openRes = files[files.Count - 1].open(); |
||
83 | if (openRes) |
||
84 | { |
||
85 | treeView.Nodes["Files"].Nodes.Add(envArgs[i], envArgs[i]); |
||
86 | showFile(files.Count - 1); |
||
87 | } |
||
88 | else |
||
89 | files.RemoveAt(files.Count - 1); |
||
90 | } |
||
91 | } |
||
1 | freddie | 92 | } |
93 | |||
94 | public void initAll() |
||
95 | { |
||
96 | initTreeView(); |
||
97 | } |
||
98 | |||
99 | public void initTreeView() |
||
100 | { |
||
101 | // welcome |
||
102 | treeView.Nodes.Add("Welcome", "Welcome"); |
||
103 | treeView.Nodes["Welcome"].Nodes.Add("Introduction", "Introduction"); |
||
104 | treeView.Nodes["Welcome"].Nodes.Add("Help", "Help"); |
||
105 | |||
106 | // |
||
107 | treeView.Nodes.Add("Files", "Files"); |
||
108 | } |
||
109 | |||
110 | void TreeViewNodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) |
||
111 | { |
||
112 | if (e.Node.Level == 0) |
||
113 | { |
||
114 | switch (e.Node.Name) |
||
115 | { |
||
116 | case ("Welcome"): |
||
117 | { |
||
118 | welcomeView.Url = new Uri("http://tim32.org/~freddie/LassyPad/Welcome.htm"); |
||
119 | break; |
||
120 | } |
||
121 | } |
||
122 | } |
||
123 | else |
||
124 | { |
||
125 | switch (e.Node.Parent.Name) |
||
126 | { |
||
127 | case ("Welcome"): |
||
128 | { |
||
129 | if (e.Node.Name == "Introduction") |
||
130 | welcomeView.Url = new Uri("http://tim32.org/~freddie/LassyPad/Introduction.htm"); |
||
131 | else if (e.Node.Name == "Help") |
||
132 | welcomeView.Url = new Uri("http://tim32.org/~freddie/LassyPad/Help.htm"); |
||
133 | |||
134 | break; |
||
135 | } |
||
136 | case ("Files"): |
||
137 | { |
||
138 | showFile(e.Node.Name); |
||
139 | break; |
||
140 | } |
||
141 | } |
||
142 | } |
||
143 | } |
||
144 | |||
145 | // other functions |
||
146 | |||
7 | freddie | 147 | public void insertImageFromFile() |
148 | { |
||
149 | DialogResult res = openFileDialog1.ShowDialog(); |
||
150 | if (openFileDialog1.FileName != "" && res != DialogResult.Cancel) |
||
151 | { |
||
152 | // HELL - Get help from somewhere.... |
||
153 | } |
||
154 | } |
||
155 | |||
156 | public void zoomIn() |
||
157 | { |
||
158 | editorView.ZoomFactor *= 1.2f; |
||
159 | } |
||
160 | |||
161 | public void zoomOut() |
||
162 | { |
||
163 | editorView.ZoomFactor /= 1.2f; |
||
164 | } |
||
165 | |||
1 | freddie | 166 | public void setSelection() |
167 | { |
||
168 | editorView.Select(selectionStart, selectionLength); |
||
169 | } |
||
170 | |||
171 | public void showFile(int fileID) |
||
172 | { |
||
173 | tabControl.SelectedIndex = 1; |
||
174 | editorView.Rtf = files[fileID].rtfBox.Rtf; |
||
175 | curFileID = fileID; |
||
176 | } |
||
177 | |||
178 | public void showFile(string filename) |
||
179 | { |
||
180 | tabControl.SelectedIndex = 1; |
||
181 | for (int i = 0; i < files.Count; i++) |
||
182 | { |
||
3 | freddie | 183 | if (files[i].loc == filename) |
184 | { |
||
185 | editorView.Rtf = files[i].rtfBox.Rtf; |
||
186 | curFileID = i; |
||
187 | return; |
||
188 | } |
||
1 | freddie | 189 | } |
190 | } |
||
191 | |||
192 | // file functions |
||
193 | |||
194 | public void newFile() |
||
195 | { |
||
196 | int i = 0; |
||
3 | freddie | 197 | bool exists; |
1 | freddie | 198 | while (true) |
199 | { |
||
3 | freddie | 200 | exists = false; |
1 | freddie | 201 | for (int j = 0; j < files.Count; j++) |
202 | { |
||
3 | freddie | 203 | if ((files[j].loc == "NewFile" + i.ToString() + ".rtf")) |
204 | exists = true; |
||
1 | freddie | 205 | } |
3 | freddie | 206 | if (!exists) |
207 | { |
||
208 | files.Add(new rtfFile("NewFile" + i.ToString() + ".rtf")); |
||
209 | treeView.Nodes["Files"].Nodes.Add("NewFile" + i.ToString() + ".rtf", "NewFile" + i.ToString() + ".rtf"); |
||
210 | showFile(files.Count - 1); |
||
211 | return; |
||
212 | } |
||
1 | freddie | 213 | i++; |
214 | } |
||
215 | |||
216 | } |
||
217 | |||
218 | public void renameFile(string filename, string newFilename) |
||
219 | { |
||
3 | freddie | 220 | treeView.Nodes["Files"].Nodes[filename].Text = newFilename; |
221 | treeView.Nodes["Files"].Nodes[filename].Name = newFilename; |
||
1 | freddie | 222 | for (int i = 0; i < files.Count; i++) |
223 | { |
||
224 | if (files[i].loc == filename) |
||
225 | files[i].loc = newFilename; |
||
226 | } |
||
227 | } |
||
228 | |||
3 | freddie | 229 | public void renameFile(int fileID, string newFilename) |
230 | { |
||
231 | treeView.Nodes["Files"].Nodes[files[fileID].loc].Text = newFilename; |
||
232 | treeView.Nodes["Files"].Nodes[files[fileID].loc].Name = newFilename; |
||
233 | files[fileID].loc = newFilename; |
||
234 | } |
||
235 | |||
1 | freddie | 236 | public void openFile() |
237 | { |
||
4 | freddie | 238 | bool openRes; |
1 | freddie | 239 | DialogResult res = openFileDialog1.ShowDialog(); |
240 | if (openFileDialog1.FileName != "" && res != DialogResult.Cancel) |
||
241 | { |
||
242 | files.Add(new rtfFile(openFileDialog1.FileName)); |
||
4 | freddie | 243 | openRes = files[files.Count - 1].open(); |
244 | if (openRes) |
||
245 | { |
||
246 | treeView.Nodes["Files"].Nodes.Add(openFileDialog1.FileName, openFileDialog1.FileName); |
||
247 | showFile(files.Count - 1); |
||
248 | } |
||
249 | else |
||
250 | files.RemoveAt(files.Count - 1); |
||
1 | freddie | 251 | } |
252 | } |
||
253 | |||
254 | public void closeFile() |
||
255 | { |
||
256 | if (curFileID < files.Count && curFileID > -1) |
||
257 | { |
||
258 | treeView.Nodes["Files"].Nodes.RemoveByKey(files[curFileID].loc); |
||
259 | files.RemoveAt(curFileID); |
||
260 | if (curFileID >= files.Count) |
||
261 | curFileID--; |
||
262 | if (files.Count > 0) |
||
263 | showFile(curFileID); |
||
264 | else |
||
265 | tabControl.SelectedIndex = 0; |
||
266 | } |
||
267 | } |
||
268 | |||
8 | freddie | 269 | public void saveAll() |
270 | { |
||
271 | for (int i = 0; i < files.Count; i++) |
||
272 | files[i].save(); |
||
273 | } |
||
274 | |||
1 | freddie | 275 | // format functions |
276 | |||
277 | public void addStyle(FontStyle newStyles) |
||
278 | { |
||
279 | FontStyle fStyle = editorView.SelectionFont.Style; |
||
280 | editorView.SelectionFont = new Font(editorView.SelectionFont, fStyle | newStyles); |
||
281 | } |
||
282 | |||
283 | public void remStyle(FontStyle remStyles) |
||
284 | { |
||
285 | FontStyle fStyle = editorView.SelectionFont.Style; |
||
286 | fStyle -= remStyles; |
||
287 | editorView.SelectionFont = new Font(editorView.SelectionFont, fStyle); |
||
288 | } |
||
289 | |||
290 | // form events |
||
291 | |||
292 | void EditorViewTextChanged(object sender, EventArgs e) |
||
293 | { |
||
294 | if (curFileID < files.Count && curFileID > -1) |
||
295 | { |
||
296 | files[curFileID].rtfBox.Rtf = editorView.Rtf; |
||
297 | files[curFileID].saved = false; |
||
298 | } |
||
299 | } |
||
300 | |||
301 | void EditorViewSelectionChanged(object sender, EventArgs e) |
||
302 | { |
||
303 | selectionStart = editorView.SelectionStart; |
||
304 | selectionLength = editorView.SelectionLength; |
||
7 | freddie | 305 | textColourF.ForeColor = editorView.SelectionColor; |
2 | tom | 306 | // sizeF.Text = editorView.SelectionFont.Size.ToString(); |
1 | freddie | 307 | } |
308 | |||
309 | #region File stuff |
||
310 | |||
311 | void ToolStripButton1Click(object sender, EventArgs e) |
||
312 | { |
||
313 | newFile(); |
||
314 | } |
||
315 | |||
316 | void NewToolStripMenuItemClick(object sender, EventArgs e) |
||
317 | { |
||
318 | newFile(); |
||
319 | } |
||
320 | |||
321 | void ToolStripButton2Click(object sender, EventArgs e) |
||
322 | { |
||
323 | openFile(); |
||
324 | } |
||
325 | |||
326 | void OpenToolStripMenuItemClick(object sender, EventArgs e) |
||
327 | { |
||
328 | openFile(); |
||
329 | } |
||
330 | |||
331 | void ToolStripButton3Click(object sender, EventArgs e) |
||
332 | { |
||
333 | files[curFileID].save(); |
||
334 | } |
||
335 | |||
336 | void SaveToolStripMenuItemClick(object sender, EventArgs e) |
||
337 | { |
||
338 | files[curFileID].save(); |
||
339 | } |
||
340 | |||
341 | void ToolStripButton4Click(object sender, EventArgs e) |
||
342 | { |
||
343 | closeFile(); |
||
344 | } |
||
345 | |||
3 | freddie | 346 | void SaveAsToolStripMenuItemClick(object sender, EventArgs e) |
347 | { |
||
348 | DialogResult res = saveFileDialog1.ShowDialog(); |
||
349 | if (saveFileDialog1.FileName != "" && res != DialogResult.Cancel) |
||
350 | { |
||
351 | renameFile(curFileID, saveFileDialog1.FileName); |
||
352 | files[curFileID].loc = saveFileDialog1.FileName; |
||
353 | files[curFileID].save(); |
||
354 | } |
||
355 | } |
||
356 | |||
1 | freddie | 357 | #endregion |
358 | |||
359 | #region Format stuff |
||
360 | |||
361 | void BoldFClick(object sender, EventArgs e) |
||
362 | { |
||
363 | if (editorView.SelectionFont.Bold) |
||
364 | remStyle(FontStyle.Bold); |
||
365 | else |
||
366 | addStyle(FontStyle.Bold); |
||
367 | } |
||
368 | |||
369 | void ItalicFClick(object sender, EventArgs e) |
||
370 | { |
||
371 | if (editorView.SelectionFont.Italic) |
||
372 | remStyle(FontStyle.Italic); |
||
373 | else |
||
374 | addStyle(FontStyle.Italic); |
||
375 | } |
||
376 | |||
377 | void UnderlineFClick(object sender, EventArgs e) |
||
378 | { |
||
379 | if (editorView.SelectionFont.Underline) |
||
380 | remStyle(FontStyle.Underline); |
||
381 | else |
||
382 | addStyle(FontStyle.Underline); |
||
383 | } |
||
384 | |||
385 | void ToolStripButton5Click(object sender, EventArgs e) |
||
386 | { |
||
387 | editorView.SelectionAlignment = HorizontalAlignment.Left; |
||
388 | } |
||
389 | |||
390 | void ToolStripButton6Click(object sender, EventArgs e) |
||
391 | { |
||
392 | editorView.SelectionAlignment = HorizontalAlignment.Center; |
||
393 | } |
||
394 | |||
395 | void ToolStripButton7Click(object sender, EventArgs e) |
||
396 | { |
||
397 | editorView.SelectionAlignment = HorizontalAlignment.Right; |
||
398 | } |
||
399 | |||
400 | void SizeFTextChanged(object sender, EventArgs e) |
||
401 | { |
||
402 | setSelection(); |
||
403 | FontStyle fStyle = editorView.SelectionFont.Style; |
||
404 | float size = 0.0f; |
||
405 | if (float.TryParse(sizeF.Text, out size)) |
||
406 | { |
||
407 | if (size > 0) |
||
408 | editorView.SelectionFont = new Font(editorView.SelectionFont.FontFamily, size, fStyle); |
||
409 | } |
||
410 | } |
||
411 | |||
412 | #endregion |
||
413 | |||
414 | void WordWrapToolStripMenuItemClick(object sender, EventArgs e) |
||
415 | { |
||
416 | editorView.WordWrap = ! editorView.WordWrap; |
||
417 | wordWrapToolStripMenuItem.Checked = editorView.WordWrap; |
||
418 | } |
||
6 | freddie | 419 | |
420 | void ToolStripButton8Click(object sender, EventArgs e) |
||
421 | { |
||
7 | freddie | 422 | insertImageFromFile(); |
6 | freddie | 423 | } |
7 | freddie | 424 | |
425 | #region editorMenuStrip |
||
426 | |||
427 | void CloseFileToolStripMenuItemClick(object sender, EventArgs e) |
||
428 | { |
||
429 | closeFile(); |
||
430 | } |
||
431 | |||
432 | void SaveFileToolStripMenuItemClick(object sender, EventArgs e) |
||
433 | { |
||
434 | files[curFileID].save(); |
||
435 | } |
||
436 | |||
437 | void InToolStripMenuItemClick(object sender, EventArgs e) |
||
438 | { |
||
439 | zoomIn(); |
||
440 | } |
||
441 | |||
442 | void OutToolStripMenuItemClick(object sender, EventArgs e) |
||
443 | { |
||
444 | zoomOut(); |
||
445 | } |
||
446 | |||
447 | #endregion |
||
448 | |||
8 | freddie | 449 | void TextColourFClick(object sender, EventArgs e) |
7 | freddie | 450 | { |
451 | DialogResult res = colorDialog1.ShowDialog(); |
||
452 | if (res != DialogResult.Cancel) |
||
453 | { |
||
454 | textColourF.ForeColor = colorDialog1.Color; |
||
455 | editorView.SelectionColor = colorDialog1.Color; |
||
456 | } |
||
457 | } |
||
8 | freddie | 458 | |
459 | void ToolStripButton9Click(object sender, EventArgs e) |
||
460 | { |
||
461 | saveAll(); |
||
462 | } |
||
1 | freddie | 463 | } |
464 | } |