/// <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); } }
/// <summary> /// Render TextBox control. /// Return true if failed. /// </summary> /// <param name="canvas">Parent canvas</param> /// <param name="uiCmd">UICommand</param> /// <returns>Success = false, Failure = true</returns> public static void RenderTextBox(RenderInfo r, UICommand uiCmd) { // WB082 textbox control's y coord is of textbox's, not textlabel's. Debug.Assert(uiCmd.Info.GetType() == typeof(UIInfo_TextBox)); UIInfo_TextBox info = uiCmd.Info as UIInfo_TextBox; TextBox box = new TextBox() { Text = info.Value, FontSize = CalcFontPointScale(), VerticalContentAlignment = VerticalAlignment.Center, }; box.LostFocus += (object sender, RoutedEventArgs e) => { TextBox tBox = sender as TextBox; info.Value = tBox.Text; uiCmd.Update(); }; SetToolTip(box, info.ToolTip); DrawToCanvas(r, box, uiCmd.Rect); if (uiCmd.Text.Equals(string.Empty, StringComparison.Ordinal) == false) { TextBlock block = new TextBlock() { Text = uiCmd.Text, LineStackingStrategy = LineStackingStrategy.BlockLineHeight, LineHeight = CalcFontPointScale(), FontSize = CalcFontPointScale(), }; SetToolTip(block, info.ToolTip); double margin = PointToDeviceIndependentPixel * DefaultFontPoint * 1.2; Rect blockRect = new Rect(uiCmd.Rect.Left, uiCmd.Rect.Top - margin, uiCmd.Rect.Width, uiCmd.Rect.Height); DrawToCanvas(r, block, blockRect); } }
/// <summary> /// Render TextLabel 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 RenderTextLabel(RenderInfo r, UICommand uiCmd) { Debug.Assert(uiCmd.Info.GetType() == typeof(UIInfo_TextLabel)); UIInfo_TextLabel info = uiCmd.Info as UIInfo_TextLabel; TextBlock block = new TextBlock() { Text = uiCmd.Text, TextWrapping = TextWrapping.Wrap, LineStackingStrategy = LineStackingStrategy.BlockLineHeight, LineHeight = CalcFontPointScale(info.FontSize), FontSize = CalcFontPointScale(info.FontSize), }; switch (info.Style) { case UIInfo_TextLabel_Style.Bold: block.FontWeight = FontWeights.Bold; break; case UIInfo_TextLabel_Style.Italic: block.FontStyle = FontStyles.Italic; break; case UIInfo_TextLabel_Style.Underline: block.TextDecorations = TextDecorations.Underline; break; case UIInfo_TextLabel_Style.Strike: block.TextDecorations = TextDecorations.Strikethrough; break; } SetToolTip(block, info.ToolTip); DrawToCanvas(r, block, uiCmd.Rect); }
/// <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); }
/// <summary> /// Render FileBox control. /// Return true if failed. /// </summary> /// <param name="canvas">Parent canvas</param> /// <param name="uiCmd">UICommand</param> public static void RenderFileBox(RenderInfo r, UICommand uiCmd, Variables variables) { // It took time to find WB082 textbox control's y coord is of textbox's, not textlabel's. Debug.Assert(uiCmd.Info.GetType() == typeof(UIInfo_FileBox)); UIInfo_FileBox info = uiCmd.Info as UIInfo_FileBox; TextBox box = new TextBox() { Text = uiCmd.Text, FontSize = CalcFontPointScale(), VerticalContentAlignment = VerticalAlignment.Center, }; box.TextChanged += (object sender, TextChangedEventArgs e) => { TextBox tBox = sender as TextBox; uiCmd.Text = tBox.Text; uiCmd.Update(); }; SetToolTip(box, info.ToolTip); Button button = new Button() { FontSize = CalcFontPointScale(), Content = ImageHelper.GetMaterialIcon(PackIconMaterialKind.FolderUpload, 0), }; SetToolTip(button, info.ToolTip); button.Click += (object sender, RoutedEventArgs e) => { Button bt = sender as Button; if (info.IsFile) { // File string currentPath = StringEscaper.Preprocess(variables, uiCmd.Text); if (File.Exists(currentPath)) { currentPath = Path.GetDirectoryName(currentPath); } else { currentPath = string.Empty; } Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog() { Filter = "All Files|*.*", InitialDirectory = currentPath, }; if (dialog.ShowDialog() == true) { box.Text = dialog.FileName; } } else { // Directory string currentPath = StringEscaper.Preprocess(variables, uiCmd.Text); if (Directory.Exists(currentPath) == false) { currentPath = string.Empty; } System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog() { ShowNewFolderButton = true, SelectedPath = currentPath, }; System.Windows.Forms.DialogResult result = dialog.ShowDialog(); if (result == System.Windows.Forms.DialogResult.OK) { box.Text = dialog.SelectedPath; if (Directory.Exists(dialog.SelectedPath) && !dialog.SelectedPath.EndsWith("\\", StringComparison.Ordinal)) { box.Text += "\\"; } } } }; double margin = 5; Rect boxRect = new Rect(uiCmd.Rect.Left, uiCmd.Rect.Top, uiCmd.Rect.Width - (uiCmd.Rect.Height + margin), uiCmd.Rect.Height); Rect btnRect = new Rect(boxRect.Right + margin, uiCmd.Rect.Top, uiCmd.Rect.Height, uiCmd.Rect.Height); DrawToCanvas(r, box, boxRect); DrawToCanvas(r, button, btnRect); }
/// <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); }
/// <summary> /// Render Image 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 RenderImage(RenderInfo r, UICommand uiCmd) { Debug.Assert(uiCmd.Info.GetType() == typeof(UIInfo_Image)); UIInfo_Image info = uiCmd.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(uiCmd.Addr.Plugin, uiCmd.Text)) { if (ImageHelper.GetImageType(uiCmd.Text, out ImageHelper.ImageType type)) { return; } button = new Button() { Style = (Style)r.Window.FindResource("ImageButton") }; if (type == ImageHelper.ImageType.Svg) { double width = uiCmd.Rect.Width * r.MasterScale; double height = uiCmd.Rect.Height * r.MasterScale; button.Background = ImageHelper.SvgToImageBrush(ms, width, height); } else { button.Background = ImageHelper.ImageToImageBrush(ms); } } bool hasUrl = false; if (info.URL != null && string.Equals(info.URL, string.Empty, StringComparison.Ordinal) == false) { if (Uri.TryCreate(info.URL, UriKind.Absolute, out Uri unused)) { // Success hasUrl = true; } else { // Failure throw new InvalidUICommandException($"Invalid URL [{info.URL}]", uiCmd); } } if (hasUrl) { // Open URL button.Click += (object sender, RoutedEventArgs e) => { Process.Start(info.URL); }; } else { // Open picture with external viewer button.Click += (object sender, RoutedEventArgs e) => { if (ImageHelper.GetImageType(uiCmd.Text, out ImageHelper.ImageType t)) { return; } string path = System.IO.Path.ChangeExtension(System.IO.Path.GetTempFileName(), "." + t.ToString().ToLower()); using (MemoryStream ms = EncodedFile.ExtractInterfaceEncoded(uiCmd.Addr.Plugin, uiCmd.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, info.ToolTip); DrawToCanvas(r, button, uiCmd.Rect); }