/[RomCheater]/trunk/RomCheater.ScratchPad/ScratchPadDocument.cs
ViewVC logotype

Contents of /trunk/RomCheater.ScratchPad/ScratchPadDocument.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 778 - (show annotations) (download)
Thu Jun 20 23:21:37 2013 UTC (8 years, 3 months ago) by william
File size: 8475 byte(s)

1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using WeifenLuo.WinFormsUI.Docking;
10 using RomCheater.Core;
11 using System.IO;
12 using RomCheater.Logging;
13 using Fireball.CodeEditor.SyntaxFiles;
14
15 namespace RomCheater.ScratchPad
16 {
17 public partial class ScratchPadDocument : UserControl
18 {
19 public event BaseEventHandler<ControlClosingEventArgs<int>> DocumentClosing;
20
21 const string NewDocumentFilename = "New";
22 public ScratchPadDocument() : this(NewDocumentFilename, new TabPage(), 0) { }
23 public ScratchPadDocument(TabPage tp, int index) : this(NewDocumentFilename, tp, index) { }
24 public ScratchPadDocument(int index) : this(NewDocumentFilename, new TabPage(), index) { }
25 public ScratchPadDocument(string documentFilename, TabPage tp, int index)
26 {
27 InitializeComponent();
28 this.DocumentFilename = documentFilename;
29 this.DocumentIndex = index;
30 this.DocumentTab = tp;
31 this.DocumentTab.Name = string.Format("tp{0}", this.DocumentIndex);
32 this.DocumentTab.Text = string.Format("{0}{1}", this.DocumentFilename, this.DocumentIndex);
33 //this.DocumentClosing += new BaseEventHandler<ControlClosingEventArgs<int>>(ScratchPadDocument_DocumentClosing);
34 this.IsDefaultDocument = true;
35 this.DocumentSaved = true;
36 this.DocumentTab.Controls.Add(this);
37 }
38
39 //void ScratchPadDocument_DocumentClosing(ControlClosingEventArgs<int> e)
40 //{
41 // CloseDocument(this.Disposing);
42 //}
43
44
45
46
47 public void OnDocumentQuit()
48 {
49 CloseDocument(true);
50 }
51
52
53
54 private bool ShouldAskSave()
55 {
56 return !DocumentSaved;
57 }
58
59 private bool _IsDefaultDocument;
60 public bool IsDefaultDocument { get { return _IsDefaultDocument; } private set { _IsDefaultDocument = value; } }
61
62 private bool _DocumentSaved;
63 public bool DocumentSaved { get { return _DocumentSaved; } private set { _DocumentSaved = value; } }
64
65 private string _DocumentFilename;
66 public string DocumentFilename { get { return _DocumentFilename; } set { _DocumentFilename = value; } }
67 private TabPage _DocumentTab;
68 public TabPage DocumentTab { get { return _DocumentTab; } set { _DocumentTab = value; } }
69 private int _DocumentIndex;
70 public int DocumentIndex { get { return _DocumentIndex; } set { _DocumentIndex = value; } }
71
72 private void UpdateTabName(string name)
73 {
74 this.DocumentTab.Text = name;
75 }
76 private TabControl GetParentTabControl()
77 {
78 var parent = this.DocumentTab.Parent;
79 TabControl tb = (parent as TabControl);
80 if (tb == null)
81 {
82 throw new ArgumentNullException("parent", string.Format("The parent of tabpage '{0}' cannot be null", DocumentTab.Name));
83 }
84 else
85 {
86 return tb;
87 }
88 }
89
90 private void mnuItemOpen_Click(object sender, EventArgs e)
91 {
92 OpenDocument();
93 this.DocumentSaved = true; // the file was just opened
94 }
95
96 private void mnuItemSave_Click(object sender, EventArgs e)
97 {
98 SaveDocument();
99 }
100
101 private void mnuItemClose_Click(object sender, EventArgs e)
102 {
103 CloseDocument(false);
104 }
105
106
107 private void OpenDocument()
108 {
109 DialogResult result = FileLoader.ShowDialog();
110 if (result != DialogResult.OK) return;
111 FileInfo fi = new FileInfo(FileLoader.FileName);
112 this.DocumentFilename = fi.FullName;
113 try
114 {
115
116 using (FileStream fs = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read, FileShare.Read))
117 {
118 try
119 {
120 using (StreamReader sr = new StreamReader(fs))
121 {
122 try
123 {
124 var text = sr.ReadToEnd();
125 sr.Close();
126
127 txtEditor.Document = new Fireball.Syntax.SyntaxDocument();
128 txtEditor.Document.Text = text;
129 SyntaxLanguage language = SyntaxLanguage.Text;
130 CodeEditorSyntaxLoader.SetSyntax(txtEditor, language);
131 txtEditor.Document.ReParse();
132 this.UpdateTabName(fi.Name);
133 this.IsDefaultDocument = false;
134 this.DocumentSaved = true;
135 }
136 catch (Exception ex)
137 {
138 throw ex;
139 }
140 }
141 }
142 catch (Exception ex)
143 {
144 throw ex;
145 }
146 }
147 }
148 catch (Exception ex)
149 {
150 this.DocumentSaved = false;
151 MessageBox.Show(string.Format("Failed to open: '{0}'", fi.Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
152 logger.Error.WriteLine("Failed to open: '{0}'", fi.FullName);
153 logger.Error.WriteLine(ex.ToString());
154 }
155 }
156 private void SaveDocument()
157 {
158 FileInfo fi = null;
159 if (this.IsDefaultDocument)
160 {
161 DialogResult result = FileSaver.ShowDialog();
162 if (result != DialogResult.OK) return;
163 fi = new FileInfo(FileSaver.FileName);
164 }
165 else
166 {
167 fi = new FileInfo(this.DocumentFilename);
168 }
169 try
170 {
171 using (FileStream fs = new FileStream(fi.FullName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read))
172 {
173 try
174 {
175 using (StreamWriter sw = new StreamWriter(fs))
176 {
177 try
178 {
179 foreach (var t in txtEditor.Document.Lines)
180 {
181 sw.WriteLine(t);
182 }
183 sw.Flush();
184 sw.Close();
185 this.DocumentSaved = true;
186 }
187 catch (Exception ex)
188 {
189 throw ex;
190 }
191 }
192 }
193 catch (Exception ex)
194 {
195 throw ex;
196 }
197 }
198 }
199 catch (Exception ex)
200 {
201 this.DocumentSaved = false;
202 MessageBox.Show(string.Format("Failed to save: '{0}'", fi.Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
203 logger.Error.WriteLine("Failed to save: '{0}'", fi.FullName);
204 logger.Error.WriteLine(ex.ToString());
205 }
206 }
207
208 private void CloseDocument(bool quiting)
209 {
210 if (ShouldAskSave())
211 {
212 SaveDocument();
213 }
214 var tb = GetParentTabControl();
215 tb.TabPages.RemoveAt(this.DocumentIndex);
216 if (this.DocumentClosing != null && !quiting)
217 {
218 this.DocumentClosing.Invoke(new ControlClosingEventArgs<int>(this, this.DocumentIndex));
219 }
220 }
221
222 private void txtEditor_TextChanged(object sender, EventArgs e)
223 {
224 this.DocumentSaved = false;
225 }
226 }
227 }

  ViewVC Help
Powered by ViewVC 1.1.22