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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
// Copyright (c) 2013-2021 Bruyère Jean-Philippe <jp_bruyere@hotmail.com>
//
// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
using System;
using Crow;
using System.IO;
using Glfw;
using Crow.Text;
using System.Collections.Generic;
using Encoding = System.Text.Encoding;
using Crow.DebugLogger;
namespace Samples
{
public class SampleBaseForEditor : SampleBase
{
static SampleBaseForEditor() {
//DbgLogger.ConsoleOutput = !Configuration.Global.Get<bool> (nameof (DebugLogToFile));
//RecordedEvents = Configuration.Global.Get<ObservableList<DbgEvtType>> ("RecordedEvents");
}
public static SampleBaseForEditor CurrentProgramInstance;
protected override void OnInitialized () {
initCommands ();
base.OnInitialized ();
if (string.IsNullOrEmpty (CurrentDir))
CurrentDir = Path.Combine (Directory.GetCurrentDirectory (), "Interfaces");
}
protected const string _defaultFileName = "unnamed.txt";
protected string source = "", origSource;
protected Editor editor;
public string CurrentDir {
get => Configuration.Global.Get<string> (nameof (CurrentDir));
set {
if (CurrentDir == value)
return;
Configuration.Global.Set (nameof (CurrentDir), value);
NotifyValueChanged (CurrentDir);
}
}
public string CurrentFile {
get => Configuration.Global.Get<string> (nameof (CurrentFile));
set {
if (CurrentFile == value)
return;
Configuration.Global.Set (nameof (CurrentFile), value);
NotifyValueChanged (CurrentFile);
}
}
public virtual string Source {
get => source;
set {
if (source == value)
return;
source = value;
CMDSave.CanExecute = IsDirty;
NotifyValueChanged (source);
NotifyValueChanged ("IsDirty", IsDirty);
}
}
#region DebugLog
bool debugLogRecording;
public bool DebugLoggingEnabled => DbgLogger.IsEnabled;
public IEnumerable<DbgEvent> DebugEvents => DbgLogger.Events;
static ObservableList<DbgEvtType> RecordedEvents;
public bool DebugLogRecording {
get => debugLogRecording;
set {
if (debugLogRecording == value)
return;
debugLogRecording = value;
NotifyValueChanged(debugLogRecording);
}
}
public bool DebugLogToFile {
get => Configuration.Global.Get<bool> (nameof(DebugLogToFile));
set {
if (DebugLogToFile == value)
return;
Configuration.Global.Set (nameof(DebugLogToFile), value);
NotifyValueChanged(DebugLogToFile);
DbgLogger.ConsoleOutput = !value;
}
}
public string DebugLogFilePath {
get => Configuration.Global.Get<string> (nameof (DebugLogFilePath));
set {
if (CurrentFile == value)
return;
Configuration.Global.Set (nameof (DebugLogFilePath), value);
NotifyValueChanged (DebugLogFilePath);
}
}
#endregion
public new bool IsDirty => source != origSource;
public ActionCommand CMDNew, CMDOpen, CMDSave, CMDSaveAs, CMDQuit, CMDShowLeftPane,
CMDUndo, CMDRedo, CMDCut, CMDCopy, CMDPaste, CMDHelp, CMDAbout, CMDOptions;
public CommandGroup EditorFileCommands, EditorEditCommands, EditorAllCommands;
protected virtual void initCommands ()
{
CMDNew = new ActionCommand ("New", new Action (onNewFile), "#Icons.blank-file.svg");
CMDSave = new ActionCommand ("Save", new Action (onSave), "#Icons.save.svg", false);
CMDSaveAs = new ActionCommand ("Save As...", new Action (onSaveAs), "#Icons.save.svg");
CMDQuit = new ActionCommand ("Quit", new Action (() => base.Quit ()), "#Icons.exit.svg");
CMDUndo = new ActionCommand ("Undo", new Action (undo),"#Icons.undo.svg", false);
CMDRedo = new ActionCommand ("Redo", new Action (redo),"#Icons.redo.svg", false);
CMDCut = new ActionCommand ("Cut", new Action (cut), "#Icons.scissors.svg", false);
CMDCopy = new ActionCommand ("Copy", new Action (copy), "#Icons.copy-file.svg", false);
CMDPaste= new ActionCommand ("Paste", new Action (paste), "#Icons.paste-on-document.svg", false);
EditorFileCommands = new CommandGroup ("File", CMDSave, CMDSaveAs, CMDQuit);
EditorEditCommands = new CommandGroup ("Edit", CMDUndo, CMDRedo, CMDCut, CMDCopy, CMDPaste);
EditorAllCommands = new CommandGroup (EditorFileCommands, EditorEditCommands);
}
protected Stack<TextChange> undoStack = new Stack<TextChange> ();
protected Stack<TextChange> redoStack = new Stack<TextChange> ();
protected TextSpan selection;
protected string SelectedText =>
selection.IsEmpty ? "" : Source.AsSpan (selection.Start, selection.Length).ToString ();
public void onEditorSelectionChanged(Object sender, TextSelectionChangeEventArgs e) {
selection = e.Selection;
CMDCut.CanExecute = CMDCopy.CanExecute = !selection.IsEmpty;
}
protected void undo () {
if (undoStack.TryPop (out TextChange tch)) {
redoStack.Push (tch.Inverse (source));
CMDRedo.CanExecute = true;
apply (tch);
editor.SetCursorPosition (tch.End + tch.ChangedText.Length);
}
if (undoStack.Count == 0)
CMDUndo.CanExecute = false;
}
protected void redo () {
if (redoStack.TryPop (out TextChange tch)) {
undoStack.Push (tch.Inverse (source));
CMDUndo.CanExecute = true;
apply (tch);
editor.SetCursorPosition (tch.End + tch.ChangedText.Length);
}
if (redoStack.Count == 0)
CMDRedo.CanExecute = false;
}
protected void resetUndoRedo () {
undoStack.Clear ();
redoStack.Clear ();
CMDUndo.CanExecute = false;
CMDRedo.CanExecute = false;
}
protected void cut () {
copy ();
applyChange (new TextChange (selection.Start, selection.Length, ""));
}
protected void copy () {
Clipboard = SelectedText;
CMDPaste.CanExecute = !string.IsNullOrEmpty (Clipboard);
}
protected void paste () {
applyChange (new TextChange (selection.Start, selection.Length, Clipboard));
}
protected void onNewFile () {
if (IsDirty) {
MessageBox.ShowModal (this, MessageBox.Type.YesNo, "Current file has unsaved changes, are you sure?").Yes += (sender, e) => newFile ();
} else
newFile ();
}
protected void onSave ()
{
if (!File.Exists (CurrentFile)) {
onSaveAs ();
return;
}
save ();
}
protected void onSaveAs ()
{
string dir = Path.GetDirectoryName (CurrentFile);
if (string.IsNullOrEmpty (dir))
dir = Directory.GetCurrentDirectory ();
LoadIMLFragment (@"<FileDialog Width='60%' Height='50%' Caption='Save as ...' CurrentDirectory='" +
dir + "' SelectedFile='" +
Path.GetFileName(CurrentFile) + "' OkClicked='saveFileDialog_OkClicked'/>").DataSource = this;
}
protected void saveFileDialog_OkClicked (object sender, EventArgs e)
{
FileDialog fd = sender as FileDialog;
if (string.IsNullOrEmpty (fd.SelectedFileFullPath))
return;
if (File.Exists(fd.SelectedFileFullPath)) {
MessageBox mb = MessageBox.ShowModal (this, MessageBox.Type.YesNo, "File exists, overwrite?");
mb.Yes += (sender2, e2) => {
CurrentFile = fd.SelectedFileFullPath;
save ();
};
return;
}
CurrentFile = fd.SelectedFileFullPath;
save ();
}
protected void newFile()
{
disableTextChangedEvent = true;
Source = @"<Label Text='Hello World' Background='MediumSeaGreen' Margin='10'/>";
disableTextChangedEvent = false;
resetUndoRedo ();
if (!string.IsNullOrEmpty (CurrentFile))
CurrentFile = Path.Combine (Path.GetDirectoryName (CurrentFile), "newfile.crow");
else
CurrentFile = Path.Combine (CurrentDir, "newfile.crow");
}
protected void save () {
using (Stream s = new FileStream(CurrentFile, FileMode.Create)) {
s.WriteByte (0xEF);
s.WriteByte (0xBB);
s.WriteByte (0xBF);
byte [] buff = Encoding.UTF8.GetBytes (source);
s.Write (buff, 0, buff.Length);
}
origSource = source;
NotifyValueChanged ("IsDirty", IsDirty);
CMDSave.CanExecute = false;
}
protected void reloadFromFile () {
disableTextChangedEvent = true;
if (File.Exists (CurrentFile)) {
using (Stream s = new FileStream (CurrentFile, FileMode.Open)) {
using (StreamReader sr = new StreamReader (s))
Source = origSource = sr.ReadToEnd ();
}
}
disableTextChangedEvent = false;
resetUndoRedo ();
}
protected bool disableTextChangedEvent = false;
protected void apply (TextChange change, bool updateEditorPosition = true) {
Span<char> tmp = stackalloc char[source.Length + (change.ChangedText.Length - change.Length)];
ReadOnlySpan<char> src = source.AsSpan ();
src.Slice (0, change.Start).CopyTo (tmp);
if (!string.IsNullOrEmpty (change.ChangedText))
change.ChangedText.AsSpan ().CopyTo (tmp.Slice (change.Start));
src.Slice (change.End).CopyTo (tmp.Slice (change.Start + change.ChangedText.Length));
disableTextChangedEvent = true;
Source = tmp.ToString ();
disableTextChangedEvent = false;
if (updateEditorPosition) {
editor.SelectionStart = null;
editor.SetCursorPosition (change.Start + change.ChangedText.Length);
forceTextCursor();
}
}
protected void applyChange (TextChange change, bool updateEditorPosition = true) {
undoStack.Push (change.Inverse (source));
redoStack.Clear ();
CMDUndo.CanExecute = true;
CMDRedo.CanExecute = false;
apply (change, updateEditorPosition);
}
public void goUpDirClick (object sender, MouseButtonEventArgs e)
{
if (string.IsNullOrEmpty (CurrentDir))
return;
string root = Directory.GetDirectoryRoot (CurrentDir);
if (CurrentDir == root)
return;
CurrentDir = Directory.GetParent (CurrentDir).FullName;
}
protected void Dv_SelectedItemChanged (object sender, SelectionChangeEventArgs e)
{
FileSystemInfo fi = e.NewValue as FileSystemInfo;
if (fi == null)
return;
if (fi is DirectoryInfo)
return;
if (IsDirty) {
MessageBox mb = MessageBox.ShowModal (this, MessageBox.Type.YesNo, "Current file has unsaved changes, are you sure?");
mb.Yes += (mbsender, mbe) => { CurrentFile = fi.FullName; reloadFromFile (); };
return;
}
CurrentFile = fi.FullName;
reloadFromFile ();
}
protected void onTextChanged (object sender, TextChangeEventArgs e) {
if (disableTextChangedEvent)
return;
applyChange (e.Change, false);
}
protected void textView_KeyDown (object sender, Crow.KeyEventArgs e) {
if (Ctrl) {
if (e.Key == Glfw.Key.W) {
if (e.Modifiers.HasFlag(Modifier.Shift))
CMDRedo.Execute ();
else
CMDUndo.Execute ();
} else if (e.Key == Glfw.Key.S) {
onSave ();
}
}
}
}
}
|