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