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