ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/RomCheater/trunk/RomCheater.ScratchPad/ScratchPadDocument.cs
Revision: 775
Committed: Thu Jun 20 22:59:40 2013 UTC (9 years, 9 months ago) by william
File size: 5729 byte(s)
Log Message:

File Contents

# User Rev Content
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     bool shouldAskSave = false;
55     return shouldAskSave;
56     }
57    
58 william 775
59 william 767 private string _DocumentFilename;
60     public string DocumentFilename { get { return _DocumentFilename; } set { _DocumentFilename = value; } }
61 william 769 private TabPage _DocumentTab;
62     public TabPage DocumentTab { get { return _DocumentTab; } set { _DocumentTab = value; } }
63 william 767 private int _DocumentIndex;
64     public int DocumentIndex { get { return _DocumentIndex; } set { _DocumentIndex = value; } }
65 william 769
66     private TabControl GetParentTabControl()
67     {
68     var parent = this.DocumentTab.Parent;
69     TabControl tb = (parent as TabControl);
70     if (tb == null)
71     {
72     throw new ArgumentNullException("parent", string.Format("The parent of tabpage '{0}' cannot be null", DocumentTab.Name));
73     }
74     else
75     {
76     return tb;
77     }
78     }
79 william 774
80     private void mnuItemOpen_Click(object sender, EventArgs e)
81     {
82 william 775 OpenDocument();
83 william 774 }
84    
85     private void mnuItemSave_Click(object sender, EventArgs e)
86     {
87 william 775 SaveDocument();
88 william 774 }
89    
90     private void mnuItemClose_Click(object sender, EventArgs e)
91     {
92     CloseDocument(false);
93     }
94 william 775
95    
96     private void OpenDocument()
97     {
98     DialogResult result = FileLoader.ShowDialog();
99     if (result != DialogResult.OK) return;
100     FileInfo fi = new FileInfo(FileLoader.FileName);
101     try
102     {
103    
104     using (FileStream fs = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read, FileShare.Read))
105     {
106     try
107     {
108     using (StreamReader sr = new StreamReader(fs))
109     {
110     try
111     {
112     var text = sr.ReadToEnd();
113     sr.Close();
114    
115     txtEditor.Document = new Fireball.Syntax.SyntaxDocument();
116     txtEditor.Document.Text = text;
117     SyntaxLanguage language = SyntaxLanguage.Text;
118     CodeEditorSyntaxLoader.SetSyntax(txtEditor, language);
119     txtEditor.Document.ReParse();
120     }
121     catch (Exception ex)
122     {
123     throw ex;
124     }
125     }
126     }
127     catch (Exception ex)
128     {
129     throw ex;
130     }
131     }
132     }
133     catch (Exception ex)
134     {
135     MessageBox.Show(string.Format("Failed to open: '{0}'", fi.Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
136     logger.Error.WriteLine("Failed to open: '{0}'", fi.FullName);
137     logger.Error.WriteLine(ex.ToString());
138     }
139     }
140     private void SaveDocument()
141     {
142     DialogResult result = FileSaver.ShowDialog();
143     if (result != DialogResult.OK) return;
144     }
145    
146     private void CloseDocument(bool quiting)
147     {
148     if (ShouldAskSave())
149     {
150     SaveDocument();
151     }
152     var tb = GetParentTabControl();
153     tb.TabPages.RemoveAt(this.DocumentIndex);
154     if (this.DocumentClosing != null && !quiting)
155     {
156     this.DocumentClosing.Invoke(new ControlClosingEventArgs<int>(this, this.DocumentIndex));
157     }
158     }
159 william 767 }
160     }