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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22