Ejemplo n.º 1
0
        /// <summary>
        /// Render RadioGroup control.
        /// Return true if failed.
        /// </summary>
        /// <param name="r.Canvas">Parent r.Canvas</param>
        /// <param name="uiCmd">UICommand</param>
        /// <returns>Success = false, Failure = true</returns>
        public static void RenderRadioButton(RenderInfo r, UICommand uiCmd, UICommand[] radioButtons)
        {
            Debug.Assert(uiCmd.Info.GetType() == typeof(UIInfo_RadioButton));
            UIInfo_RadioButton info = uiCmd.Info as UIInfo_RadioButton;

            double fontSize = CalcFontPointScale();

            RadioButton radio = new RadioButton()
            {
                GroupName = r.Plugin.FullPath,
                Content   = uiCmd.Text,
                FontSize  = fontSize,
                IsChecked = info.Selected,
                VerticalContentAlignment = VerticalAlignment.Center,
            };

            if (info.SectionName != null)
            {
                radio.Click += (object sender, RoutedEventArgs e) =>
                {
                    if (r.Plugin.Sections.ContainsKey(info.SectionName)) // Only if section exists
                    {
                        SectionAddress addr = new SectionAddress(r.Plugin, r.Plugin.Sections[info.SectionName]);
                        UIRenderer.RunOneSection(addr, $"{r.Plugin.Title} - RadioButton [{uiCmd.Key}]", info.HideProgress);
                    }
                    else
                    {
                        Application.Current.Dispatcher.Invoke((Action)(() =>
                        {
                            MainWindow w = Application.Current.MainWindow as MainWindow;
                            w.Logger.System_Write(new LogInfo(LogState.Error, $"Section [{info.SectionName}] does not exists"));
                        }));
                    }
                };
            }

            radio.Checked += (object sender, RoutedEventArgs e) =>
            {
                RadioButton btn = sender as RadioButton;
                info.Selected = true;

                // Uncheck the other RadioButtons
                List <UICommand> updateList = radioButtons.Where(x => !x.Key.Equals(uiCmd.Key, StringComparison.Ordinal)).ToList();
                foreach (UICommand uncheck in updateList)
                {
                    Debug.Assert(uncheck.Info.GetType() == typeof(UIInfo_RadioButton));
                    UIInfo_RadioButton unInfo = uncheck.Info as UIInfo_RadioButton;

                    unInfo.Selected = false;
                }

                updateList.Add(uiCmd);
                UICommand.Update(updateList);
            };

            SetToolTip(radio, info.ToolTip);
            DrawToCanvas(r, radio, uiCmd.Rect);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Render CheckBox control.
        /// Return true if failed.
        /// </summary>
        /// <param name="r.Canvas">Parent r.Canvas</param>
        /// <param name="uiCmd">UICommand</param>
        /// <returns>Success = false, Failure = true</returns>
        public static void RenderCheckBox(RenderInfo r, UICommand uiCmd)
        {
            Debug.Assert(uiCmd.Info.GetType() == typeof(UIInfo_CheckBox));
            UIInfo_CheckBox info = uiCmd.Info as UIInfo_CheckBox;

            CheckBox checkBox = new CheckBox()
            {
                Content   = uiCmd.Text,
                IsChecked = info.Value,
                FontSize  = CalcFontPointScale(),
                VerticalContentAlignment = VerticalAlignment.Center,
            };

            if (info.SectionName != null)
            {
                checkBox.Click += (object sender, RoutedEventArgs e) =>
                {
                    if (r.Plugin.Sections.ContainsKey(info.SectionName)) // Only if section exists
                    {
                        SectionAddress addr = new SectionAddress(r.Plugin, r.Plugin.Sections[info.SectionName]);
                        UIRenderer.RunOneSection(addr, $"{r.Plugin.Title} - CheckBox [{uiCmd.Key}]", info.HideProgress);
                    }
                    else
                    {
                        r.Logger.System_Write(new LogInfo(LogState.Error, $"Section [{info.SectionName}] does not exists"));
                    }
                };
            }

            checkBox.Checked += (object sender, RoutedEventArgs e) =>
            {
                CheckBox box = sender as CheckBox;
                info.Value = true;
                uiCmd.Update();
            };
            checkBox.Unchecked += (object sender, RoutedEventArgs e) =>
            {
                CheckBox box = sender as CheckBox;
                info.Value = false;
                uiCmd.Update();
            };

            SetToolTip(checkBox, info.ToolTip);
            DrawToCanvas(r, checkBox, uiCmd.Rect);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Render ComboBox control.
        /// Return true if failed.
        /// </summary>
        /// <param name="r.Canvas">Parent r.Canvas</param>
        /// <param name="uiCmd">UICommand</param>
        /// <returns>Success = false, Failure = true</returns>
        public static void RenderComboBox(RenderInfo r, UICommand uiCmd)
        {
            Debug.Assert(uiCmd.Info.GetType() == typeof(UIInfo_ComboBox));
            UIInfo_ComboBox info = uiCmd.Info as UIInfo_ComboBox;

            ComboBox comboBox = new ComboBox()
            {
                FontSize                 = CalcFontPointScale(),
                ItemsSource              = info.Items,
                SelectedIndex            = info.Index,
                VerticalContentAlignment = VerticalAlignment.Center,
            };

            comboBox.LostFocus += (object sender, RoutedEventArgs e) =>
            {
                ComboBox box = sender as ComboBox;
                if (info.Index != box.SelectedIndex)
                {
                    info.Index = box.SelectedIndex;
                    uiCmd.Text = info.Items[box.SelectedIndex];
                    uiCmd.Update();
                }
            };

            if (info.SectionName != null)
            {
                comboBox.SelectionChanged += (object sender, SelectionChangedEventArgs e) =>
                {
                    if (r.Plugin.Sections.ContainsKey(info.SectionName)) // Only if section exists
                    {
                        SectionAddress addr = new SectionAddress(r.Plugin, r.Plugin.Sections[info.SectionName]);
                        UIRenderer.RunOneSection(addr, $"{r.Plugin.Title} - CheckBox [{uiCmd.Key}]", info.HideProgress);
                    }
                    else
                    {
                        r.Logger.System_Write(new LogInfo(LogState.Error, $"Section [{info.SectionName}] does not exists"));
                    }
                };
            }

            SetToolTip(comboBox, info.ToolTip);
            DrawToCanvas(r, comboBox, uiCmd.Rect);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Render WebLabel control.
        /// Return true if failed.
        /// </summary>
        /// <param name="r.Canvas">Parent r.Canvas</param>
        /// <param name="uiCtrl">UIControl</param>
        /// <returns>Success = false, Failure = true</returns>
        public static void RenderWebLabel(RenderInfo r, UIControl uiCtrl)
        {
            Debug.Assert(uiCtrl.Info.GetType() == typeof(UIInfo_WebLabel));
            UIInfo_WebLabel info = uiCtrl.Info as UIInfo_WebLabel;

            TextBlock block = new TextBlock()
            {
                TextWrapping = TextWrapping.Wrap,
                FontSize     = CalcFontPointScale(),
            };

            Hyperlink hyperLink = new Hyperlink()
            {
                NavigateUri = new Uri(info.URL),
            };

            hyperLink.Inlines.Add(uiCtrl.Text);
            hyperLink.RequestNavigate += (object sender, RequestNavigateEventArgs e) =>
            {
                Process.Start(e.Uri.ToString());
            };
            block.Inlines.Add(hyperLink);

            string toolTip = UIRenderer.AppendUrlToToolTip(info.ToolTip, info.URL);

            SetToolTip(block, toolTip);

            if (IgnoreWidthOfWebLabel)
            {
                Rect rect = uiCtrl.Rect;
                rect.Width = block.Width;
                DrawToCanvas(r, block, rect);
            }
            else
            {
                DrawToCanvas(r, block, uiCtrl.Rect);
            }
        }
Ejemplo n.º 5
0
        public void Render()
        {
            if (uiCodes == null) // This plugin does not have 'Interface' section
            {
                return;
            }

            InitCanvas(renderInfo.Canvas);
            UICommand[] radioButtons = uiCodes.Where(x => x.Type == UIType.RadioButton).ToArray();
            foreach (UICommand uiCmd in uiCodes)
            {
                try
                {
                    switch (uiCmd.Type)
                    {
                    case UIType.TextBox:
                        UIRenderer.RenderTextBox(renderInfo, uiCmd);
                        break;

                    case UIType.TextLabel:
                        UIRenderer.RenderTextLabel(renderInfo, uiCmd);
                        break;

                    case UIType.NumberBox:
                        UIRenderer.RenderNumberBox(renderInfo, uiCmd);
                        break;

                    case UIType.CheckBox:
                        UIRenderer.RenderCheckBox(renderInfo, uiCmd);
                        break;

                    case UIType.ComboBox:
                        UIRenderer.RenderComboBox(renderInfo, uiCmd);
                        break;

                    case UIType.Image:
                        UIRenderer.RenderImage(renderInfo, uiCmd);
                        break;

                    case UIType.TextFile:
                        UIRenderer.RenderTextFile(renderInfo, uiCmd);
                        break;

                    case UIType.Button:
                        UIRenderer.RenderButton(renderInfo, uiCmd, logger);
                        break;

                    case UIType.WebLabel:
                        UIRenderer.RenderWebLabel(renderInfo, uiCmd);
                        break;

                    case UIType.RadioButton:
                        UIRenderer.RenderRadioButton(renderInfo, uiCmd, radioButtons);
                        break;

                    case UIType.Bevel:
                        UIRenderer.RenderBevel(renderInfo, uiCmd);
                        break;

                    case UIType.FileBox:
                        UIRenderer.RenderFileBox(renderInfo, uiCmd, variables);
                        break;

                    case UIType.RadioGroup:
                        UIRenderer.RenderRadioGroup(renderInfo, uiCmd);
                        break;

                    default:
                        logger.System_Write(new LogInfo(LogState.Error, $"Unable to render [{uiCmd.RawLine}]"));
                        break;
                    }
                }
                catch (Exception e)
                { // Log failure
                    logger.System_Write(new LogInfo(LogState.Error, $"{Logger.LogExceptionMessage(e)} [{uiCmd.RawLine}]"));
                }
            }

            return;
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Render RadioGroup control.
        /// Return true if failed.
        /// </summary>
        /// <param name="r.Canvas">Parent r.Canvas</param>
        /// <param name="uiCmd">UICommand</param>
        /// <returns>Success = false, Failure = true</returns>
        public static void RenderRadioGroup(RenderInfo r, UICommand uiCmd)
        {
            Debug.Assert(uiCmd.Info.GetType() == typeof(UIInfo_RadioGroup));
            UIInfo_RadioGroup info = uiCmd.Info as UIInfo_RadioGroup;

            double fontSize = CalcFontPointScale();

            GroupBox box = new GroupBox()
            {
                Header      = uiCmd.Text,
                FontSize    = fontSize,
                BorderBrush = Brushes.LightGray,
            };

            SetToolTip(box, info.ToolTip);

            Grid grid = new Grid();

            box.Content = grid;

            for (int i = 0; i < info.Items.Count; i++)
            {
                RadioButton radio = new RadioButton()
                {
                    GroupName = r.Plugin.FullPath + uiCmd.Key,
                    Content   = info.Items[i],
                    Tag       = i,
                    FontSize  = fontSize,
                    VerticalContentAlignment = VerticalAlignment.Center,
                };

                radio.IsChecked = (i == info.Selected);
                radio.Checked  += (object sender, RoutedEventArgs e) =>
                {
                    RadioButton btn = sender as RadioButton;
                    info.Selected = (int)btn.Tag;
                    uiCmd.Update();
                };

                if (info.SectionName != null)
                {
                    radio.Click += (object sender, RoutedEventArgs e) =>
                    {
                        if (r.Plugin.Sections.ContainsKey(info.SectionName)) // Only if section exists
                        {
                            SectionAddress addr = new SectionAddress(r.Plugin, r.Plugin.Sections[info.SectionName]);
                            UIRenderer.RunOneSection(addr, $"{r.Plugin.Title} - RadioGroup [{uiCmd.Key}]", info.HideProgress);
                        }
                        else
                        {
                            Application.Current.Dispatcher.Invoke(() =>
                            {
                                MainWindow w = Application.Current.MainWindow as MainWindow;
                                w.Logger.System_Write(new LogInfo(LogState.Error, $"Section [{info.SectionName}] does not exists"));
                            });
                        }
                    };
                }

                SetToolTip(radio, info.ToolTip);

                Grid.SetRow(radio, i);
                grid.RowDefinitions.Add(new RowDefinition());
                grid.Children.Add(radio);
            }

            Rect rect = new Rect(uiCmd.Rect.Left, uiCmd.Rect.Top, uiCmd.Rect.Width, uiCmd.Rect.Height);

            DrawToCanvas(r, box, rect);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Render Button control.
        /// Return true if failed.
        /// </summary>
        /// <param name="r.Canvas">Parent r.Canvas</param>
        /// <param name="uiCmd">UICommand</param>
        /// <returns>Success = false, Failure = true</returns>
        public static void RenderButton(RenderInfo r, UICommand uiCmd, Logger logger)
        {
            Debug.Assert(uiCmd.Info.GetType() == typeof(UIInfo_Button));
            UIInfo_Button info = uiCmd.Info as UIInfo_Button;

            Button button = new Button()
            {
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment   = VerticalAlignment.Center,
            };

            button.Click += (object sender, RoutedEventArgs e) =>
            {
                if (r.Plugin.Sections.ContainsKey(info.SectionName)) // Only if section exists
                {
                    SectionAddress addr = new SectionAddress(r.Plugin, r.Plugin.Sections[info.SectionName]);
                    UIRenderer.RunOneSection(addr, $"{r.Plugin.Title} - Button [{uiCmd.Key}]", info.ShowProgress);
                }
                else
                {
                    Application.Current.Dispatcher.Invoke((Action)(() =>
                    {
                        MainWindow w = Application.Current.MainWindow as MainWindow;
                        w.Logger.System_Write(new LogInfo(LogState.Error, $"Section [{info.SectionName}] does not exists"));
                    }));
                }
            };

            if (info.Picture != null && uiCmd.Addr.Plugin.Sections.ContainsKey($"EncodedFile-InterfaceEncoded-{info.Picture}"))
            { // Has Picture
                if (ImageHelper.GetImageType(info.Picture, out ImageHelper.ImageType type))
                {
                    return;
                }

                Image image = new Image()
                {
                    StretchDirection  = StretchDirection.DownOnly,
                    Stretch           = Stretch.Uniform,
                    UseLayoutRounding = true,
                };
                RenderOptions.SetBitmapScalingMode(image, BitmapScalingMode.HighQuality);

                using (MemoryStream ms = EncodedFile.ExtractInterfaceEncoded(uiCmd.Addr.Plugin, info.Picture))
                {
                    if (type == ImageHelper.ImageType.Svg)
                    {
                        ImageHelper.GetSvgSize(ms, out double width, out double height);
                        image.Source = ImageHelper.SvgToBitmapImage(ms, width, height);
                    }
                    else
                    {
                        image.Source = ImageHelper.ImageToBitmapImage(ms);
                    }
                }

                if (uiCmd.Text.Equals(string.Empty, StringComparison.Ordinal))
                { // No text, just image
                    button.Content = image;
                }
                else
                { // Button has text
                    StackPanel panel = new StackPanel()
                    {
                        HorizontalAlignment = HorizontalAlignment.Center,
                        VerticalAlignment   = VerticalAlignment.Center,
                        Orientation         = Orientation.Horizontal,
                    };

                    TextBlock text = new TextBlock()
                    {
                        Text              = uiCmd.Text,
                        FontSize          = CalcFontPointScale(),
                        Height            = double.NaN,
                        VerticalAlignment = VerticalAlignment.Center,
                        Margin            = new Thickness(CalcFontPointScale() / 2, 0, 0, 0),
                    };

                    panel.Children.Add(image);
                    panel.Children.Add(text);
                    button.Content = panel;
                }
            }
            else
            { // No picture
                button.Content  = uiCmd.Text;
                button.FontSize = CalcFontPointScale();
            }

            SetToolTip(button, info.ToolTip);
            DrawToCanvas(r, button, uiCmd.Rect);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Render Image control.
        /// Return true if failed.
        /// </summary>
        /// <param name="r.Canvas">Parent r.Canvas</param>
        /// <param name="uiCtrl">UIControl</param>
        /// <returns>Success = false, Failure = true</returns>
        public static void RenderImage(RenderInfo r, UIControl uiCtrl)
        {
            Debug.Assert(uiCtrl.Info.GetType() == typeof(UIInfo_Image));
            UIInfo_Image info = uiCtrl.Info as UIInfo_Image;

            Image image = new Image()
            {
                StretchDirection  = StretchDirection.DownOnly,
                Stretch           = Stretch.Uniform,
                UseLayoutRounding = true,
            };

            RenderOptions.SetBitmapScalingMode(image, BitmapScalingMode.HighQuality);
            Button button;

            // double width, height;
            using (MemoryStream ms = EncodedFile.ExtractInterfaceEncoded(uiCtrl.Addr.Script, uiCtrl.Text))
            {
                if (ImageHelper.GetImageType(uiCtrl.Text, out ImageHelper.ImageType type))
                {
                    return;
                }

                button = new Button()
                {
                    Style = (Style)r.Window.FindResource("ImageButton")
                };

                if (type == ImageHelper.ImageType.Svg)
                {
                    double width  = uiCtrl.Rect.Width * r.MasterScale;
                    double height = uiCtrl.Rect.Height * r.MasterScale;
                    button.Background = ImageHelper.SvgToImageBrush(ms, width, height);
                }
                else
                {
                    button.Background = ImageHelper.ImageToImageBrush(ms);
                }
            }

            bool hasUrl = false;

            if (string.IsNullOrEmpty(info.URL) == false)
            {
                if (Uri.TryCreate(info.URL, UriKind.Absolute, out Uri _)) // Success
                {
                    hasUrl = true;
                }
                else // Failure
                {
                    throw new InvalidUIControlException($"Invalid URL [{info.URL}]", uiCtrl);
                }
            }

            string toolTip = info.ToolTip;

            if (hasUrl)
            { // Open URL
                button.Click += (object sender, RoutedEventArgs e) =>
                {
                    Process.Start(info.URL);
                };

                toolTip = UIRenderer.AppendUrlToToolTip(info.ToolTip, info.URL);
            }
            else
            { // Open picture with external viewer
                button.Click += (object sender, RoutedEventArgs e) =>
                {
                    if (ImageHelper.GetImageType(uiCtrl.Text, out ImageHelper.ImageType t))
                    {
                        return;
                    }
                    string path = System.IO.Path.ChangeExtension(System.IO.Path.GetTempFileName(), "." + t.ToString().ToLower());

                    using (MemoryStream ms = EncodedFile.ExtractInterfaceEncoded(uiCtrl.Addr.Script, uiCtrl.Text))
                        using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
                        {
                            ms.Position = 0;
                            ms.CopyTo(fs);
                        }

                    ProcessStartInfo procInfo = new ProcessStartInfo()
                    {
                        Verb            = "open",
                        FileName        = path,
                        UseShellExecute = true
                    };
                    Process.Start(procInfo);
                };
            }

            SetToolTip(button, toolTip);
            // SetToolTip(button, info.ToolTip);
            DrawToCanvas(r, button, uiCtrl.Rect);
        }