-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
178 lines (151 loc) · 7.24 KB
/
MainWindow.xaml.cs
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
using DevExpress.Xpf.Core;
using DevExpress.Xpf.Grid;
using DevExpress.XtraRichEdit.API.Native;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Documents;
namespace DXRichEditControlAPISample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
ExampleCodeEditor codeEditor;
ExampleEvaluatorByTimer evaluator;
ExampleLanguage projectLanguage;
bool richEditControlVBLoaded = false;
bool richEditControlCsLoaded = false;
//CultureInfo defaultCulture = new CultureInfo("en-US");
public MainWindow() {
InitializeComponent();
string examplePath = "CodeExamples";
Dictionary<string, FileInfo> examplesCS = CodeExampleDemoUtils.GatherExamplesFromProject(examplePath, ExampleLanguage.Csharp);
Dictionary<string, FileInfo> examplesVB = CodeExampleDemoUtils.GatherExamplesFromProject(examplePath, ExampleLanguage.VB);
DisableTabs(examplesCS.Count, examplesVB.Count);
List<CodeExampleGroup> examples = CodeExampleDemoUtils.FindExamples(examplePath, examplesCS, examplesVB);
ShowExamplesInTreeList(treeList1, examples);
richEditControlCS.Loaded += richEditControlCS_Loaded;
richEditControlVB.Loaded += richEditControlVB_Loaded;
this.evaluator = new RichEditExampleEvaluatorByTimer();
this.evaluator.QueryEvaluate += OnExampleEvaluatorQueryEvaluate;
this.evaluator.OnBeforeCompile += evaluator_OnBeforeCompile;
this.evaluator.OnAfterCompile += evaluator_OnAfterCompile;
}
void evaluator_OnAfterCompile(object sender, OnAfterCompileEventArgs args) {
if (codeEditor != null)
codeEditor.AfterCompile(args.Result);
}
void evaluator_OnBeforeCompile(object sender, EventArgs args) {
Document document = richEditControl.Document;
document.BeginUpdate();
codeEditor.BeforeCompile();
richEditControl.CreateNewDocument();
document.Unit = DevExpress.Office.DocumentUnit.Document;
document.EndUpdate();
}
void richEditControlCS_Loaded(object sender, System.Windows.RoutedEventArgs e) {
if (richEditControlVBLoaded && !richEditControlCsLoaded)
CreateCodeEditor();
richEditControlCsLoaded = true;
}
void richEditControlVB_Loaded(object sender, System.Windows.RoutedEventArgs e) {
if (richEditControlCsLoaded && !richEditControlVBLoaded )
CreateCodeEditor();
richEditControlVBLoaded = true;
}
void DisableTabs(int examplesCSCount, int examplesVBCount) {
if (examplesCSCount == 0)
foreach (DXTabItem t in tabControl.Items) if (t.Header.ToString().StartsWith("CS")) t.IsEnabled = false;
if (examplesVBCount == 0)
foreach (DXTabItem t in tabControl.Items) if (t.Header.ToString().StartsWith("VB")) t.IsEnabled = false;
}
void CreateCodeEditor(){
System.Diagnostics.Debug.Assert(codeEditor == null);
this.codeEditor = new ExampleCodeEditor(richEditControlCS, richEditControlVB, richEditControlCSClass, richEditControlVBClass);
this.codeEditor.CurrentExampleLanguage = CurrentExampleLanguage;
ShowFirstExample();
}
void ShowExamplesInTreeList(TreeListControl treeList1, List<CodeExampleGroup> examples) {
treeList1.ItemsSource = examples;
}
void ShowFirstExample() {
projectLanguage = CodeExampleDemoUtils.DetectExampleLanguage("DXRichEditControlAPISample");
if (projectLanguage == ExampleLanguage.Csharp)
this.tabControl.SelectedIndex = 0;
else
this.tabControl.SelectedIndex = 1;
treeList1.View.ExpandAllNodes();
if (treeList1.View.Nodes.Count > 0)
treeList1.View.FocusedNode = treeList1.View.Nodes[0].Nodes.First();
}
public ExampleLanguage CurrentExampleLanguage
{
get
{
if (tabControl.SelectedContainer.Header.ToString().StartsWith("CS")) return ExampleLanguage.Csharp;
else return ExampleLanguage.VB;
}
set
{
if (this.codeEditor != null)
{
this.codeEditor.CurrentExampleLanguage = value;
}
}
}
private void OnNewExampleSelected(object sender, CurrentItemChangedEventArgs e) {
CodeExample newExample = e.NewItem as CodeExample;
CodeExample oldExample = e.OldItem as CodeExample;
if (newExample == null )
return;
if (codeEditor == null)
return;
string exampleCode = codeEditor.ShowExample(oldExample, newExample);
codeExampleNameLbl.Content = CodeExampleDemoUtils.ConvertStringToMoreHumanReadableForm(newExample.RegionName) + " example";
CodeEvaluationEventArgs args = new CodeEvaluationEventArgs();
InitializeCodeEvaluationEventArgs(args);
evaluator.ForceCompile(args);
if (newExample.HumanReadableGroupName == "Comments Actions")
{
this.richEditControl.Options.Comments.Visibility = DevExpress.XtraRichEdit.RichEditCommentVisibility.Visible;
}
else
{
this.richEditControl.Options.Comments.Visibility = DevExpress.XtraRichEdit.RichEditCommentVisibility.Hidden;
}
}
void InitializeCodeEvaluationEventArgs(CodeEvaluationEventArgs e) {
e.Result = true;
if (codeEditor == null)
return;
e.Code = codeEditor.CurrentCodeEditor.Text;
e.CodeClasses = codeEditor.CurrentCodeClassEditor.Text;
e.Language = CurrentExampleLanguage;
e.EvaluationParameter = richEditControl.Document;
}
void OnExampleEvaluatorQueryEvaluate(object sender, CodeEvaluationEventArgs e) {
e.Result = false;
if ((codeEditor != null) &&codeEditor.RichEditTextChanged) {
TimeSpan span = DateTime.Now - codeEditor.LastExampleCodeModifiedTime;
if (span < TimeSpan.FromMilliseconds(1000)) {
codeEditor.ResetLastExampleModifiedTime();
return;
}
InitializeCodeEvaluationEventArgs(e);
}
}
void tabControl_SelectionChanged(object sender, DevExpress.Xpf.Core.TabControlSelectionChangedEventArgs e) {
CurrentExampleLanguage = (((DXTabItem) e.NewSelectedItem).Header.ToString().StartsWith("CS")) ? ExampleLanguage.Csharp : ExampleLanguage.VB;
}
private void view_CustomColumnDisplayText(object sender, DevExpress.Xpf.Grid.TreeList.TreeListCustomColumnDisplayTextEventArgs e) {
if (e.Node.HasChildren && e.Node.Content is CodeExampleGroup) {
e.DisplayText = (e.Node.Content as CodeExampleGroup).Name;
}
}
}
}