aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Modules/Tango.FSE.Procedures/Views/ProcedureDesignerView.xaml.cs
blob: 1e5deb04adbb0b46a26c9e007472b13f8939f720 (plain)
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
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.Scripting.Basic;
using Tango.Scripting.Editors;

namespace Tango.FSE.Procedures.Views
{
    /// <summary>
    /// Interaction logic for ProcedureDesignerView.xaml
    /// </summary>
    public partial class ProcedureDesignerView : UserControl, IProcedureDesignerView
    {
        private ProcedureDesignerViewVM _vm;

        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());
        }
    }
}