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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 775 - (show annotations) (download)
Thu Jun 20 22:59:40 2013 UTC (7 years, 10 months ago) by william
File size: 5729 byte(s)

1 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 using RomCheater.Core;
11 using System.IO;
12 using RomCheater.Logging;
13 using Fireball.CodeEditor.SyntaxFiles;
14
15 namespace RomCheater.ScratchPad
16 {
17 public partial class ScratchPadDocument : UserControl
18 {
19 public event BaseEventHandler<ControlClosingEventArgs<int>> DocumentClosing;
20
21 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 {
27 InitializeComponent();
28 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 //this.DocumentClosing += new BaseEventHandler<ControlClosingEventArgs<int>>(ScratchPadDocument_DocumentClosing);
34 this.DocumentTab.Controls.Add(this);
35 }
36
37 //void ScratchPadDocument_DocumentClosing(ControlClosingEventArgs<int> e)
38 //{
39 // CloseDocument(this.Disposing);
40 //}
41
42
43
44
45 public void OnDocumentQuit()
46 {
47 CloseDocument(true);
48 }
49
50
51
52 private bool ShouldAskSave()
53 {
54 bool shouldAskSave = false;
55 return shouldAskSave;
56 }
57
58
59 private string _DocumentFilename;
60 public string DocumentFilename { get { return _DocumentFilename; } set { _DocumentFilename = value; } }
61 private TabPage _DocumentTab;
62 public TabPage DocumentTab { get { return _DocumentTab; } set { _DocumentTab = value; } }
63 private int _DocumentIndex;
64 public int DocumentIndex { get { return _DocumentIndex; } set { _DocumentIndex = value; } }
65
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
80 private void mnuItemOpen_Click(object sender, EventArgs e)
81 {
82 OpenDocument();
83 }
84
85 private void mnuItemSave_Click(object sender, EventArgs e)
86 {
87 SaveDocument();
88 }
89
90 private void mnuItemClose_Click(object sender, EventArgs e)
91 {
92 CloseDocument(false);
93 }
94
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 }
160 }

  ViewVC Help
Powered by ViewVC 1.1.22