Repository navigation
Expand file tree
/
Copy pathEditorFile.cs
More file actions
214 lines (170 loc) · 7.36 KB
/
Copy pathEditorFile.cs
File metadata and controls
214 lines (170 loc) · 7.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using FastColoredTextBoxNS;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
namespace Trax {
internal class EditorFile : ProjectFile {
#region Static part
#region Fields
internal static Main Main;
internal static new List<EditorFile> All = new List<EditorFile>();
#endregion
#region Events
public static event EventHandler BatchStart;
public static event EventHandler BatchEnd;
public static event EventHandler ReadingFile;
public static event EventHandler ReadingFileDone;
public static event EventHandler WritingFile;
public static event EventHandler WritingFileDone;
public static event EventHandler ProcessingFile;
public static event EventHandler ProcessingFileDone;
#endregion
#region Methods
public static void Reset() {
All.ForEach(i => {
i.Editor.Dispose();
i.Container.DocumentMap.Dispose();
i.Container.Splitter.Dispose();
i.Container.Dispose();
});
All.Clear();
var s = Properties.Settings.Default;
s.LastOpenFile = null;
s.Save();
}
/// <summary>
/// Tries to open a relative filename within the current project
/// </summary>
/// <param name="context"></param>
/// <param name="name"></param>
/// <returns></returns>
public static bool TryOpen(ProjectFile context, string name) {
string b = context.BaseDirectory, s = context.SceneryDirectory, n = name, l = "\\", i = ".inc", t = ".txt";
var searchPath = new string[] {
b + l + n,
b + l + n + i,
b + l + n + t,
s + l + n,
s + l + n + i,
s + l + n + t,
};
foreach (var path in searchPath) {
if (System.IO.File.Exists(path)) {
new EditorFile(path);
return true;
}
}
return false;
}
public static void SaveAll() {
if (BatchStart != null) BatchStart.Invoke(All, EventArgs.Empty);
All.ForEach(m => m.Save());
if (BatchEnd != null) BatchEnd.Invoke(All, EventArgs.Empty);
}
public static bool ProceedWithUnsavedChanges() {
if (All.Count < 1) return true;
foreach (EditorFile i in All) if (!i.Container.ProceedWithUnsavedChanges()) return false;
return true;
}
public static void Run(bool debug = true, bool andExit = false) {
if (All.Count < 1) return;
var main = All.First(i => i.Type == Types.SceneryMain);
if (main != null && !String.IsNullOrWhiteSpace(main.BaseDirectory) && ProceedWithUnsavedChanges()) {
var simExe = String.Format("{0}\\{1}", main.BaseDirectory, System.IO.Path.GetFileName(Properties.Settings.Default.SimExe));
if (System.IO.File.Exists(simExe)) {
var m = (new ScnSyntax.FirstActiveDynamic()).Match(main.Editor.Text);
if (m.Success) {
System.IO.Directory.SetCurrentDirectory(main.BaseDirectory);
var i = new ProcessStartInfo();
i.FileName = simExe;
i.Arguments = String.Format("-s {0} -v {1}", main.FileName, m.Value);
i.WindowStyle = ProcessWindowStyle.Minimized;
var p = Process.Start(i);
if (debug && !andExit) {
p.WaitForExit();
var logPath = String.Format("{0}\\{1}", main.BaseDirectory, "log.txt");
if (System.IO.File.Exists(logPath)) new EditorFile(logPath, Roles.Log);
}
if (andExit) Application.Exit();
}
}
}
}
#endregion
#endregion
#region Fields
internal new Editor Editor;
internal EditorContainer Container;
#endregion
#region Properties
internal override string Text { get { return Editor != null && !String.IsNullOrEmpty(Editor.Text) ? Editor.Text : base.Text; } }
#endregion
#region Constructors
public EditorFile(string path, Roles role = Roles.Any) : base(path, role) {
Open(DockState.Document);
}
public EditorFile(ProjectFile file) : base(file.Path, file.Role) {
if (file.TextCache != null) TextCache = file.TextCache;
IsChanged = file.IsChanged;
Open(DockState.Document);
var projectIndex = ProjectFile.All.FindIndex(i => i.Path == Path);
if (projectIndex >= 0) ProjectFile.All[projectIndex] = this;
}
#endregion
#region Internal methods
internal new void Normalize() {
if (Type == Types.SceneryMain || Type == Types.SceneryPart) {
if (ProcessingFile != null) ProcessingFile.Invoke(this, EventArgs.Empty);
Application.DoEvents();
Editor.BeginAutoUndo();
Editor.Text = base.Normalize();
Editor.OnSyntaxHighlight(new TextChangedEventArgs(Editor.VisibleRange));
if (ProcessingFileDone != null) ProcessingFileDone.Invoke(this, EventArgs.Empty);
Application.DoEvents();
}
}
internal void Save() {
if (Editor.IsChanged) {
if (WritingFile != null) WritingFile.Invoke(this, EventArgs.Empty);
Application.DoEvents();
Editor.SaveToFile();
if (WritingFileDone != null) WritingFileDone.Invoke(this, EventArgs.Empty);
Application.DoEvents();
}
}
#endregion
#region Private methods
private void Open(DockState dockState = DockState.Document) {
var existing = All.FirstOrDefault(i => i.Path == this.Path);
if (existing != null) {
existing.Container.Activate();
return;
}
Editor = new Editor(this) { Font = Properties.Settings.Default.Font };
if (ReadingFile != null) ReadingFile.Invoke(this, EventArgs.Empty);
Application.DoEvents();
if (Role == Roles.Main) {
if (Main.SceneryPanel != null) Main.SceneryPanel.Dispose();
Reset();
Main.SceneryPanel = new ProjectPanel(this);
}
Editor.LoadFromFile();
(Main.WordWrapMenuItem as ToolStripMenuItem).Checked = Editor.WordWrap;
Container = new EditorContainer(Editor, Main.DockPanel, dockState);
Container.UpdateText();
if (ReadingFileDone != null) ReadingFileDone.Invoke(this, EventArgs.Empty);
Application.DoEvents();
if (All.Count < 1) {
var s = Properties.Settings.Default;
s.LastOpenFile = Path;
s.Save();
}
All.Add(this);
Status.FileName = FileName;
}
#endregion
}
}