Exemple #1
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);
            }
        }
Exemple #2
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);
        }