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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 782 - (show annotations) (download)
Fri Jun 21 04:22:03 2013 UTC (6 years, 7 months ago) by william
File size: 8510 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 BaseEventHandler<ControlClosingEventArgs<int>> DocumentClosing;
19
20 const string NewDocumentFilename = "ScratchPad";
21 public ScratchPadDocument() : this(NewDocumentFilename, new TabPage(), 0) { }
22 public ScratchPadDocument(TabPage tp, int index) : this(NewDocumentFilename, tp, index) { }
23 public ScratchPadDocument(int index) : this(NewDocumentFilename, new TabPage(), index) { }
24 public ScratchPadDocument(string documentFilename, TabPage tp, int index)
25 {
26 InitializeComponent();
27 this.DocumentFilename = documentFilename;
28 this.DocumentIndex = index;
29 this.DocumentTab = tp;
30 this.DocumentTab.Name = string.Format("tp{0}", this.DocumentIndex);
31 this.DocumentTab.Text = string.Format("{0}{1}", this.DocumentFilename, this.DocumentIndex);
32 //this.DocumentClosing += new BaseEventHandler<ControlClosingEventArgs<int>>(ScratchPadDocument_DocumentClosing);
33 this.IsDefaultDocument = true;
34 this.DocumentSaved = 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 public override Font Font
90 {
91 get
92 {
93 return base.Font;
94 }
95 set
96 {
97 base.Font = value;
98 txtEditor.Font = base.Font;
99 mnuItemClose.Font = base.Font;
100 mnuItemOpen.Font = base.Font;
101 mnuItemSave.Font = base.Font;
102 }
103 }
104
105 private void mnuItemOpen_Click(object sender, EventArgs e)
106 {
107 OpenDocument();
108 this.DocumentSaved = true; // the file was just opened
109 }
110
111 private void mnuItemSave_Click(object sender, EventArgs e)
112 {
113 SaveDocument();
114 }
115
116 private void mnuItemClose_Click(object sender, EventArgs e)
117 {
118 CloseDocument(false);
119 }
120
121
122 private void OpenDocument()
123 {
124 DialogResult result = FileLoader.ShowDialog();
125 if (result != DialogResult.OK) return;
126 FileInfo fi = new FileInfo(FileLoader.FileName);
127 this.DocumentFilename = fi.FullName;
128 try
129 {
130
131 using (FileStream fs = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read, FileShare.Read))
132 {
133 try
134 {
135 using (StreamReader sr = new StreamReader(fs))
136 {
137 try
138 {
139 var text = sr.ReadToEnd();
140 sr.Close();
141 txtEditor.Text = text;
142 this.UpdateTabName(fi.Name);
143 this.IsDefaultDocument = false;
144 this.DocumentSaved = true;
145 }
146 catch (Exception ex)
147 {
148 throw ex;
149 }
150 }
151 }
152 catch (Exception ex)
153 {
154 throw ex;
155 }
156 }
157 }
158 catch (Exception ex)
159 {
160 this.DocumentSaved = false;
161 MessageBox.Show(string.Format("Failed to open: '{0}'", fi.Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
162 logger.Error.WriteLine("Failed to open: '{0}'", fi.FullName);
163 logger.Error.WriteLine(ex.ToString());
164 }
165 }
166 private void SaveDocument()
167 {
168 FileInfo fi = null;
169 if (this.IsDefaultDocument)
170 {
171 DialogResult result = FileSaver.ShowDialog();
172 if (result != DialogResult.OK) return;
173 fi = new FileInfo(FileSaver.FileName);
174 }
175 else
176 {
177 fi = new FileInfo(this.DocumentFilename);
178 }
179 try
180 {
181 using (FileStream fs = new FileStream(fi.FullName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read))
182 {
183 try
184 {
185 using (StreamWriter sw = new StreamWriter(fs))
186 {
187 try
188 {
189 foreach (var t in txtEditor.Lines)
190 {
191 sw.WriteLine(t);
192 }
193 sw.Flush();
194 sw.Close();
195 this.DocumentSaved = true;
196 }
197 catch (Exception ex)
198 {
199 throw ex;
200 }
201 }
202 }
203 catch (Exception ex)
204 {
205 throw ex;
206 }
207 }
208 }
209 catch (Exception ex)
210 {
211 this.DocumentSaved = false;
212 MessageBox.Show(string.Format("Failed to save: '{0}'", fi.Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
213 logger.Error.WriteLine("Failed to save: '{0}'", fi.FullName);
214 logger.Error.WriteLine(ex.ToString());
215 }
216 }
217
218 private void CloseDocument(bool quiting)
219 {
220 if (ShouldAskSave())
221 {
222 SaveDocument();
223 }
224 var tb = GetParentTabControl();
225 tb.TabPages.RemoveAt(this.DocumentIndex);
226 if (this.DocumentClosing != null && !quiting)
227 {
228 this.DocumentClosing.Invoke(new ControlClosingEventArgs<int>(this, this.DocumentIndex));
229 }
230 }
231
232 private void txtEditor_TextChanged(object sender, EventArgs e)
233 {
234 this.DocumentSaved = false;
235 }
236 }
237 }

  ViewVC Help
Powered by ViewVC 1.1.22