Exemple #1
0
        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;

                    this.Dispatcher.Invoke(() =>
                    {
                        Graph G = Ans as Graph;
                        SKImage Img;
                        object Obj;

                        if (G != null)
                        {
                            GraphSettings Settings = new GraphSettings();
                            Tuple <int, int> Size;
                            double d;

                            if ((Size = G.RecommendedBitmapSize) != null)
                            {
                                Settings.Width  = Size.Item1;
                                Settings.Height = Size.Item2;

                                Settings.MarginLeft  = (int)Math.Round(15.0 * Settings.Width / 640);
                                Settings.MarginRight = Settings.MarginLeft;

                                Settings.MarginTop     = (int)Math.Round(15.0 * Settings.Height / 480);
                                Settings.MarginBottom  = Settings.MarginTop;
                                Settings.LabelFontSize = 12.0 * Settings.Height / 480;
                            }
                            else
                            {
                                if (this.variables.TryGetVariable("GraphWidth", out Variable v) && (Obj = v.ValueObject) is double && (d = (double)Obj) >= 1)
                                {
                                    Settings.Width       = (int)Math.Round(d);
                                    Settings.MarginLeft  = (int)Math.Round(15 * d / 640);
                                    Settings.MarginRight = Settings.MarginLeft;
                                }
                                else if (!this.variables.ContainsVariable("GraphWidth"))
                                {
                                    this.variables["GraphWidth"] = (double)Settings.Width;
                                }

                                if (this.variables.TryGetVariable("GraphHeight", out v) && (Obj = v.ValueObject) is double && (d = (double)Obj) >= 1)
                                {
                                    Settings.Height        = (int)Math.Round(d);
                                    Settings.MarginTop     = (int)Math.Round(15 * d / 480);
                                    Settings.MarginBottom  = Settings.MarginTop;
                                    Settings.LabelFontSize = 12 * d / 480;
                                }
                                else if (!this.variables.ContainsVariable("GraphHeight"))
                                {
                                    this.variables["GraphHeight"] = (double)Settings.Height;
                                }
                            }

                            using (SKImage Bmp = G.CreateBitmap(Settings, out object[] States))
                            {
                                this.AddImageBlock(ScriptBlock, Bmp);
                            }
                        }
                        else if ((Img = Ans.AssociatedObjectValue as SKImage) != null)
                        {
                            this.AddImageBlock(ScriptBlock, Img);
                        }
                        else if (Ans.AssociatedObjectValue is Exception ex)
                        {
                            AggregateException ex2;

                            ex = Log.UnnestException(ex);

                            if ((ex2 = ex as AggregateException) != null)
                            {
                                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
                        {
                            this.AddTextBlock(ScriptBlock, Ans.ToString(), Colors.Red, FontWeights.Normal, true);
                        }
                    });
                }
        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;

                    this.Dispatcher.BeginInvoke(new ThreadStart(() =>
                    {
                        try
                        {
                            SKImage Img;

                            if (Ans is Graph G)
                            {
                                using (SKImage Bmp = G.CreateBitmap(this.variables, out object[] States))
                                {
                                    this.AddImageBlock(ScriptBlock, Bmp, G, States);
                                }
                            }
                            else if ((Img = Ans.AssociatedObjectValue as SKImage) != null)
                            {
                                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
                            {
                                this.AddTextBlock(ScriptBlock, Ans.ToString(), Colors.Red, FontWeights.Normal, true);
                            }
                        }
                        catch (Exception ex)
                        {
                            ex  = Log.UnnestException(ex);
                            Ans = new ObjectValue(ex);
                            this.variables["Ans"] = Ans;

                            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);
                            }
                        }
                    }));
        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;

                    this.Dispatcher.BeginInvoke(new ThreadStart(() =>
                    {
                        try
                        {
                            SKImage Img;

                            if (Ans is Graph G)
                            {
                                using (SKImage Bmp = G.CreateBitmap(this.variables, out object[] States))
                                {
                                    this.AddImageBlock(ScriptBlock, Bmp, G, States);
                                }
                            }
                            else if ((Img = Ans.AssociatedObjectValue as SKImage) != null)
                            {
                                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());
                                XamlSettings Settings = new XamlSettings()
                                {
                                    TableCellRowBackgroundColor1 = "#20404040",
                                    TableCellRowBackgroundColor2 = "#10808080"
                                };

                                string XAML = Doc.GenerateXAML(Settings);

                                if (XamlReader.Parse(XAML) is UIElement Parsed)
                                {
                                    this.AddBlock(ScriptBlock, Parsed);
                                }
                            }
Exemple #4
0
        private void ExecuteButton_Click(object sender, RoutedEventArgs e)
        {
            Waher.Script.Expression Exp;
            TextBlock ScriptBlock;
            UIElement ResultBlock = null;

            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;

                    Exp.OnPreview += (sender2, e2) =>
                    {
                        this.Dispatcher.Invoke(() =>
                                               ResultBlock = this.ShowResult(ResultBlock, e2.Preview, ScriptBlock));
                    };

                    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(() =>
                    {
                        ResultBlock = this.ShowResult(ResultBlock, Ans, ScriptBlock);
                    });
                }
                catch (Exception ex)
                {
                    ex = Log.UnnestException(ex);
                    MainWindow.MessageBox(ex.Message, "Unable to parse script.", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            });
        }