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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 776 - (hide annotations) (download)
Thu Jun 20 23:07:32 2013 UTC (7 years, 10 months ago) by william
File size: 7714 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     this.DocumentTab.Controls.Add(this);
35 william 769 }
36 william 767
37 william 774 //void ScratchPadDocument_DocumentClosing(ControlClosingEventArgs<int> e)
38     //{
39     // CloseDocument(this.Disposing);
40     //}
41 william 769
42 william 774
43    
44    
45 william 769 public void OnDocumentQuit()
46 william 767 {
47 william 774 CloseDocument(true);
48     }
49    
50 william 775
51 william 767
52 william 774 private bool ShouldAskSave()
53     {
54 william 776 return !DocumentSaved;
55 william 774 }
56    
57 william 776
58     private bool _DocumentSaved;
59     public bool DocumentSaved { get { return _DocumentSaved; } private set { _DocumentSaved = value; } }
60    
61 william 767 private string _DocumentFilename;
62     public string DocumentFilename { get { return _DocumentFilename; } set { _DocumentFilename = value; } }
63 william 769 private TabPage _DocumentTab;
64     public TabPage DocumentTab { get { return _DocumentTab; } set { _DocumentTab = value; } }
65 william 767 private int _DocumentIndex;
66     public int DocumentIndex { get { return _DocumentIndex; } set { _DocumentIndex = value; } }
67 william 769
68 william 776 private void UpdateTabName(string name)
69     {
70     this.DocumentTab.Text = name;
71     }
72 william 769 private TabControl GetParentTabControl()
73     {
74     var parent = this.DocumentTab.Parent;
75     TabControl tb = (parent as TabControl);
76     if (tb == null)
77     {
78     throw new ArgumentNullException("parent", string.Format("The parent of tabpage '{0}' cannot be null", DocumentTab.Name));
79     }
80     else
81     {
82     return tb;
83     }
84     }
85 william 774
86     private void mnuItemOpen_Click(object sender, EventArgs e)
87     {
88 william 775 OpenDocument();
89 william 774 }
90    
91     private void mnuItemSave_Click(object sender, EventArgs e)
92     {
93 william 775 SaveDocument();
94 william 774 }
95    
96     private void mnuItemClose_Click(object sender, EventArgs e)
97     {
98     CloseDocument(false);
99     }
100 william 775
101    
102     private void OpenDocument()
103     {
104     DialogResult result = FileLoader.ShowDialog();
105     if (result != DialogResult.OK) return;
106     FileInfo fi = new FileInfo(FileLoader.FileName);
107     try
108     {
109    
110     using (FileStream fs = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read, FileShare.Read))
111     {
112     try
113     {
114     using (StreamReader sr = new StreamReader(fs))
115     {
116     try
117     {
118     var text = sr.ReadToEnd();
119     sr.Close();
120    
121     txtEditor.Document = new Fireball.Syntax.SyntaxDocument();
122     txtEditor.Document.Text = text;
123     SyntaxLanguage language = SyntaxLanguage.Text;
124     CodeEditorSyntaxLoader.SetSyntax(txtEditor, language);
125     txtEditor.Document.ReParse();
126 william 776 this.UpdateTabName(fi.Name);
127 william 775 }
128     catch (Exception ex)
129     {
130     throw ex;
131     }
132     }
133     }
134     catch (Exception ex)
135     {
136     throw ex;
137     }
138     }
139     }
140     catch (Exception ex)
141     {
142     MessageBox.Show(string.Format("Failed to open: '{0}'", fi.Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
143     logger.Error.WriteLine("Failed to open: '{0}'", fi.FullName);
144     logger.Error.WriteLine(ex.ToString());
145     }
146     }
147     private void SaveDocument()
148     {
149     DialogResult result = FileSaver.ShowDialog();
150     if (result != DialogResult.OK) return;
151 william 776 FileInfo fi = new FileInfo(FileSaver.FileName);
152     try
153     {
154     using (FileStream fs = new FileStream(fi.FullName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read))
155     {
156     try
157     {
158     using (StreamWriter sw = new StreamWriter(fs))
159     {
160     try
161     {
162     foreach (var t in txtEditor.Document.Lines)
163     {
164     sw.WriteLine(t);
165     }
166     sw.Flush();
167     sw.Close();
168     this.DocumentSaved = true;
169     }
170     catch (Exception ex)
171     {
172     throw ex;
173     }
174     }
175     }
176     catch (Exception ex)
177     {
178     throw ex;
179     }
180     }
181     }
182     catch (Exception ex)
183     {
184     this.DocumentSaved = false;
185     MessageBox.Show(string.Format("Failed to save: '{0}'", fi.Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
186     logger.Error.WriteLine("Failed to save: '{0}'", fi.FullName);
187     logger.Error.WriteLine(ex.ToString());
188     }
189 william 775 }
190    
191     private void CloseDocument(bool quiting)
192     {
193     if (ShouldAskSave())
194     {
195     SaveDocument();
196     }
197     var tb = GetParentTabControl();
198     tb.TabPages.RemoveAt(this.DocumentIndex);
199     if (this.DocumentClosing != null && !quiting)
200     {
201     this.DocumentClosing.Invoke(new ControlClosingEventArgs<int>(this, this.DocumentIndex));
202     }
203     }
204 william 776
205     private void txtEditor_TextChanged(object sender, EventArgs e)
206     {
207     this.DocumentSaved = false;
208     }
209 william 767 }
210     }

  ViewVC Help
Powered by ViewVC 1.1.22