ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/RomCheater/trunk/RomCheater.ScratchPad/ScratchPadDocument.cs
Revision: 812
Committed: Tue Apr 15 14:52:10 2014 UTC (9 years, 5 months ago) by william
File size: 9568 byte(s)
Log Message:
+ update logging to use Enterpise.Logging -- more work is still needed

File Contents

# Content
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 Enterprise.Logging;
14
15 namespace RomCheater.ScratchPad
16 {
17 public partial class ScratchPadDocument : UserControl
18 {
19 public event EventHandler<LinkClickedEventArgs> LinkClicked;
20 public event BaseEventHandler<ControlClosingEventArgs<int>> DocumentClosing;
21
22 const string NewDocumentFilename = "ScratchPad";
23 public ScratchPadDocument() : this(NewDocumentFilename, new TabPage(), 0) { }
24 public ScratchPadDocument(TabPage tp, int index) : this(NewDocumentFilename, tp, index) { }
25 public ScratchPadDocument(int index) : this(NewDocumentFilename, new TabPage(), index) { }
26 public ScratchPadDocument(string documentFilename, TabPage tp, int index)
27 {
28 InitializeComponent();
29 this.DocumentFilename = documentFilename;
30 this.DocumentIndex = index;
31 this.DocumentTab = tp;
32 this.DocumentTab.Name = string.Format("tp{0}", this.DocumentIndex);
33 this.DocumentTab.Text = string.Format("{0}{1}", this.DocumentFilename, this.DocumentIndex);
34 //this.DocumentClosing += new BaseEventHandler<ControlClosingEventArgs<int>>(ScratchPadDocument_DocumentClosing);
35 this.IsDefaultDocument = true;
36 this.DocumentSaved = true;
37 this.DocumentTab.Controls.Add(this);
38 }
39
40 //void ScratchPadDocument_DocumentClosing(ControlClosingEventArgs<int> e)
41 //{
42 // CloseDocument(this.Disposing);
43 //}
44
45
46
47
48 public void OnDocumentQuit()
49 {
50 CloseDocument(true);
51 }
52
53
54
55 private bool ShouldAskSave()
56 {
57 return !DocumentSaved;
58 }
59
60 private bool _IsDefaultDocument;
61 public bool IsDefaultDocument { get { return _IsDefaultDocument; } private set { _IsDefaultDocument = value; } }
62
63 private bool _DocumentSaved;
64 public bool DocumentSaved { get { return _DocumentSaved; } private set { _DocumentSaved = value; } }
65
66 private string _DocumentFilename;
67 public string DocumentFilename { get { return _DocumentFilename; } set { _DocumentFilename = value; } }
68 private TabPage _DocumentTab;
69 public TabPage DocumentTab { get { return _DocumentTab; } set { _DocumentTab = value; } }
70 private int _DocumentIndex;
71 public int DocumentIndex { get { return _DocumentIndex; } set { _DocumentIndex = value; } }
72
73 private void UpdateTabName(string name)
74 {
75 this.DocumentTab.Text = name;
76 }
77 private TabControl GetParentTabControl()
78 {
79 var parent = this.DocumentTab.Parent;
80 TabControl tb = (parent as TabControl);
81 if (tb == null)
82 {
83 throw new ArgumentNullException("parent", string.Format("The parent of tabpage '{0}' cannot be null", DocumentTab.Name));
84 }
85 else
86 {
87 return tb;
88 }
89 }
90
91 //public override Font Font
92 //{
93 // get
94 // {
95 // return base.Font;
96 // }
97 // set
98 // {
99 // base.Font = new Font(value.FontFamily, value.SizeInPoints);
100 // txtEditor.Font = base.Font;
101 // mnuItemClose.Font = base.Font;
102 // mnuItemOpen.Font = base.Font;
103 // mnuItemSave.Font = base.Font;
104 // }
105 //}
106
107 private void mnuItemOpen_Click(object sender, EventArgs e)
108 {
109 OpenDocument();
110 this.DocumentSaved = true; // the file was just opened
111 }
112
113 private void mnuItemSave_Click(object sender, EventArgs e)
114 {
115 SaveDocument();
116 }
117
118 private void mnuItemClose_Click(object sender, EventArgs e)
119 {
120 CloseDocument(false);
121 }
122
123
124 private void OpenDocument()
125 {
126 DialogResult result = FileLoader.ShowDialog();
127 if (result != DialogResult.OK) return;
128 FileInfo fi = new FileInfo(FileLoader.FileName);
129 this.DocumentFilename = fi.FullName;
130 try
131 {
132
133 using (FileStream fs = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read, FileShare.Read))
134 {
135 try
136 {
137 using (StreamReader sr = new StreamReader(fs))
138 {
139 try
140 {
141 var text = sr.ReadToEnd();
142 sr.Close();
143 txtEditor.Text = text;
144 this.UpdateTabName(fi.Name);
145 this.IsDefaultDocument = false;
146 this.DocumentSaved = true;
147 }
148 catch (Exception ex)
149 {
150 throw ex;
151 }
152 }
153 }
154 catch (Exception ex)
155 {
156 throw ex;
157 }
158 }
159 }
160 catch (Exception ex)
161 {
162 this.DocumentSaved = false;
163 MessageBox.Show(string.Format("Failed to open: '{0}'", fi.Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
164 gLog.Error.WriteLine("Failed to open: '{0}'", fi.FullName);
165 gLog.Verbose.Error.WriteLine(ex.ToString());
166 }
167 }
168 private void SaveDocument()
169 {
170 FileInfo fi = null;
171 if (this.IsDefaultDocument)
172 {
173 DialogResult result = FileSaver.ShowDialog();
174 if (result != DialogResult.OK) return;
175 fi = new FileInfo(FileSaver.FileName);
176 }
177 else
178 {
179 fi = new FileInfo(this.DocumentFilename);
180 }
181 try
182 {
183 using (FileStream fs = new FileStream(fi.FullName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read))
184 {
185 try
186 {
187 using (StreamWriter sw = new StreamWriter(fs))
188 {
189 try
190 {
191 //foreach (var t in txtEditor.Lines)
192 //{
193 // sw.WriteLine(t);
194 //}
195 string line =string.Empty;
196 using (StringReader sr = new StringReader(txtEditor.Text))
197 {
198 while ((line = sr.ReadLine()) != null)
199 {
200 sw.WriteLine(line);
201 }
202 }
203
204 sw.Flush();
205 sw.Close();
206 this.DocumentSaved = true;
207 }
208 catch (Exception ex)
209 {
210 throw ex;
211 }
212 }
213 }
214 catch (Exception ex)
215 {
216 throw ex;
217 }
218 }
219 }
220 catch (Exception ex)
221 {
222 this.DocumentSaved = false;
223 MessageBox.Show(string.Format("Failed to save: '{0}'", fi.Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
224 gLog.Error.WriteLine("Failed to save: '{0}'", fi.FullName);
225 gLog.Verbose.Error.WriteLine(ex.ToString());
226 }
227 }
228
229 private void CloseDocument(bool quiting)
230 {
231 if (ShouldAskSave())
232 {
233 SaveDocument();
234 }
235 var tb = GetParentTabControl();
236 tb.TabPages.RemoveAt(this.DocumentIndex);
237 if (this.DocumentClosing != null && !quiting)
238 {
239 this.DocumentClosing.Invoke(new ControlClosingEventArgs<int>(this, this.DocumentIndex));
240 }
241 }
242
243 private void txtEditor_TextChanged(object sender, EventArgs e)
244 {
245 this.DocumentSaved = false;
246 }
247
248 private void txtEditor_LinkClicked(object sender, LinkClickedEventArgs e)
249 {
250 if (this.LinkClicked != null)
251 {
252 this.LinkClicked.Invoke(sender, e);
253 }
254 }
255
256 private void ScratchPadDocument_Load(object sender, EventArgs e)
257 {
258 this.Font = new System.Drawing.Font(this.Font.FontFamily, this.Font.SizeInPoints);
259 }
260 }
261 }