private void ExecuteButton_Click(object sender, RoutedEventArgs e) { Waher.Script.Expression Exp; TextBlock ScriptBlock; try { Exp = new Waher.Script.Expression(this.Input.Text); ScriptBlock = new TextBlock() { Text = this.Input.Text, FontFamily = new FontFamily("Courier New"), TextWrapping = TextWrapping.Wrap, Tag = Exp }; ScriptBlock.PreviewMouseDown += TextBlock_PreviewMouseDown; this.HistoryPanel.Children.Add(ScriptBlock); this.HistoryScrollViewer.ScrollToBottom(); this.Input.Text = string.Empty; this.Input.Focus(); } catch (Exception ex) { ex = Log.UnnestException(ex); MessageBox.Show(MainWindow.currentInstance, ex.Message, "Unable to parse script.", MessageBoxButton.OK, MessageBoxImage.Error); return; } Task.Run(() => { try { IElement Ans; try { Ans = Exp.Root.Evaluate(this.variables); } catch (ScriptReturnValueException ex) { Ans = ex.ReturnValue; } catch (Exception ex) { Ans = new ObjectValue(ex); } this.variables["Ans"] = Ans; MainWindow.UpdateGui(() => { try { if (Ans is Graph G) { using (SKImage Bmp = G.CreateBitmap(this.variables, out object[] States)) { this.AddImageBlock(ScriptBlock, Bmp, G, States); } } else if (Ans.AssociatedObjectValue is SKImage Img) { this.AddImageBlock(ScriptBlock, Img, null, null); } else if (Ans.AssociatedObjectValue is Exception ex) { ex = Log.UnnestException(ex); if (ex is AggregateException ex2) { foreach (Exception ex3 in ex2.InnerExceptions) { ScriptBlock = this.AddTextBlock(ScriptBlock, ex3.Message, Colors.Red, FontWeights.Bold, ex3); } } else { this.AddTextBlock(ScriptBlock, ex.Message, Colors.Red, FontWeights.Bold, ex); } } else if (Ans.AssociatedObjectValue is ObjectMatrix M && M.ColumnNames != null) { StringBuilder Markdown = new StringBuilder(); foreach (string s2 in M.ColumnNames) { Markdown.Append("| "); Markdown.Append(MarkdownDocument.Encode(s2)); } Markdown.AppendLine(" |"); foreach (string s2 in M.ColumnNames) { Markdown.Append("|---"); } Markdown.AppendLine("|"); int x, y; for (y = 0; y < M.Rows; y++) { for (x = 0; x < M.Columns; x++) { Markdown.Append("| "); object Item = M.GetElement(x, y).AssociatedObjectValue; if (Item != null) { if (!(Item is string s2)) { s2 = Waher.Script.Expression.ToString(Item); } s2 = s2.Replace("\r\n", "\n").Replace("\r", "\n").Replace("\n", "<br/>"); Markdown.Append(MarkdownDocument.Encode(s2)); } } Markdown.AppendLine(" |"); } MarkdownDocument Doc = new MarkdownDocument(Markdown.ToString(), ChatView.GetMarkdownSettings()); string XAML = Doc.GenerateXAML(); if (XamlReader.Parse(XAML) is UIElement Parsed) { this.AddBlock(ScriptBlock, Parsed); } }
public void Load(string FileName) { try { XmlDocument Xml = new XmlDocument(); Xml.Load(FileName); switch (Xml.DocumentElement.LocalName) { case "ClientConnections": this.connections.Load(FileName, Xml); this.FileName = FileName; this.ConnectionTree.Items.Clear(); foreach (TreeNode Node in this.connections.RootNodes) { this.AddNode(Node); } break; case "Sniff": TabItem TabItem = MainWindow.NewTab(Path.GetFileName(FileName)); this.MainWindow.Tabs.Items.Add(TabItem); SnifferView SnifferView = new SnifferView(null); TabItem.Content = SnifferView; SnifferView.Sniffer = new TabSniffer(TabItem, SnifferView); this.MainWindow.Tabs.SelectedItem = TabItem; SnifferView.Load(Xml, FileName); break; case "Chat": TabItem = MainWindow.NewTab(Path.GetFileName(FileName)); this.MainWindow.Tabs.Items.Add(TabItem); ChatView ChatView = new ChatView(null); ChatView.Input.IsEnabled = false; ChatView.SendButton.IsEnabled = false; TabItem.Content = ChatView; this.MainWindow.Tabs.SelectedItem = TabItem; ChatView.Load(Xml, FileName); break; case "SensorData": TabItem = MainWindow.NewTab(Path.GetFileName(FileName)); this.MainWindow.Tabs.Items.Add(TabItem); SensorDataView SensorDataView = new SensorDataView(null, null, false); TabItem.Content = SensorDataView; this.MainWindow.Tabs.SelectedItem = TabItem; SensorDataView.Load(Xml, FileName); break; case "SearchResult": TabItem = MainWindow.NewTab(Path.GetFileName(FileName)); this.MainWindow.Tabs.Items.Add(TabItem); SearchResultView SearchResultView = new SearchResultView(); TabItem.Content = SearchResultView; this.MainWindow.Tabs.SelectedItem = TabItem; SearchResultView.Load(Xml, FileName); break; case "Script": TabItem = MainWindow.NewTab(Path.GetFileName(FileName)); this.MainWindow.Tabs.Items.Add(TabItem); ScriptView ScriptView = new ScriptView(); TabItem.Content = ScriptView; this.MainWindow.Tabs.SelectedItem = TabItem; ScriptView.Load(Xml, FileName); break; case "EventOutput": TabItem = MainWindow.NewTab(Path.GetFileName(FileName)); this.MainWindow.Tabs.Items.Add(TabItem); LogView LogView = new LogView(false); TabItem.Content = LogView; this.MainWindow.Tabs.SelectedItem = TabItem; LogView.Load(Xml, FileName); break; default: throw new Exception("Unrecognized file format."); } } catch (Exception ex) { ex = Log.UnnestException(ex); MessageBox.Show(ex.Message, ex.Message, MessageBoxButton.OK, MessageBoxImage.Error); } }