BioGTK  6.7.0
A .NET library & program for annotating, editing various microscopy imaging formats using Bioformats supported images. Including whole slide, pyramidal, and series.
Loading...
Searching...
No Matches
BioConsole.cs
1using BioGTK;
2using Gtk;
3using Pango;
4using System;
5using System.Collections.Generic;
6using System.IO;
7
8namespace BioGTK
9{
10 public class BioConsole : Window
11 {
12 #region Properties
13
15 private Builder _builder;
16 private string pred = "";
17 private List<string> preds = new List<string>();
18 public static bool onTab = false;
19 public static bool useBioformats = true;
20 public static bool headless = false;
21 public static bool resultInNewTab = false;
22 int line = 0;
23#pragma warning disable 649
24 [Builder.Object]
25 private Label predLabel;
26 [Builder.Object]
27 private Button imagejBut;
28 [Builder.Object]
29 private Button runBut;
30 [Builder.Object]
31 private TextView textBox;
32 [Builder.Object]
33 private TextView consoleBox;
34 [Builder.Object]
35 private CheckButton headlessBox;
36 [Builder.Object]
37 private CheckButton bioformatsBox;
38 [Builder.Object]
39 private CheckButton resultsBox;
40 [Builder.Object]
41 private CheckButton selRadioBut;
42 [Builder.Object]
43 private CheckButton tabRadioBut;
44#pragma warning restore 649
45
46 #endregion
47
48 #region Constructors / Destructors
49
52 public static BioConsole Create()
53 {
54 Builder builder = new Builder(new FileStream(System.IO.Path.GetDirectoryName(Environment.ProcessPath) + "/" + "Glade/BioConsole.glade", FileMode.Open));
55 return new BioConsole(builder, builder.GetObject("console").Handle);
56 }
57
61 protected BioConsole(Builder builder, IntPtr handle) : base(handle)
62 {
63 _builder = builder;
64 builder.Autoconnect(this);
66 App.ApplyStyles(this);
67 }
68
69 #endregion
70
71 #region Handlers
72
74 protected void SetupHandlers()
75 {
76 runBut.Clicked += RunBut_Clicked;
77 imagejBut.Clicked += ImagejBut_Clicked;
78 headlessBox.Clicked += HeadlessBox_Clicked;
79 resultsBox.Clicked += ResultsBox_Clicked;
80 selRadioBut.Clicked += SelRadioBox_Clicked;
81 tabRadioBut.Clicked += TabRadioBox_Clicked;
82 consoleBox.Buffer.Changed += Buffer_Changed;
83 consoleBox.KeyPressEvent += Console_KeyPressEvent;
84 }
85
86 private int Measure(string s)
87 {
88 // Create a Pango layout using the label's context
89 using (var layout = new Pango.Layout(predLabel.PangoContext))
90 {
91 // Set the text and font description of the layout
92 layout.SetText(s);
93 layout.FontDescription = FontDescription.FromString("Sans 12"); // Specify your desired font here
94 // Get the size of the layout
95 int width, height;
96 layout.GetPixelSize(out width, out height);
97 return width;
98 }
99 }
100
101 bool skip = false;
102 string chs = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
103 private void Buffer_Changed(object sender, EventArgs e)
104 {
105 if (consoleBox.Buffer.CharCount == 0)
106 return;
107 if(skip)
108 {
109 skip = false;
110 return;
111 }
112 pred = pred.Replace("\t", "").Replace("\r","").Replace("\n","");
113 if (pred.Contains(' '))
114 pred = consoleBox.Buffer.Text.Remove(0,consoleBox.Buffer.Text.LastIndexOf(' '));
115 else
116 pred = consoleBox.Buffer.Text.Replace("\t", "").Replace("\r", "").Replace("\n", "");
117 bool ends = false;
118 char ch = ' ';
119 int i = 0;
120 int ind = -1;
121 foreach (Char cs in pred)
122 {
123 ends = false;
124 foreach (Char c in chs)
125 {
126 if (cs == c)
127 {
128 ends = true;
129 ch = c;
130 }
131 }
132 if (!ends)
133 ind = i;
134 i++;
135 }
136 if (ind!=-1)
137 pred = pred.Remove(0, ind+1);
138
139 preds.Clear();
140 i = 0;
141 ind = -1;
142 foreach(var cs in Fiji.Macro.Commands)
143 {
144 if (cs.Value.Name.StartsWith(pred))
145 preds.Add(cs.Value.Name);
146 }
147 foreach (var cs in Fiji.Macro.Functions)
148 {
149 if (cs.Value[0].Name.StartsWith(pred))
150 preds.Add(cs.Value[0].Name);
151 }
152 predLabel.Text = "";
153 foreach(var item in preds)
154 {
155 if (this.AllocatedWidth > Measure(predLabel.Text))
156 {
157 predLabel.Text += item + ", ";
158 }
159 else break;
160 }
161 if(consoleBox.Buffer.Text.EndsWith('\t'))
162 {
163 string s = consoleBox.Buffer.Text.TrimEnd('\t');
164 skip = true;
165 consoleBox.Buffer.Text = s.Remove(s.Length - pred.Length, pred.Length) + preds[0];
166 }
167 }
168
174 private void ResultsBox_Clicked(object sender, EventArgs e)
175 {
176 resultInNewTab = resultsBox.Active;
177 }
178
186 private void Console_KeyPressEvent(object o, KeyPressEventArgs args)
187 {
188 if (args.Event.Key == Gdk.Key.w)
189 {
190 line++;
191 string[] s = consoleBox.Buffer.Text.Split(Environment.NewLine);
192 textBox.Buffer.Text = s[s.Length - 1 - line];
193 }
194 if (args.Event.Key == Gdk.Key.s)
195 {
196 line--;
197 string[] s = consoleBox.Buffer.Text.Split(Environment.NewLine);
198 textBox.Buffer.Text = s[s.Length - 1 - line];
199 }
200 }
201
206 private void TabRadioBox_Clicked(object sender, EventArgs e)
207 {
208 onTab = tabRadioBut.Active;
209 selRadioBut.Active = !onTab;
210 }
211
216 private void SelRadioBox_Clicked(object sender, EventArgs e)
217 {
218 onTab = !selRadioBut.Active;
219 tabRadioBut.Active = onTab;
220 }
221 private void HeadlessBox_Clicked(object sender, EventArgs e)
222 {
223 headless = headlessBox.Active;
224 }
225
233 private void ImagejBut_Clicked(object sender, EventArgs e)
234 {
235 if (ImageView.SelectedImage == null)
236 return;
237 Fiji.RunOnImage(ImageView.SelectedImage, consoleBox.Buffer.Text, headless, onTab, useBioformats, resultsBox.Active);
238 textBox.Buffer.Text += textBox.Buffer.Text + Environment.NewLine;
239 consoleBox.Buffer.Text = "";
240 }
241
246 private void RunBut_Clicked(object sender, EventArgs e)
247 {
248 object o = Scripting.Script.RunString(consoleBox.Buffer.Text);
249 textBox.Buffer.Text += textBox.Buffer.Text + Environment.NewLine + o.ToString() + Environment.NewLine;
250 consoleBox.Buffer.Text = "";
251 }
252
253 #endregion
254
255
256 }
257}
BioConsole(Builder builder, IntPtr handle)
Specialised constructor for use only by derived class.
Definition BioConsole.cs:61
static BioConsole Create()
Definition BioConsole.cs:52
void SetupHandlers()
Sets up the handlers.
Definition BioConsole.cs:74
static object RunString(string st)
Definition Scripting.cs:278
Definition ML.cs:17