Rev 6 | Rev 8 | 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 | |||
269 | // format functions |
||
270 | |||
271 | public void addStyle(FontStyle newStyles) |
||
272 | { |
||
273 | FontStyle fStyle = editorView.SelectionFont.Style; |
||
274 | editorView.SelectionFont = new Font(editorView.SelectionFont, fStyle | newStyles); |
||
275 | } |
||
276 | |||
277 | public void remStyle(FontStyle remStyles) |
||
278 | { |
||
279 | FontStyle fStyle = editorView.SelectionFont.Style; |
||
280 | fStyle -= remStyles; |
||
281 | editorView.SelectionFont = new Font(editorView.SelectionFont, fStyle); |
||
282 | } |
||
283 | |||
284 | // form events |
||
285 | |||
286 | void EditorViewTextChanged(object sender, EventArgs e) |
||
287 | { |
||
288 | if (curFileID < files.Count && curFileID > -1) |
||
289 | { |
||
290 | files[curFileID].rtfBox.Rtf = editorView.Rtf; |
||
291 | files[curFileID].saved = false; |
||
292 | } |
||
293 | } |
||
294 | |||
295 | void EditorViewSelectionChanged(object sender, EventArgs e) |
||
296 | { |
||
297 | selectionStart = editorView.SelectionStart; |
||
298 | selectionLength = editorView.SelectionLength; |
||
7 | freddie | 299 | textColourF.ForeColor = editorView.SelectionColor; |
2 | tom | 300 | // sizeF.Text = editorView.SelectionFont.Size.ToString(); |
1 | freddie | 301 | } |
302 | |||
303 | #region File stuff |
||
304 | |||
305 | void ToolStripButton1Click(object sender, EventArgs e) |
||
306 | { |
||
307 | newFile(); |
||
308 | } |
||
309 | |||
310 | void NewToolStripMenuItemClick(object sender, EventArgs e) |
||
311 | { |
||
312 | newFile(); |
||
313 | } |
||
314 | |||
315 | void ToolStripButton2Click(object sender, EventArgs e) |
||
316 | { |
||
317 | openFile(); |
||
318 | } |
||
319 | |||
320 | void OpenToolStripMenuItemClick(object sender, EventArgs e) |
||
321 | { |
||
322 | openFile(); |
||
323 | } |
||
324 | |||
325 | void ToolStripButton3Click(object sender, EventArgs e) |
||
326 | { |
||
327 | files[curFileID].save(); |
||
328 | } |
||
329 | |||
330 | void SaveToolStripMenuItemClick(object sender, EventArgs e) |
||
331 | { |
||
332 | files[curFileID].save(); |
||
333 | } |
||
334 | |||
335 | void ToolStripButton4Click(object sender, EventArgs e) |
||
336 | { |
||
337 | closeFile(); |
||
338 | } |
||
339 | |||
3 | freddie | 340 | void SaveAsToolStripMenuItemClick(object sender, EventArgs e) |
341 | { |
||
342 | DialogResult res = saveFileDialog1.ShowDialog(); |
||
343 | if (saveFileDialog1.FileName != "" && res != DialogResult.Cancel) |
||
344 | { |
||
345 | renameFile(curFileID, saveFileDialog1.FileName); |
||
346 | files[curFileID].loc = saveFileDialog1.FileName; |
||
347 | files[curFileID].save(); |
||
348 | } |
||
349 | } |
||
350 | |||
1 | freddie | 351 | #endregion |
352 | |||
353 | #region Format stuff |
||
354 | |||
355 | void BoldFClick(object sender, EventArgs e) |
||
356 | { |
||
357 | if (editorView.SelectionFont.Bold) |
||
358 | remStyle(FontStyle.Bold); |
||
359 | else |
||
360 | addStyle(FontStyle.Bold); |
||
361 | } |
||
362 | |||
363 | void ItalicFClick(object sender, EventArgs e) |
||
364 | { |
||
365 | if (editorView.SelectionFont.Italic) |
||
366 | remStyle(FontStyle.Italic); |
||
367 | else |
||
368 | addStyle(FontStyle.Italic); |
||
369 | } |
||
370 | |||
371 | void UnderlineFClick(object sender, EventArgs e) |
||
372 | { |
||
373 | if (editorView.SelectionFont.Underline) |
||
374 | remStyle(FontStyle.Underline); |
||
375 | else |
||
376 | addStyle(FontStyle.Underline); |
||
377 | } |
||
378 | |||
379 | void ToolStripButton5Click(object sender, EventArgs e) |
||
380 | { |
||
381 | editorView.SelectionAlignment = HorizontalAlignment.Left; |
||
382 | } |
||
383 | |||
384 | void ToolStripButton6Click(object sender, EventArgs e) |
||
385 | { |
||
386 | editorView.SelectionAlignment = HorizontalAlignment.Center; |
||
387 | } |
||
388 | |||
389 | void ToolStripButton7Click(object sender, EventArgs e) |
||
390 | { |
||
391 | editorView.SelectionAlignment = HorizontalAlignment.Right; |
||
392 | } |
||
393 | |||
394 | void SizeFTextChanged(object sender, EventArgs e) |
||
395 | { |
||
396 | setSelection(); |
||
397 | FontStyle fStyle = editorView.SelectionFont.Style; |
||
398 | float size = 0.0f; |
||
399 | if (float.TryParse(sizeF.Text, out size)) |
||
400 | { |
||
401 | if (size > 0) |
||
402 | editorView.SelectionFont = new Font(editorView.SelectionFont.FontFamily, size, fStyle); |
||
403 | } |
||
404 | } |
||
405 | |||
406 | #endregion |
||
407 | |||
408 | void WordWrapToolStripMenuItemClick(object sender, EventArgs e) |
||
409 | { |
||
410 | editorView.WordWrap = ! editorView.WordWrap; |
||
411 | wordWrapToolStripMenuItem.Checked = editorView.WordWrap; |
||
412 | } |
||
6 | freddie | 413 | |
414 | void ToolStripButton8Click(object sender, EventArgs e) |
||
415 | { |
||
7 | freddie | 416 | insertImageFromFile(); |
6 | freddie | 417 | } |
7 | freddie | 418 | |
419 | #region editorMenuStrip |
||
420 | |||
421 | void CloseFileToolStripMenuItemClick(object sender, EventArgs e) |
||
422 | { |
||
423 | closeFile(); |
||
424 | } |
||
425 | |||
426 | void SaveFileToolStripMenuItemClick(object sender, EventArgs e) |
||
427 | { |
||
428 | files[curFileID].save(); |
||
429 | } |
||
430 | |||
431 | void InToolStripMenuItemClick(object sender, EventArgs e) |
||
432 | { |
||
433 | zoomIn(); |
||
434 | } |
||
435 | |||
436 | void OutToolStripMenuItemClick(object sender, EventArgs e) |
||
437 | { |
||
438 | zoomOut(); |
||
439 | } |
||
440 | |||
441 | #endregion |
||
442 | |||
443 | void ToolStripButton9Click(object sender, EventArgs e) |
||
444 | { |
||
445 | DialogResult res = colorDialog1.ShowDialog(); |
||
446 | if (res != DialogResult.Cancel) |
||
447 | { |
||
448 | textColourF.ForeColor = colorDialog1.Color; |
||
449 | editorView.SelectionColor = colorDialog1.Color; |
||
450 | } |
||
451 | } |
||
1 | freddie | 452 | } |
453 | } |