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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 785 - (show annotations) (download)
Fri Jun 21 04:50:33 2013 UTC (7 years, 1 month ago) by william
File size: 9533 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
14 namespace RomCheater.ScratchPad
15 {
16 public partial class ScratchPadDocument : UserControl
17 {
18 public event EventHandler<LinkClickedEventArgs> LinkClicked;
19 public event BaseEventHandler<ControlClosingEventArgs<int>> DocumentClosing;
20
21 const string NewDocumentFilename = "ScratchPad";
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 //public override Font Font
91 //{
92 // get
93 // {
94 // return base.Font;
95 // }
96 // set
97 // {
98 // base.Font = new Font(value.FontFamily, value.SizeInPoints);
99 // txtEditor.Font = base.Font;
100 // mnuItemClose.Font = base.Font;
101 // mnuItemOpen.Font = base.Font;
102 // mnuItemSave.Font = base.Font;
103 // }
104 //}
105
106 private void mnuItemOpen_Click(object sender, EventArgs e)
107 {
108 OpenDocument();
109 this.DocumentSaved = true; // the file was just opened
110 }
111
112 private void mnuItemSave_Click(object sender, EventArgs e)
113 {
114 SaveDocument();
115 }
116
117 private void mnuItemClose_Click(object sender, EventArgs e)
118 {
119 CloseDocument(false);
120 }
121
122
123 private void OpenDocument()
124 {
125 DialogResult result = FileLoader.ShowDialog();
126 if (result != DialogResult.OK) return;
127 FileInfo fi = new FileInfo(FileLoader.FileName);
128 this.DocumentFilename = fi.FullName;
129 try
130 {
131
132 using (FileStream fs = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read, FileShare.Read))
133 {
134 try
135 {
136 using (StreamReader sr = new StreamReader(fs))
137 {
138 try
139 {
140 var text = sr.ReadToEnd();
141 sr.Close();
142 txtEditor.Text = text;
143 this.UpdateTabName(fi.Name);
144 this.IsDefaultDocument = false;
145 this.DocumentSaved = true;
146 }
147 catch (Exception ex)
148 {
149 throw ex;
150 }
151 }
152 }
153 catch (Exception ex)
154 {
155 throw ex;
156 }
157 }
158 }
159 catch (Exception ex)
160 {
161 this.DocumentSaved = false;
162 MessageBox.Show(string.Format("Failed to open: '{0}'", fi.Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
163 logger.Error.WriteLine("Failed to open: '{0}'", fi.FullName);
164 logger.Error.WriteLine(ex.ToString());
165 }
166 }
167 private void SaveDocument()
168 {
169 FileInfo fi = null;
170 if (this.IsDefaultDocument)
171 {
172 DialogResult result = FileSaver.ShowDialog();
173 if (result != DialogResult.OK) return;
174 fi = new FileInfo(FileSaver.FileName);
175 }
176 else
177 {
178 fi = new FileInfo(this.DocumentFilename);
179 }
180 try
181 {
182 using (FileStream fs = new FileStream(fi.FullName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read))
183 {
184 try
185 {
186 using (StreamWriter sw = new StreamWriter(fs))
187 {
188 try
189 {
190 //foreach (var t in txtEditor.Lines)
191 //{
192 // sw.WriteLine(t);
193 //}
194 string line =string.Empty;
195 using (StringReader sr = new StringReader(txtEditor.Text))
196 {
197 while ((line = sr.ReadLine()) != null)
198 {
199 sw.WriteLine(line);
200 }
201 }
202
203 sw.Flush();
204 sw.Close();
205 this.DocumentSaved = true;
206 }
207 catch (Exception ex)
208 {
209 throw ex;
210 }
211 }
212 }
213 catch (Exception ex)
214 {
215 throw ex;
216 }
217 }
218 }
219 catch (Exception ex)
220 {
221 this.DocumentSaved = false;
222 MessageBox.Show(string.Format("Failed to save: '{0}'", fi.Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
223 logger.Error.WriteLine("Failed to save: '{0}'", fi.FullName);
224 logger.Error.WriteLine(ex.ToString());
225 }
226 }
227
228 private void CloseDocument(bool quiting)
229 {
230 if (ShouldAskSave())
231 {
232 SaveDocument();
233 }
234 var tb = GetParentTabControl();
235 tb.TabPages.RemoveAt(this.DocumentIndex);
236 if (this.DocumentClosing != null && !quiting)
237 {
238 this.DocumentClosing.Invoke(new ControlClosingEventArgs<int>(this, this.DocumentIndex));
239 }
240 }
241
242 private void txtEditor_TextChanged(object sender, EventArgs e)
243 {
244 this.DocumentSaved = false;
245 }
246
247 private void txtEditor_LinkClicked(object sender, LinkClickedEventArgs e)
248 {
249 if (this.LinkClicked != null)
250 {
251 this.LinkClicked.Invoke(sender, e);
252 }
253 }
254
255 private void ScratchPadDocument_Load(object sender, EventArgs e)
256 {
257 this.Font = new System.Drawing.Font(this.Font.FontFamily, this.Font.SizeInPoints);
258 }
259 }
260 }

  ViewVC Help
Powered by ViewVC 1.1.22