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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 785 - (hide annotations) (download)
Fri Jun 21 04:50:33 2013 UTC (7 years ago) by william
File size: 9533 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 783 {
18     public event EventHandler<LinkClickedEventArgs> LinkClicked;
19 william 774 public event BaseEventHandler<ControlClosingEventArgs<int>> DocumentClosing;
20    
21 william 779 const string NewDocumentFilename = "ScratchPad";
22 william 769 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 william 767 {
27     InitializeComponent();
28 william 769 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 william 774 //this.DocumentClosing += new BaseEventHandler<ControlClosingEventArgs<int>>(ScratchPadDocument_DocumentClosing);
34 william 777 this.IsDefaultDocument = true;
35 william 778 this.DocumentSaved = true;
36 william 774 this.DocumentTab.Controls.Add(this);
37 william 769 }
38 william 767
39 william 774 //void ScratchPadDocument_DocumentClosing(ControlClosingEventArgs<int> e)
40     //{
41     // CloseDocument(this.Disposing);
42     //}
43 william 769
44 william 774
45    
46    
47 william 769 public void OnDocumentQuit()
48 william 767 {
49 william 774 CloseDocument(true);
50     }
51    
52 william 775
53 william 767
54 william 774 private bool ShouldAskSave()
55     {
56 william 776 return !DocumentSaved;
57 william 774 }
58    
59 william 777 private bool _IsDefaultDocument;
60     public bool IsDefaultDocument { get { return _IsDefaultDocument; } private set { _IsDefaultDocument = value; } }
61 william 776
62     private bool _DocumentSaved;
63     public bool DocumentSaved { get { return _DocumentSaved; } private set { _DocumentSaved = value; } }
64    
65 william 767 private string _DocumentFilename;
66     public string DocumentFilename { get { return _DocumentFilename; } set { _DocumentFilename = value; } }
67 william 769 private TabPage _DocumentTab;
68     public TabPage DocumentTab { get { return _DocumentTab; } set { _DocumentTab = value; } }
69 william 767 private int _DocumentIndex;
70     public int DocumentIndex { get { return _DocumentIndex; } set { _DocumentIndex = value; } }
71 william 769
72 william 776 private void UpdateTabName(string name)
73     {
74     this.DocumentTab.Text = name;
75     }
76 william 769 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 william 774
90 william 785 //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 william 780
106 william 774 private void mnuItemOpen_Click(object sender, EventArgs e)
107     {
108 william 775 OpenDocument();
109 william 778 this.DocumentSaved = true; // the file was just opened
110 william 774 }
111    
112     private void mnuItemSave_Click(object sender, EventArgs e)
113     {
114 william 775 SaveDocument();
115 william 774 }
116    
117     private void mnuItemClose_Click(object sender, EventArgs e)
118     {
119     CloseDocument(false);
120     }
121 william 775
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 william 777 this.DocumentFilename = fi.FullName;
129 william 775 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 william 782 txtEditor.Text = text;
143 william 776 this.UpdateTabName(fi.Name);
144 william 777 this.IsDefaultDocument = false;
145 william 778 this.DocumentSaved = true;
146 william 775 }
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 william 778 this.DocumentSaved = false;
162 william 775 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 william 777 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 william 776 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 william 785 //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 william 776 {
197 william 785 while ((line = sr.ReadLine()) != null)
198     {
199     sw.WriteLine(line);
200     }
201 william 776 }
202 william 785
203 william 776 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 william 775 }
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 william 776
242     private void txtEditor_TextChanged(object sender, EventArgs e)
243     {
244     this.DocumentSaved = false;
245     }
246 william 783
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 william 785
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 william 767 }
260     }

  ViewVC Help
Powered by ViewVC 1.1.22