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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 779 - (hide annotations) (download)
Thu Jun 20 23:22:24 2013 UTC (7 years, 10 months ago) by william
File size: 8482 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     using Fireball.CodeEditor.SyntaxFiles;
14 william 767
15     namespace RomCheater.ScratchPad
16     {
17 william 769 public partial class ScratchPadDocument : UserControl
18 william 767 {
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     private void mnuItemOpen_Click(object sender, EventArgs e)
91     {
92 william 775 OpenDocument();
93 william 778 this.DocumentSaved = true; // the file was just opened
94 william 774 }
95    
96     private void mnuItemSave_Click(object sender, EventArgs e)
97     {
98 william 775 SaveDocument();
99 william 774 }
100    
101     private void mnuItemClose_Click(object sender, EventArgs e)
102     {
103     CloseDocument(false);
104     }
105 william 775
106    
107     private void OpenDocument()
108     {
109     DialogResult result = FileLoader.ShowDialog();
110     if (result != DialogResult.OK) return;
111     FileInfo fi = new FileInfo(FileLoader.FileName);
112 william 777 this.DocumentFilename = fi.FullName;
113 william 775 try
114     {
115    
116     using (FileStream fs = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read, FileShare.Read))
117     {
118     try
119     {
120     using (StreamReader sr = new StreamReader(fs))
121     {
122     try
123     {
124     var text = sr.ReadToEnd();
125     sr.Close();
126    
127     txtEditor.Document = new Fireball.Syntax.SyntaxDocument();
128     txtEditor.Document.Text = text;
129     SyntaxLanguage language = SyntaxLanguage.Text;
130     CodeEditorSyntaxLoader.SetSyntax(txtEditor, language);
131     txtEditor.Document.ReParse();
132 william 776 this.UpdateTabName(fi.Name);
133 william 777 this.IsDefaultDocument = false;
134 william 778 this.DocumentSaved = true;
135 william 775 }
136     catch (Exception ex)
137     {
138     throw ex;
139     }
140     }
141     }
142     catch (Exception ex)
143     {
144     throw ex;
145     }
146     }
147     }
148     catch (Exception ex)
149     {
150 william 778 this.DocumentSaved = false;
151 william 775 MessageBox.Show(string.Format("Failed to open: '{0}'", fi.Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
152     logger.Error.WriteLine("Failed to open: '{0}'", fi.FullName);
153     logger.Error.WriteLine(ex.ToString());
154     }
155     }
156     private void SaveDocument()
157     {
158 william 777 FileInfo fi = null;
159     if (this.IsDefaultDocument)
160     {
161     DialogResult result = FileSaver.ShowDialog();
162     if (result != DialogResult.OK) return;
163     fi = new FileInfo(FileSaver.FileName);
164     }
165     else
166     {
167     fi = new FileInfo(this.DocumentFilename);
168     }
169 william 776 try
170     {
171     using (FileStream fs = new FileStream(fi.FullName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read))
172     {
173     try
174     {
175     using (StreamWriter sw = new StreamWriter(fs))
176     {
177     try
178     {
179     foreach (var t in txtEditor.Document.Lines)
180     {
181     sw.WriteLine(t);
182     }
183     sw.Flush();
184     sw.Close();
185     this.DocumentSaved = true;
186     }
187     catch (Exception ex)
188     {
189     throw ex;
190     }
191     }
192     }
193     catch (Exception ex)
194     {
195     throw ex;
196     }
197     }
198     }
199     catch (Exception ex)
200     {
201     this.DocumentSaved = false;
202     MessageBox.Show(string.Format("Failed to save: '{0}'", fi.Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
203     logger.Error.WriteLine("Failed to save: '{0}'", fi.FullName);
204     logger.Error.WriteLine(ex.ToString());
205     }
206 william 775 }
207    
208     private void CloseDocument(bool quiting)
209     {
210     if (ShouldAskSave())
211     {
212     SaveDocument();
213     }
214     var tb = GetParentTabControl();
215     tb.TabPages.RemoveAt(this.DocumentIndex);
216     if (this.DocumentClosing != null && !quiting)
217     {
218     this.DocumentClosing.Invoke(new ControlClosingEventArgs<int>(this, this.DocumentIndex));
219     }
220     }
221 william 776
222     private void txtEditor_TextChanged(object sender, EventArgs e)
223     {
224     this.DocumentSaved = false;
225     }
226 william 767 }
227     }

  ViewVC Help
Powered by ViewVC 1.1.22