Rev 4 | Rev 7 | 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(); |
||
74 | } |
||
75 | |||
76 | public void initAll() |
||
77 | { |
||
78 | initTreeView(); |
||
79 | } |
||
80 | |||
81 | public void initTreeView() |
||
82 | { |
||
83 | // welcome |
||
84 | treeView.Nodes.Add("Welcome", "Welcome"); |
||
85 | treeView.Nodes["Welcome"].Nodes.Add("Introduction", "Introduction"); |
||
86 | treeView.Nodes["Welcome"].Nodes.Add("Help", "Help"); |
||
87 | |||
88 | // |
||
89 | treeView.Nodes.Add("Files", "Files"); |
||
90 | } |
||
91 | |||
92 | void TreeViewNodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) |
||
93 | { |
||
94 | if (e.Node.Level == 0) |
||
95 | { |
||
96 | switch (e.Node.Name) |
||
97 | { |
||
98 | case ("Welcome"): |
||
99 | { |
||
100 | welcomeView.Url = new Uri("http://tim32.org/~freddie/LassyPad/Welcome.htm"); |
||
101 | break; |
||
102 | } |
||
103 | } |
||
104 | } |
||
105 | else |
||
106 | { |
||
107 | switch (e.Node.Parent.Name) |
||
108 | { |
||
109 | case ("Welcome"): |
||
110 | { |
||
111 | if (e.Node.Name == "Introduction") |
||
112 | welcomeView.Url = new Uri("http://tim32.org/~freddie/LassyPad/Introduction.htm"); |
||
113 | else if (e.Node.Name == "Help") |
||
114 | welcomeView.Url = new Uri("http://tim32.org/~freddie/LassyPad/Help.htm"); |
||
115 | |||
116 | break; |
||
117 | } |
||
118 | case ("Files"): |
||
119 | { |
||
3 | freddie | 120 | |
1 | freddie | 121 | showFile(e.Node.Name); |
122 | break; |
||
123 | } |
||
124 | } |
||
125 | } |
||
126 | } |
||
127 | |||
128 | // other functions |
||
129 | |||
130 | public void setSelection() |
||
131 | { |
||
132 | editorView.Select(selectionStart, selectionLength); |
||
133 | } |
||
134 | |||
135 | public void showFile(int fileID) |
||
136 | { |
||
137 | tabControl.SelectedIndex = 1; |
||
138 | editorView.Rtf = files[fileID].rtfBox.Rtf; |
||
139 | curFileID = fileID; |
||
140 | } |
||
141 | |||
142 | public void showFile(string filename) |
||
143 | { |
||
144 | tabControl.SelectedIndex = 1; |
||
145 | for (int i = 0; i < files.Count; i++) |
||
146 | { |
||
3 | freddie | 147 | if (files[i].loc == filename) |
148 | { |
||
149 | editorView.Rtf = files[i].rtfBox.Rtf; |
||
150 | curFileID = i; |
||
151 | return; |
||
152 | } |
||
1 | freddie | 153 | } |
154 | } |
||
155 | |||
156 | // file functions |
||
157 | |||
158 | public void newFile() |
||
159 | { |
||
160 | int i = 0; |
||
3 | freddie | 161 | bool exists; |
1 | freddie | 162 | while (true) |
163 | { |
||
3 | freddie | 164 | exists = false; |
1 | freddie | 165 | for (int j = 0; j < files.Count; j++) |
166 | { |
||
3 | freddie | 167 | if ((files[j].loc == "NewFile" + i.ToString() + ".rtf")) |
168 | exists = true; |
||
1 | freddie | 169 | } |
3 | freddie | 170 | if (!exists) |
171 | { |
||
172 | files.Add(new rtfFile("NewFile" + i.ToString() + ".rtf")); |
||
173 | treeView.Nodes["Files"].Nodes.Add("NewFile" + i.ToString() + ".rtf", "NewFile" + i.ToString() + ".rtf"); |
||
174 | showFile(files.Count - 1); |
||
175 | return; |
||
176 | } |
||
1 | freddie | 177 | i++; |
178 | } |
||
179 | |||
180 | } |
||
181 | |||
182 | public void renameFile(string filename, string newFilename) |
||
183 | { |
||
3 | freddie | 184 | treeView.Nodes["Files"].Nodes[filename].Text = newFilename; |
185 | treeView.Nodes["Files"].Nodes[filename].Name = newFilename; |
||
1 | freddie | 186 | for (int i = 0; i < files.Count; i++) |
187 | { |
||
188 | if (files[i].loc == filename) |
||
189 | files[i].loc = newFilename; |
||
190 | } |
||
191 | } |
||
192 | |||
3 | freddie | 193 | public void renameFile(int fileID, string newFilename) |
194 | { |
||
195 | treeView.Nodes["Files"].Nodes[files[fileID].loc].Text = newFilename; |
||
196 | treeView.Nodes["Files"].Nodes[files[fileID].loc].Name = newFilename; |
||
197 | files[fileID].loc = newFilename; |
||
198 | } |
||
199 | |||
1 | freddie | 200 | public void openFile() |
201 | { |
||
4 | freddie | 202 | bool openRes; |
1 | freddie | 203 | DialogResult res = openFileDialog1.ShowDialog(); |
204 | if (openFileDialog1.FileName != "" && res != DialogResult.Cancel) |
||
205 | { |
||
206 | files.Add(new rtfFile(openFileDialog1.FileName)); |
||
4 | freddie | 207 | openRes = files[files.Count - 1].open(); |
208 | if (openRes) |
||
209 | { |
||
210 | treeView.Nodes["Files"].Nodes.Add(openFileDialog1.FileName, openFileDialog1.FileName); |
||
211 | showFile(files.Count - 1); |
||
212 | } |
||
213 | else |
||
214 | files.RemoveAt(files.Count - 1); |
||
1 | freddie | 215 | } |
216 | } |
||
217 | |||
218 | public void closeFile() |
||
219 | { |
||
220 | if (curFileID < files.Count && curFileID > -1) |
||
221 | { |
||
222 | treeView.Nodes["Files"].Nodes.RemoveByKey(files[curFileID].loc); |
||
223 | files.RemoveAt(curFileID); |
||
224 | if (curFileID >= files.Count) |
||
225 | curFileID--; |
||
226 | if (files.Count > 0) |
||
227 | showFile(curFileID); |
||
228 | else |
||
229 | tabControl.SelectedIndex = 0; |
||
230 | } |
||
231 | } |
||
232 | |||
233 | // format functions |
||
234 | |||
235 | public void addStyle(FontStyle newStyles) |
||
236 | { |
||
237 | FontStyle fStyle = editorView.SelectionFont.Style; |
||
238 | editorView.SelectionFont = new Font(editorView.SelectionFont, fStyle | newStyles); |
||
239 | } |
||
240 | |||
241 | public void remStyle(FontStyle remStyles) |
||
242 | { |
||
243 | FontStyle fStyle = editorView.SelectionFont.Style; |
||
244 | fStyle -= remStyles; |
||
245 | editorView.SelectionFont = new Font(editorView.SelectionFont, fStyle); |
||
246 | } |
||
247 | |||
248 | // form events |
||
249 | |||
250 | void EditorViewTextChanged(object sender, EventArgs e) |
||
251 | { |
||
252 | if (curFileID < files.Count && curFileID > -1) |
||
253 | { |
||
254 | files[curFileID].rtfBox.Rtf = editorView.Rtf; |
||
255 | files[curFileID].saved = false; |
||
256 | } |
||
257 | } |
||
258 | |||
259 | void EditorViewSelectionChanged(object sender, EventArgs e) |
||
260 | { |
||
261 | selectionStart = editorView.SelectionStart; |
||
262 | selectionLength = editorView.SelectionLength; |
||
2 | tom | 263 | // sizeF.Text = editorView.SelectionFont.Size.ToString(); |
1 | freddie | 264 | } |
265 | |||
266 | #region File stuff |
||
267 | |||
268 | void ToolStripButton1Click(object sender, EventArgs e) |
||
269 | { |
||
270 | newFile(); |
||
271 | } |
||
272 | |||
273 | void NewToolStripMenuItemClick(object sender, EventArgs e) |
||
274 | { |
||
275 | newFile(); |
||
276 | } |
||
277 | |||
278 | void ToolStripButton2Click(object sender, EventArgs e) |
||
279 | { |
||
280 | openFile(); |
||
281 | } |
||
282 | |||
283 | void OpenToolStripMenuItemClick(object sender, EventArgs e) |
||
284 | { |
||
285 | openFile(); |
||
286 | } |
||
287 | |||
288 | void ToolStripButton3Click(object sender, EventArgs e) |
||
289 | { |
||
290 | files[curFileID].save(); |
||
291 | } |
||
292 | |||
293 | void SaveToolStripMenuItemClick(object sender, EventArgs e) |
||
294 | { |
||
295 | files[curFileID].save(); |
||
296 | } |
||
297 | |||
298 | void ToolStripButton4Click(object sender, EventArgs e) |
||
299 | { |
||
300 | closeFile(); |
||
301 | } |
||
302 | |||
3 | freddie | 303 | void SaveAsToolStripMenuItemClick(object sender, EventArgs e) |
304 | { |
||
305 | DialogResult res = saveFileDialog1.ShowDialog(); |
||
306 | if (saveFileDialog1.FileName != "" && res != DialogResult.Cancel) |
||
307 | { |
||
308 | renameFile(curFileID, saveFileDialog1.FileName); |
||
309 | files[curFileID].loc = saveFileDialog1.FileName; |
||
310 | files[curFileID].save(); |
||
311 | } |
||
312 | } |
||
313 | |||
1 | freddie | 314 | #endregion |
315 | |||
316 | #region Format stuff |
||
317 | |||
318 | void BoldFClick(object sender, EventArgs e) |
||
319 | { |
||
320 | if (editorView.SelectionFont.Bold) |
||
321 | remStyle(FontStyle.Bold); |
||
322 | else |
||
323 | addStyle(FontStyle.Bold); |
||
324 | } |
||
325 | |||
326 | void ItalicFClick(object sender, EventArgs e) |
||
327 | { |
||
328 | if (editorView.SelectionFont.Italic) |
||
329 | remStyle(FontStyle.Italic); |
||
330 | else |
||
331 | addStyle(FontStyle.Italic); |
||
332 | } |
||
333 | |||
334 | void UnderlineFClick(object sender, EventArgs e) |
||
335 | { |
||
336 | if (editorView.SelectionFont.Underline) |
||
337 | remStyle(FontStyle.Underline); |
||
338 | else |
||
339 | addStyle(FontStyle.Underline); |
||
340 | } |
||
341 | |||
342 | void ToolStripButton5Click(object sender, EventArgs e) |
||
343 | { |
||
344 | editorView.SelectionAlignment = HorizontalAlignment.Left; |
||
345 | } |
||
346 | |||
347 | void ToolStripButton6Click(object sender, EventArgs e) |
||
348 | { |
||
349 | editorView.SelectionAlignment = HorizontalAlignment.Center; |
||
350 | } |
||
351 | |||
352 | void ToolStripButton7Click(object sender, EventArgs e) |
||
353 | { |
||
354 | editorView.SelectionAlignment = HorizontalAlignment.Right; |
||
355 | } |
||
356 | |||
357 | void SizeFTextChanged(object sender, EventArgs e) |
||
358 | { |
||
359 | setSelection(); |
||
360 | FontStyle fStyle = editorView.SelectionFont.Style; |
||
361 | float size = 0.0f; |
||
362 | if (float.TryParse(sizeF.Text, out size)) |
||
363 | { |
||
364 | if (size > 0) |
||
365 | editorView.SelectionFont = new Font(editorView.SelectionFont.FontFamily, size, fStyle); |
||
366 | } |
||
367 | } |
||
368 | |||
369 | #endregion |
||
370 | |||
371 | void WordWrapToolStripMenuItemClick(object sender, EventArgs e) |
||
372 | { |
||
373 | editorView.WordWrap = ! editorView.WordWrap; |
||
374 | wordWrapToolStripMenuItem.Checked = editorView.WordWrap; |
||
375 | } |
||
6 | freddie | 376 | |
377 | void ToolStripButton8Click(object sender, EventArgs e) |
||
378 | { |
||
379 | |||
380 | } |
||
1 | freddie | 381 | } |
382 | } |