24 private Builder _builder;
26#pragma warning disable 649
28 private TreeView scriptView;
30 private TextView view;
32 private TextView outputBox;
34 private TextView errorBox;
36 private TextView logBox;
38 private Button scriptLoadBut;
40 private Button saveBut;
42 private Button runBut;
44 private Button stopBut;
46 private Label scriptLabel;
48 private CheckButton headlessBox;
50 private Notebook tabsView;
51#pragma warning restore 649
55 #region Constructors / Destructors
62 Builder builder =
new Builder(
new FileStream(System.IO.Path.GetDirectoryName(Environment.ProcessPath) +
"/" +
"Glade/Scripting.glade", FileMode.Open));
63 return new Scripting(builder, builder.GetObject(
"scripting").Handle);
67 protected Scripting(Builder builder, IntPtr handle) : base(handle)
70 builder.Autoconnect(
this);
71 scriptView.RowActivated += ScriptView_RowActivated;
72 scriptView.ButtonPressEvent += ScriptView_ButtonPressEvent;
73 this.KeyPressEvent += Scripting_KeyPressEvent;
74 saveBut.ButtonPressEvent += SaveBut_ButtonPressEvent;
75 runBut.ButtonPressEvent += runButton_Click;
76 scriptLoadBut.ButtonPressEvent += scriptLoadBut_Click;
77 outputBox.ButtonPressEvent += OutputBox_ButtonPressEvent;
78 errorBox.ButtonPressEvent += OutputBox_ButtonPressEvent;
79 logBox.ButtonPressEvent += OutputBox_ButtonPressEvent;
80 stopBut.ButtonPressEvent += stopBut_Click;
81 tabsView.SwitchPage += TabsView_SwitchPage;
83 if (!Directory.Exists(System.IO.Path.GetDirectoryName(Environment.ProcessPath) +
"//" +
"Scripts"))
84 Directory.CreateDirectory(System.IO.Path.GetDirectoryName(Environment.ProcessPath) +
"//" +
"Scripts");
85 if (!Directory.Exists(System.IO.Path.GetDirectoryName(Environment.ProcessPath) +
"//" +
"Tools"))
86 Directory.CreateDirectory(System.IO.Path.GetDirectoryName(Environment.ProcessPath) +
"//" +
"Tools");
88 scriptLabel.Text =
"NewScript.cs";
89 App.ApplyStyles(
this);
98 private void TabsView_SwitchPage(
object o, SwitchPageArgs args)
100 logBox.Buffer.Text = log;
101 errorBox.Buffer.Text = error;
102 outputBox.Buffer.Text = output;
110 private void OutputBox_ButtonPressEvent(
object o, ButtonPressEventArgs args)
112 logBox.Buffer.Text = log;
113 errorBox.Buffer.Text = error;
114 outputBox.Buffer.Text = output;
122 private void ScriptView_ButtonPressEvent(
object o, ButtonPressEventArgs args)
131 private void SaveBut_ButtonPressEvent(
object o, ButtonPressEventArgs args)
141 private void Scripting_KeyPressEvent(
object o, KeyPressEventArgs args)
143 if (args.Event.Key == Gdk.Key.s && args.Event.State == Gdk.ModifierType.ControlMask)
155 private void ScriptView_RowActivated(
object o, RowActivatedArgs args)
157 TreeView treeView = (TreeView)o;
158 TreePath path = args.Path;
162 if (treeView.Model.GetIter(out iter, path))
165 string text = (string)treeView.Model.GetValue(iter, 0);
166 if (scripts.ContainsKey(text))
168 selectedItem = scripts[text];
169 view.Buffer.Text = selectedItem.scriptString;
170 scriptLabel.Text = selectedItem.name;
177 public static Window window;
179 public static string log;
180 public static string output;
181 public static string error;
182 public static string ImageJPath = Fiji.ImageJPath;
183 public static Dictionary<string,Script> scripts =
new Dictionary<string,Script>();
184 public static void LogLine(
string s)
186 log += s + Environment.NewLine;
193 public string scriptString;
194 public bool exit =
false;
195 public dynamic script;
197 public string output =
"";
198 public bool done =
false;
199 public static List<string> usings =
new List<string>();
200 public Exception ex =
null;
201 public Thread thread;
202 public ScriptType type = ScriptType.script;
204 public Script(
string file,
string scriptStr)
206 name = System.IO.Path.GetFileName(file);
207 scriptString = scriptStr;
208 if (file.EndsWith(
".txt") || file.EndsWith(
".ijm"))
209 type = ScriptType.imagej;
213 public Script(
string file)
215 name = System.IO.Path.GetFileName(file);
216 scriptString = File.ReadAllText(file);
218 if (file.EndsWith(
".txt") || file.EndsWith(
".ijm"))
219 type = ScriptType.imagej;
227 scriptName = rn.name;
228 Thread t =
new Thread(
new ThreadStart(RunScript));
231 private static string scriptName =
"";
232 private static string str =
"";
234 private static async
void RunScript()
236 Script rn = scripts[scriptName];
238 if (rn.type == ScriptType.imagej)
244 await Fiji.RunStringFiji(
ImageView.SelectedImage,rn.scriptString,
"",
false);
246 ImageJ.RunString(
ImageView.SelectedImage, rn.scriptString,
"",
false);
259 rn.script = CSScript.Evaluator.LoadCode(rn.scriptString);
260 rn.obj = rn.script.Load();
261 rn.output = rn.obj.ToString();
268 error = e.Message + Environment.NewLine + e.StackTrace;
283 @"//css_reference " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Name +
@".dll
286 using System.Threading;
289 foreach (
string s
in usings)
291 loader += usings + Environment.NewLine;
302 dynamic script = CSScript.Evaluator.LoadCode(loader);
303 return script.Load();
307 MessageDialog md =
new MessageDialog(window,
308 DialogFlags.DestroyWithParent, MessageType.Info,
309 ButtonsType.Close, e.Message +
", " + e.Source);
316 if (!scripts.ContainsKey(name))
317 scripts.Add(name,
this);
318 scriptName = this.name;
319 thread =
new Thread(
new ThreadStart(RunScript));
335 return name.ToString() +
", " + thread.ThreadState.ToString();
338 return name.ToString();
372 st.type = Event.Move;
385 st.type = Event.Down;
398 st.type = Event.None;
406 public bool processed =
false;
408 public override string ToString()
410 return type.ToString() +
" ,(" + p.X.ToString() +
", " + p.Y.ToString() +
"), " + buts.ToString();
423 public enum ScriptType
430 private Script SelectedItem
437 private static State state;
446 public static bool Exit(
string name)
448 if(scripts.ContainsKey(name))
450 return scripts[name].exit;
468 if (s.p.X == state.p.X && s.p.Y == state.p.Y && s.type == state.type)
470 state.processed =
true;
478 Gtk.TreeViewColumn typeCol =
new Gtk.TreeViewColumn();
479 typeCol.Title =
"Script";
480 Gtk.CellRendererText typeCell =
new Gtk.CellRendererText();
481 typeCol.PackStart(typeCell,
true);
482 scriptView.AppendColumn(typeCol);
483 typeCol.AddAttribute(typeCell,
"text", 0);
485 Gtk.TreeViewColumn statCol =
new Gtk.TreeViewColumn();
486 statCol.Title =
"Status";
487 Gtk.CellRendererText statCell =
new Gtk.CellRendererText();
488 statCol.PackStart(statCell,
true);
489 scriptView.AppendColumn(statCol);
490 statCol.AddAttribute(statCell,
"text", 1);
492 Gtk.TreeStore store =
new Gtk.TreeStore(typeof(
string), typeof(
string));
493 scriptView.Model = store;
495 string st = System.IO.Path.GetDirectoryName(Environment.ProcessPath);
496 foreach (
string file
in Directory.GetFiles(st +
"/Scripts"))
498 if (!scripts.ContainsKey(System.IO.Path.GetFileName(file)))
501 if(sc.thread ==
null)
502 store.AppendValues(sc.name, ThreadState.Unstarted.ToString());
504 store.AppendValues(sc.name, sc.thread.ThreadState.ToString());
505 scripts.Add(sc.name, sc);
508 foreach (
string file
in Directory.GetFiles(st +
"/Tools"))
510 if (file.EndsWith(
".cs"))
512 if (!scripts.ContainsKey(System.IO.Path.GetFileName(file)))
515 if (sc.thread ==
null)
516 store.AppendValues(sc.name, ThreadState.Unstarted.ToString());
518 store.AppendValues(sc.name, sc.thread.ThreadState.ToString());
519 scripts.Add(sc.name, sc);
528 Gtk.TreeStore store =
new Gtk.TreeStore(typeof(
string), typeof(
string));
529 foreach (
Script sc
in scripts.Values)
531 if (sc.thread ==
null)
532 store.AppendValues(sc.name, ThreadState.Unstarted.ToString());
534 store.AppendValues(sc.name, sc.thread.ThreadState.ToString());
536 scriptView.Model = store;
590 scripts.Add(sc.name,sc);
600 scripts.Add(sc.name, sc);
616 outputBox.Buffer.Text =
"";
617 logBox.Buffer.Text =
"";
618 if (SelectedItem ==
null)
622 sc.scriptString = view.Buffer.Text;
625 outputBox.Buffer.Text = sc.output;
626 logBox.Buffer.Text = log;
634 if (SelectedItem ==
null)
644 private void runToolStripMenuItem_Click(
object sender, EventArgs e)
652 private void openScriptFolderToolStripMenuItem_Click(
object sender, EventArgs e)
654 string path = System.IO.Path.GetDirectoryName(Environment.ProcessPath) +
"\\Scripts";
655 System.Diagnostics.Process.Start(
"explorer.exe", path);
661 private void refreshToolStripMenuItem_Click(
object sender, EventArgs e)
680 private void runButton_Click(
object sender, ButtonPressEventArgs args)
682 if (scriptLabel.Text.EndsWith(
".ijm"))
685 Fiji.RunStringFiji(
ImageView.SelectedImage,view.Buffer.Text,
ImageView.SelectedImage.ID, headlessBox.Active);
687 ImageJ.RunString(
ImageView.SelectedImage, view.Buffer.Text,
ImageView.SelectedImage.ID, headlessBox.Active);
699 private void scriptLoadBut_Click(
object sender, ButtonPressEventArgs args)
701 Gtk.FileChooserDialog filechooser =
702 new Gtk.FileChooserDialog(
"Choose script file to open",
704 FileChooserAction.Open,
705 "Cancel", ResponseType.Cancel,
706 "Save", ResponseType.Accept);
707 filechooser.SetCurrentFolder(System.IO.Path.GetDirectoryName(Environment.ProcessPath));
708 if (filechooser.Run() != (
int)ResponseType.Accept)
711 Script script =
new Script(filechooser.Filename, File.ReadAllText(filechooser.Filename));
712 script.name = System.IO.Path.GetFileName(filechooser.Filename);
713 view.Buffer.Text = script.scriptString;
714 scriptLabel.Text = System.IO.Path.GetFileName(filechooser.Filename);
716 scripts.Add(script.name, script);
727 Gtk.FileChooserDialog filechooser =
728 new Gtk.FileChooserDialog(
"Choose script filename to save",
730 FileChooserAction.Save,
731 "Cancel", ResponseType.Cancel,
732 "Save", ResponseType.Accept);
733 filechooser.SetCurrentFolder(System.IO.Path.GetDirectoryName(Environment.ProcessPath));
734 if (filechooser.Run() != (
int)ResponseType.Accept)
736 scriptLabel.Text = System.IO.Path.GetFileName(filechooser.Filename);
737 File.WriteAllText(filechooser.Filename, view.Buffer.Text);
738 if(!scripts.ContainsKey(scriptLabel.Text))
740 scripts.Add(scriptLabel.Text,
new Script(filechooser.Filename, view.Buffer.Text));
743 filechooser.Destroy();
750 private void stopBut_Click(
object sender, ButtonPressEventArgs args)