forked from dimven/NavisPythonShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIronPythonConsole.xaml.cs
More file actions
125 lines (112 loc) · 4.23 KB
/
IronPythonConsole.xaml.cs
File metadata and controls
125 lines (112 loc) · 4.23 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 Microsoft.Scripting.Hosting.Shell;
using ICSharpCode.AvalonEdit;
using ICSharpCode.AvalonEdit.Highlighting;
using System.IO;
using System.Xml;
using Microsoft.Win32;
using Microsoft.Scripting;
using System.Threading;
namespace NavisPythonShell
{
/// <summary>
/// Interaction logic for IronPythonConsole.xaml
/// </summary>
public partial class IronPythonConsole : Window
{
private ConsoleOptions consoleOptionsProvider;
// this is the name of the file currently being edited in the pad
private string currentFileName;
public IronPythonConsole()
{
Initialized += new EventHandler(MainWindow_Initialized);
IHighlightingDefinition pythonHighlighting;
using (Stream s = typeof(IronPythonConsole).Assembly.GetManifestResourceStream("NavisPythonShell.Resources.Python.xshd"))
{
if (s == null)
throw new InvalidOperationException("Could not find embedded resource");
using (XmlReader reader = new XmlTextReader(s))
{
pythonHighlighting = ICSharpCode.AvalonEdit.Highlighting.Xshd.
HighlightingLoader.Load(reader, HighlightingManager.Instance);
}
}
// and register it in the HighlightingManager
HighlightingManager.Instance.RegisterHighlighting("Python Highlighting", new string[] { ".cool" }, pythonHighlighting);
InitializeComponent();
textEditor.SyntaxHighlighting = pythonHighlighting;
textEditor.PreviewKeyDown += new KeyEventHandler(textEditor_PreviewKeyDown);
consoleOptionsProvider = new ConsoleOptions(consoleControl.Pad);
}
void MainWindow_Initialized(object sender, EventArgs e)
{
//propertyGridComboBox.SelectedIndex = 1;
}
void openFileClick(object sender, RoutedEventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.CheckFileExists = true;
if (dlg.ShowDialog() ?? false)
{
currentFileName = dlg.FileName;
textEditor.Load(currentFileName);
//textEditor.SyntaxHighlighting = HighlightingManager.Instance.GetDefinitionByExtension(Path.GetExtension(currentFileName));
}
}
void saveFileClick(object sender, EventArgs e)
{
if (currentFileName == null)
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.DefaultExt = ".txt";
if (dlg.ShowDialog() ?? false)
{
currentFileName = dlg.FileName;
}
else
{
return;
}
}
textEditor.Save(currentFileName);
}
void runClick(object sender, EventArgs e)
{
RunStatements();
}
void textEditor_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.F5) RunStatements();
}
void RunStatements()
{
string statementsToRun = "";
if (textEditor.TextArea.Selection.Length > 0)
statementsToRun = textEditor.TextArea.Selection.GetText();
else
statementsToRun = textEditor.TextArea.Document.Text;
consoleControl.Pad.Console.RunStatements(statementsToRun);
}
// Clear the contents on first click inside the editor
private void textEditor_GotFocus(object sender, RoutedEventArgs e)
{
TextEditor tb = (TextEditor)sender;
tb.Text = string.Empty;
// Remove the handler from the list otherwise this handler will clear
// editor contents every time the editor gains focus.
tb.GotFocus -= textEditor_GotFocus;
}
}
}