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