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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 780 - (hide annotations) (download)
Thu Jun 20 23:35:02 2013 UTC (7 years, 10 months ago) by william
File size: 9016 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 william 780 public override Font Font
91     {
92     get
93     {
94     return base.Font;
95     }
96     set
97     {
98     base.Font = value;
99     txtEditor.Font = base.Font;
100     mnuItemClose.Font = base.Font;
101     mnuItemOpen.Font = base.Font;
102     mnuItemSave.Font = base.Font;
103     }
104     }
105    
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    
143     txtEditor.Document = new Fireball.Syntax.SyntaxDocument();
144 william 780 //txtEditor.Font = new System.Drawing.Font(this.Font.FontFamily, this.Font.SizeInPoints);
145 william 775 txtEditor.Document.Text = text;
146     SyntaxLanguage language = SyntaxLanguage.Text;
147     CodeEditorSyntaxLoader.SetSyntax(txtEditor, language);
148     txtEditor.Document.ReParse();
149 william 776 this.UpdateTabName(fi.Name);
150 william 777 this.IsDefaultDocument = false;
151 william 778 this.DocumentSaved = true;
152 william 775 }
153     catch (Exception ex)
154     {
155     throw ex;
156     }
157     }
158     }
159     catch (Exception ex)
160     {
161     throw ex;
162     }
163     }
164     }
165     catch (Exception ex)
166     {
167 william 778 this.DocumentSaved = false;
168 william 775 MessageBox.Show(string.Format("Failed to open: '{0}'", fi.Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
169     logger.Error.WriteLine("Failed to open: '{0}'", fi.FullName);
170     logger.Error.WriteLine(ex.ToString());
171     }
172     }
173     private void SaveDocument()
174     {
175 william 777 FileInfo fi = null;
176     if (this.IsDefaultDocument)
177     {
178     DialogResult result = FileSaver.ShowDialog();
179     if (result != DialogResult.OK) return;
180     fi = new FileInfo(FileSaver.FileName);
181     }
182     else
183     {
184     fi = new FileInfo(this.DocumentFilename);
185     }
186 william 776 try
187     {
188     using (FileStream fs = new FileStream(fi.FullName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read))
189     {
190     try
191     {
192     using (StreamWriter sw = new StreamWriter(fs))
193     {
194     try
195     {
196     foreach (var t in txtEditor.Document.Lines)
197     {
198     sw.WriteLine(t);
199     }
200     sw.Flush();
201     sw.Close();
202     this.DocumentSaved = true;
203     }
204     catch (Exception ex)
205     {
206     throw ex;
207     }
208     }
209     }
210     catch (Exception ex)
211     {
212     throw ex;
213     }
214     }
215     }
216     catch (Exception ex)
217     {
218     this.DocumentSaved = false;
219     MessageBox.Show(string.Format("Failed to save: '{0}'", fi.Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
220     logger.Error.WriteLine("Failed to save: '{0}'", fi.FullName);
221     logger.Error.WriteLine(ex.ToString());
222     }
223 william 775 }
224    
225     private void CloseDocument(bool quiting)
226     {
227     if (ShouldAskSave())
228     {
229     SaveDocument();
230     }
231     var tb = GetParentTabControl();
232     tb.TabPages.RemoveAt(this.DocumentIndex);
233     if (this.DocumentClosing != null && !quiting)
234     {
235     this.DocumentClosing.Invoke(new ControlClosingEventArgs<int>(this, this.DocumentIndex));
236     }
237     }
238 william 776
239     private void txtEditor_TextChanged(object sender, EventArgs e)
240     {
241     this.DocumentSaved = false;
242     }
243 william 767 }
244     }

  ViewVC Help
Powered by ViewVC 1.1.22