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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 782 - (hide annotations) (download)
Fri Jun 21 04:22:03 2013 UTC (7 years, 10 months ago) by william
File size: 8510 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 774 using RomCheater.Core;
11 william 775 using System.IO;
12     using RomCheater.Logging;
13 william 767
14     namespace RomCheater.ScratchPad
15     {
16 william 769 public partial class ScratchPadDocument : UserControl
17 william 767 {
18 william 774 public event BaseEventHandler<ControlClosingEventArgs<int>> DocumentClosing;
19    
20 william 779 const string NewDocumentFilename = "ScratchPad";
21 william 769 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 william 767 {
26     InitializeComponent();
27 william 769 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 william 774 //this.DocumentClosing += new BaseEventHandler<ControlClosingEventArgs<int>>(ScratchPadDocument_DocumentClosing);
33 william 777 this.IsDefaultDocument = true;
34 william 778 this.DocumentSaved = true;
35 william 774 this.DocumentTab.Controls.Add(this);
36 william 769 }
37 william 767
38 william 774 //void ScratchPadDocument_DocumentClosing(ControlClosingEventArgs<int> e)
39     //{
40     // CloseDocument(this.Disposing);
41     //}
42 william 769
43 william 774
44    
45    
46 william 769 public void OnDocumentQuit()
47 william 767 {
48 william 774 CloseDocument(true);
49     }
50    
51 william 775
52 william 767
53 william 774 private bool ShouldAskSave()
54     {
55 william 776 return !DocumentSaved;
56 william 774 }
57    
58 william 777 private bool _IsDefaultDocument;
59     public bool IsDefaultDocument { get { return _IsDefaultDocument; } private set { _IsDefaultDocument = value; } }
60 william 776
61     private bool _DocumentSaved;
62     public bool DocumentSaved { get { return _DocumentSaved; } private set { _DocumentSaved = value; } }
63    
64 william 767 private string _DocumentFilename;
65     public string DocumentFilename { get { return _DocumentFilename; } set { _DocumentFilename = value; } }
66 william 769 private TabPage _DocumentTab;
67     public TabPage DocumentTab { get { return _DocumentTab; } set { _DocumentTab = value; } }
68 william 767 private int _DocumentIndex;
69     public int DocumentIndex { get { return _DocumentIndex; } set { _DocumentIndex = value; } }
70 william 769
71 william 776 private void UpdateTabName(string name)
72     {
73     this.DocumentTab.Text = name;
74     }
75 william 769 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 william 774
89 william 780 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 william 774 private void mnuItemOpen_Click(object sender, EventArgs e)
106     {
107 william 775 OpenDocument();
108 william 778 this.DocumentSaved = true; // the file was just opened
109 william 774 }
110    
111     private void mnuItemSave_Click(object sender, EventArgs e)
112     {
113 william 775 SaveDocument();
114 william 774 }
115    
116     private void mnuItemClose_Click(object sender, EventArgs e)
117     {
118     CloseDocument(false);
119     }
120 william 775
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 william 777 this.DocumentFilename = fi.FullName;
128 william 775 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 william 782 txtEditor.Text = text;
142 william 776 this.UpdateTabName(fi.Name);
143 william 777 this.IsDefaultDocument = false;
144 william 778 this.DocumentSaved = true;
145 william 775 }
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 william 778 this.DocumentSaved = false;
161 william 775 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 william 777 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 william 776 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 william 782 foreach (var t in txtEditor.Lines)
190 william 776 {
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 william 775 }
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 william 776
232     private void txtEditor_TextChanged(object sender, EventArgs e)
233     {
234     this.DocumentSaved = false;
235     }
236 william 767 }
237     }

  ViewVC Help
Powered by ViewVC 1.1.22