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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 889 - (hide annotations) (download)
Wed Sep 17 04:55:52 2014 UTC (6 years, 7 months ago) by william
File size: 9574 byte(s)


1 william 767 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 william 889 using RomCheater.Interfaces;
11 william 775 using System.IO;
12     using RomCheater.Logging;
13 william 812 using Enterprise.Logging;
14 william 767
15     namespace RomCheater.ScratchPad
16     {
17 william 769 public partial class ScratchPadDocument : UserControl
18 william 783 {
19     public event EventHandler<LinkClickedEventArgs> LinkClicked;
20 william 774 public event BaseEventHandler<ControlClosingEventArgs<int>> DocumentClosing;
21    
22 william 779 const string NewDocumentFilename = "ScratchPad";
23 william 769 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 william 767 {
28     InitializeComponent();
29 william 769 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 william 774 //this.DocumentClosing += new BaseEventHandler<ControlClosingEventArgs<int>>(ScratchPadDocument_DocumentClosing);
35 william 777 this.IsDefaultDocument = true;
36 william 778 this.DocumentSaved = true;
37 william 774 this.DocumentTab.Controls.Add(this);
38 william 769 }
39 william 767
40 william 774 //void ScratchPadDocument_DocumentClosing(ControlClosingEventArgs<int> e)
41     //{
42     // CloseDocument(this.Disposing);
43     //}
44 william 769
45 william 774
46    
47    
48 william 769 public void OnDocumentQuit()
49 william 767 {
50 william 774 CloseDocument(true);
51     }
52    
53 william 775
54 william 767
55 william 774 private bool ShouldAskSave()
56     {
57 william 776 return !DocumentSaved;
58 william 774 }
59    
60 william 777 private bool _IsDefaultDocument;
61     public bool IsDefaultDocument { get { return _IsDefaultDocument; } private set { _IsDefaultDocument = value; } }
62 william 776
63     private bool _DocumentSaved;
64     public bool DocumentSaved { get { return _DocumentSaved; } private set { _DocumentSaved = value; } }
65    
66 william 767 private string _DocumentFilename;
67     public string DocumentFilename { get { return _DocumentFilename; } set { _DocumentFilename = value; } }
68 william 769 private TabPage _DocumentTab;
69     public TabPage DocumentTab { get { return _DocumentTab; } set { _DocumentTab = value; } }
70 william 767 private int _DocumentIndex;
71     public int DocumentIndex { get { return _DocumentIndex; } set { _DocumentIndex = value; } }
72 william 769
73 william 776 private void UpdateTabName(string name)
74     {
75     this.DocumentTab.Text = name;
76     }
77 william 769 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 william 774
91 william 785 //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 william 780
107 william 774 private void mnuItemOpen_Click(object sender, EventArgs e)
108     {
109 william 775 OpenDocument();
110 william 778 this.DocumentSaved = true; // the file was just opened
111 william 774 }
112    
113     private void mnuItemSave_Click(object sender, EventArgs e)
114     {
115 william 775 SaveDocument();
116 william 774 }
117    
118     private void mnuItemClose_Click(object sender, EventArgs e)
119     {
120     CloseDocument(false);
121     }
122 william 775
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 william 777 this.DocumentFilename = fi.FullName;
130 william 775 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 william 782 txtEditor.Text = text;
144 william 776 this.UpdateTabName(fi.Name);
145 william 777 this.IsDefaultDocument = false;
146 william 778 this.DocumentSaved = true;
147 william 775 }
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 william 778 this.DocumentSaved = false;
163 william 775 MessageBox.Show(string.Format("Failed to open: '{0}'", fi.Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
164 william 812 gLog.Error.WriteLine("Failed to open: '{0}'", fi.FullName);
165     gLog.Verbose.Error.WriteLine(ex.ToString());
166 william 775 }
167     }
168     private void SaveDocument()
169     {
170 william 777 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 william 776 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 william 785 //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 william 776 {
198 william 785 while ((line = sr.ReadLine()) != null)
199     {
200     sw.WriteLine(line);
201     }
202 william 776 }
203 william 785
204 william 776 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 william 812 gLog.Error.WriteLine("Failed to save: '{0}'", fi.FullName);
225     gLog.Verbose.Error.WriteLine(ex.ToString());
226 william 776 }
227 william 775 }
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 william 776
243     private void txtEditor_TextChanged(object sender, EventArgs e)
244     {
245     this.DocumentSaved = false;
246     }
247 william 783
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 william 785
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 william 767 }
261     }

  ViewVC Help
Powered by ViewVC 1.1.22