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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Tango.FSE.Procedures.Contracts;
using Tango.FSE.Procedures.ViewModels;
using Tango.Logging;
using Tango.Scripting.Basic;
using Tango.Scripting.Core;
using Tango.Scripting.Editors;
using Tango.SharedUI.Helpers;
namespace Tango.FSE.Procedures.Views
{
/// <summary>
/// Interaction logic for ProcedureDesignerView.xaml
/// </summary>
public partial class ProcedureDesignerView : UserControl, IProcedureDesignerView
{
private ProcedureDesignerViewVM _vm;
public event EventHandler<string> FileDropped;
public ProcedureDesignerView()
{
InitializeComponent();
this.Register<IProcedureDesignerView>();
Loaded += (_, __) => _vm = DataContext as ProcedureDesignerViewVM;
}
private void OnScriptItemDoubleClick(object sender, MouseButtonEventArgs e)
{
_vm.OpenScript((sender as FrameworkElement).DataContext as Script);
}
private ScriptEditor GetCurrentEditor()
{
var editor = tabControl.FindVisualChildren<ScriptEditor>().FirstOrDefault(x => x.IsVisible);
return editor;
}
private List<ScriptEditor> GetAllEditors()
{
return tabControl.FindVisualChildren<ScriptEditor>().ToList();
}
public void FormatCode()
{
GetCurrentEditor()?.FormatCode();
}
public void HighlightCode(int position, int length, int line)
{
GetCurrentEditor()?.Highlight(position, length, line);
}
public void InsertCode(string code)
{
GetCurrentEditor()?.InsertCode(code);
}
public void InvalidateHighlighting()
{
Dispatcher.BeginInvoke(new Action(() =>
{
foreach (var editor in tabControl.FindVisualChildren<ScriptEditor>())
{
editor.InvalidateHighlighting(true);
}
}));
}
public void Find(string text)
{
GetCurrentEditor()?.Find(text);
}
public void ReplaceNext(string text, string replace)
{
GetCurrentEditor()?.ReplaceNext(text, replace);
ColorizeKeyword(text);
}
public int ReplaceAll(string text, string replace)
{
var editor = GetCurrentEditor();
if (editor != null)
{
int count = editor.ReplaceAll(text, replace);
ColorizeKeyword(text);
return count;
}
else
{
return 0;
}
}
public void FocusCurrentEditor()
{
var editor = GetCurrentEditor();
if (editor != null)
{
editor.Focus();
Keyboard.Focus(editor);
}
}
public void ColorizeKeyword(string text)
{
GetCurrentEditor()?.ColorizeByKeyword(text);
}
public void ResetColrization()
{
GetAllEditors().ForEach(x => x.ResetColorizationByKeyword());
}
private void OnDialogItemDoubleClick(object sender, MouseButtonEventArgs e)
{
_vm.EditProcedureDialog((sender as FrameworkElement).DataContext as ProcedureDialog);
}
public void HighlightError(int position, int length)
{
GetCurrentEditor()?.HighlighError(position, length);
}
public void ClearErrors()
{
GetAllEditors().ForEach(x => x.ClearErrors());
}
public void ScrollToLine(int lineNumber)
{
GetCurrentEditor().ScrollToLine(lineNumber);
}
public void HighlightRuntimeError(int lineNumber)
{
ScrollToLine(lineNumber);
var editor = GetCurrentEditor();
editor.HighlightErrorLine(lineNumber);
UIHelper.DoEvents();
Point? p = editor.GetLineVisualPosition(lineNumber);
if (p != null)
{
Point point = p.Value;
point = new Point(point.X, point.Y - editor.VerticalOffset);
double height = editor.ActualHeight;
if (point.Y > runTimeErrorDock.ActualHeight)
{
DockPanel.SetDock(runTimeErrorLineGrid, Dock.Bottom);
runTimeErrorLineTransform.Angle = 0;
Canvas.SetTop(runTimeErrorDock, point.Y - runTimeErrorDock.ActualHeight);
}
else
{
DockPanel.SetDock(runTimeErrorLineGrid, Dock.Top);
runTimeErrorLineTransform.Angle = 90;
Canvas.SetTop(runTimeErrorDock, point.Y);
}
Canvas.SetLeft(runTimeErrorDock, Math.Min(point.X, editor.ActualWidth - 110 - runTimeErrorDock.ActualWidth));
runTimeErrorCanvas.Visibility = Visibility.Visible;
}
}
public void CloseRunTimeError()
{
runTimeErrorCanvas.Visibility = Visibility.Hidden;
GetCurrentEditor().ResetColorizationByKeyword();
}
public void HighlightDebugRequest(Object toDebug, int lineNumber)
{
ScrollToLine(lineNumber);
var editor = GetCurrentEditor();
editor.HighlightDebugLine(lineNumber);
UIHelper.DoEvents();
Point? p = editor.GetLineVisualPosition(lineNumber);
if (p != null)
{
Point point = p.Value;
}
}
public List<ScriptBreakPoint> GetBreakPoints()
{
List<ScriptBreakPoint> breakPoints = new List<ScriptBreakPoint>();
foreach (var editor in GetAllEditors())
{
breakPoints.AddRange(editor.GetBreakPoints());
}
return breakPoints;
}
public void HighlightBreakPointRequest(int lineNumber, List<ScriptBreakPointSymbol> symbols)
{
UIHelper.DoEvents();
ScrollToLine(lineNumber);
var editor = GetCurrentEditor();
editor.HighlightBreakPoint(lineNumber, symbols);
UIHelper.DoEvents();
}
public void ResetBreakPointRequest()
{
GetCurrentEditor().ResetBreakPointLine();
runTimeBreakPointCanvas.Visibility = Visibility.Hidden;
}
private void ScriptEditor_BreakPointSymbolPressed(object sender, BreakPointSymbolPressedEventArgs e)
{
try
{
var editor = GetCurrentEditor();
var point = new Point(e.Position.X, e.Position.Y - editor.VerticalOffset);
Canvas.SetTop(runTimeBreakPointDock, point.Y);
Canvas.SetLeft(runTimeBreakPointDock, Math.Min(point.X, editor.ActualWidth - 110 - runTimeBreakPointDock.ActualWidth));
try
{
runTimeBreakPointTreeView.SetExpansion(false);
}
catch { }
runTimeBreakPointTreeView.DataContext = DebugNode.CreateNode(e.BreakPointSymbol.Name, e.BreakPointSymbol.SymbolObject);
runTimeBreakPointCanvas.Visibility = Visibility.Visible;
}
catch (Exception ex)
{
LogManager.Default.Log(ex, "Error initializing break point debug window.");
}
}
private void RunTimeBreakPointCanvas_MouseUp(object sender, MouseButtonEventArgs e)
{
runTimeBreakPointCanvas.Visibility = Visibility.Hidden;
}
public string GetCaretWord()
{
return GetCurrentEditor()?.GetCaretWord();
}
private void OnFileDrop(object sender, DragEventArgs e)
{
try
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
if (files != null && files.Length == 1)
{
var file = files.First();
FileDropped?.Invoke(this, file);
}
}
catch { }
}
}
}
|