Exemple #1
0
        /// <summary>Creates the UI elements for the given HTML node and HTML view.</summary>
        /// <param name="node">The HTML node.</param>
        /// <param name="htmlView">The HTML view.</param>
        /// <returns>The UI elements.</returns>
        public DependencyObject[] CreateControls(HtmlNode node, IHtmlView htmlView)
        {
            var controls = new List<Grid>();
            foreach (var child in node.Children.OfType<HtmlTagNode>().Where(c => c.Name == "li"))
            {
                var grid = new Grid();
                grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(20)});
                grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });

                var textBlock = CreateBulletSymbol(htmlView);
                                grid.Children.Add(textBlock);
                Grid.SetColumn(textBlock, 0);

                var panel = new StackPanel();

                child.WrapWithHtmlTag();
                foreach (var c in child.GetChildControls(htmlView).OfType<UIElement>())
                {
                    var frameworkElement = c as FrameworkElement;
                    if (frameworkElement != null)
                        frameworkElement.HorizontalAlignment = HorizontalAlignment.Stretch;

                    panel.Children.Add(c);
                }

                grid.Children.Add(panel);
                Grid.SetColumn(panel, 1);

                controls.Add(grid);
            }

            AdjustMargins(htmlView, controls);
            return controls.OfType<DependencyObject>().ToArray();
        }
        /// <summary>Creates the UI elements for the given HTML node and HTML view.</summary>
        /// <param name="node">The HTML node.</param>
        /// <param name="htmlView">The HTML view.</param>
        /// <returns>The UI elements.</returns>
        public DependencyObject[] CreateControls(HtmlNode node, IHtmlView htmlView)
        {
            var control = CreateControl(node, htmlView);
            if (control != null)
                return new [] { control };

            return new DependencyObject[] { };
        }
Exemple #3
0
        /// <summary>Creates the UI elements for the given HTML node and HTML view.</summary>
        /// <param name="node">The HTML node.</param>
        /// <param name="htmlView">The HTML view.</param>
        /// <returns>The UI elements.</returns>
        public DependencyObject[] CreateControls(HtmlNode node, IHtmlView htmlView)
        {
            try
            {
                var link = node.Attributes["href"];
                var hyperlink = new Hyperlink();

                if (Foreground != null)
                    hyperlink.Foreground = Foreground;

#if !WINRT
                hyperlink.MouseOverForeground = htmlView.Foreground;
                hyperlink.TextDecorations = TextDecorations.Underline;
#endif                
                foreach (var child in node.Children)
                {
                    var leaves = child.GetControls(htmlView).ToArray();
                    if (leaves.Length > 0)
                    {
                        foreach (var item in leaves)
                        {
                            if (item is Inline)
                                hyperlink.Inlines.Add((Inline)item);
                            else
                                hyperlink.Inlines.Add(new InlineUIContainer { Child = (UIElement) item });
                        }
                    }
                    else if (child is HtmlTextNode && !string.IsNullOrEmpty(((HtmlTextNode)child).Text))
                        hyperlink.Inlines.Add(new Run { Text = ((HtmlTextNode)child).Text });
                }

#if WINRT

                var action = CreateLinkAction(hyperlink, link, htmlView);
                hyperlink.Click += (sender, e) =>
                {
                    action();
                    ((Control)htmlView).Focus(FocusState.Programmatic);
                };

#else

                var action = CreateLinkAction(hyperlink, link, htmlView);
                hyperlink.Command = new RelayCommand(delegate
                {
                    if (!PhoneApplication.IsNavigating)
                        action();
                });

#endif
                
                return new DependencyObject[] { hyperlink };
            }
            catch
            {
                return node.GetChildControls(htmlView); // suppress link 
            }
        }
 /// <summary>Creates the UI elements for the given HTML node and HTML view.</summary>
 /// <param name="node">The HTML node.</param>
 /// <param name="htmlView">The HTML view.</param>
 /// <returns>The UI elements.</returns>
 public DependencyObject[] CreateControls(HtmlNode node, IHtmlView htmlView)
 {
     var controls = node.GetChildControls(htmlView); 
     foreach (var leave in controls)
     {
         var element = leave as TextElement;
         if (element != null)
             element.FontWeight = FontWeights.Bold;
     }
     return controls;
 }
        /// <summary>Creates a single UI element for the given HTML node and HTML view.</summary>
        /// <param name="node">The node.</param>
        /// <param name="htmlView">The text block.</param>
        /// <returns>The UI element.</returns>
        public override DependencyObject CreateControl(HtmlNode node, IHtmlView htmlView)
        {
            try
            {
                var imageUri = node.Attributes["src"];

                var height = 0;
                if (node.Attributes.ContainsKey("height"))
                    int.TryParse(node.Attributes["height"], out height);

                var width = 0;
                if (node.Attributes.ContainsKey("width"))
                    int.TryParse(node.Attributes["width"], out width);

                if (height == 1 && width == 1)
                    return null;

                var image = new Image();
                image.Width = 0;
                image.Height = 0;

                var bitmapImage = new BitmapImage(new Uri(imageUri));

                var imageBlock = new ImageBlock
                {
                    Image = image,
                    UserHeight = height,
                    UserWidth = width,
                    Source = bitmapImage
                };

                bitmapImage.ImageOpened += delegate { imageBlock.Update(htmlView.ActualWidth); };

                image.HorizontalAlignment = HorizontalAlignment.Left;
                image.Source = bitmapImage;
                image.Margin = new Thickness(0, htmlView.ParagraphMargin, 0, htmlView.ParagraphMargin);

                if (width > 0)
                    image.Width = width;
                if (height > 0)
                    image.Height = height;

                htmlView.SizeDependentControls.Add(imageBlock);
                return new ContentPresenter { Content = image };
            }
            catch
            {
                return null;
            }
        }
        /// <summary>Creates the UI elements for the given HTML node and HTML view.</summary>
        /// <param name="node">The HTML node.</param>
        /// <param name="htmlView">The HTML view.</param>
        /// <returns>The UI elements.</returns>
        public DependencyObject[] CreateControls(HtmlNode node, IHtmlView htmlView)
        {
            var controls = node.GetChildControls(htmlView);
            foreach (var leave in controls)
            {
                var element = leave as TextElement;
                if (element != null)
                {
#if WINRT
                    element.FontStyle = FontStyle.Italic;
#else
                    element.FontStyle = FontStyles.Italic;
#endif
                }
            }
            return controls;
        }
Exemple #7
0
        /// <summary>Creates the UI elements for the given HTML node and HTML view.</summary>
        /// <param name="node">The HTML node.</param>
        /// <param name="textBlock">The HTML view.</param>
        /// <returns>The UI elements.</returns>
        public DependencyObject[] CreateControls(HtmlNode node, IHtmlView textBlock)
        {
            var leaves = node.GetChildControls(textBlock);
            if (leaves.Length == 1 && leaves[0] is Panel)
                leaves = new[] { leaves[0] };

            if (leaves.Length > 0)
            {
                var element = leaves.First() as FrameworkElement;
                if (element != null)
                    element.Margin = new Thickness(element.Margin.Left, 0, element.Margin.Right, element.Margin.Bottom);

                element = leaves.Last() as FrameworkElement;
                if (element != null)
                    element.Margin = new Thickness(element.Margin.Left, element.Margin.Top, element.Margin.Right, 0);
            }

            return leaves;
        }
        /// <summary>Creates the UI elements for the given HTML node and HTML view.</summary>
        /// <param name="node">The HTML node.</param>
        /// <param name="htmlView">The HTML view.</param>
        /// <returns>The UI elements.</returns>
        public DependencyObject[] CreateControls(HtmlNode node, IHtmlView htmlView)
        {
            var list = new List<DependencyObject>();

            var isFirst = true; 
            var addTopMargin = true; 
            var current = new List<Inline>();

            foreach (var child in node.GetChildControls(htmlView))
            {
                if (isFirst)
                {
                    if (child is Run)
                    {
                        var run = (Run)child;
                        run.Text = run.Text.TrimStart(' ', '\t'); // TODO: Trim all white spaces
                    }
                    isFirst = false;
                }

                if (child is Run && UseTextSplitting && ((Run)child).Text.Contains("\n")) // used to avoid 2048px max control size
                {
                    // split text
                    var run = (Run) child;
                    var splits = run.Text.Split('\n');

                    // join some splits to avoid small junks 
                    var currentSplit = "";
                    var newSplits = new List<string>();
                    for (var i = 0; i < splits.Length; i++)
                    {
                        var split = splits[i];
                        if (i != 0 && currentSplit.Length + split.Length > 16)
                        {
                            newSplits.Add(currentSplit);
                            currentSplit = split;
                        }
                        else
                            currentSplit += (i != 0 ? "\n" : "") + split;
                    }
                    newSplits.Add(currentSplit);

                    // create multiple text blocks
                    splits = newSplits.ToArray();
                    for (var i = 0; i < splits.Length; i++)
                    {
                        var split = splits[i];
                        current.Add(new Run { Text = split });
                        if (i < splits.Length - 1) // dont create for last
                            CreateTextBox(node, list, current, htmlView, i == 0 && addTopMargin, false);
                    }
                    addTopMargin = list.Count == 0; 
                } else if (child is Inline)
                    current.Add((Inline)child);
                else
                {
                    CreateTextBox(node, list, current, htmlView, addTopMargin, true);
                    list.Add(child);
                    addTopMargin = true; 
                }
            }

            CreateTextBox(node, list, current, htmlView, addTopMargin, true);

            if (list.Count == 0)
                return null;

            return list.ToArray();
        }
        protected virtual RichTextBlock CreateTextBlock(HtmlNode node, IHtmlView htmlView)
        {
            var textBlock = new RichTextBlock();
#endif

            textBlock.FontSize = htmlView.FontSize * FontSize;
            textBlock.Foreground = Foreground ?? htmlView.Foreground;
            textBlock.FontFamily = FontFamily ?? htmlView.FontFamily;
            textBlock.FontStyle = FontStyle;
            textBlock.FontWeight = FontWeight;

            return textBlock;
        }
        /// <summary>Creates a formatted text block.</summary>
        /// <param name="node">The node.</param>
        /// <param name="htmlView">The HTML view.</param>
#if !WINRT
        protected virtual RichTextBox CreateTextBlock(HtmlNode node, IHtmlView htmlView)
        {
            var textBlock = new RichTextBox();
        private void CreateTextBox(HtmlNode node, List<DependencyObject> list, List<Inline> current, IHtmlView htmlView, bool addTopMargin, bool addBottomMargin)
        {
            if (current.Count > 0)
            {
                var textBlock = CreateTextBlock(node, htmlView);

                var p = new Paragraph();
                foreach (var r in current)
                    p.Inlines.Add(r);

                textBlock.Blocks.Add(p);

#if !WINRT
                textBlock.Background = textBlock.Background;
                textBlock.Margin = new Thickness(-12, addTopMargin ? htmlView.ParagraphMargin : 0, -12, addBottomMargin ? htmlView.ParagraphMargin : 0);
#else
                textBlock.IsTextSelectionEnabled = false;
                textBlock.Margin = new Thickness(0, addTopMargin ? htmlView.ParagraphMargin : 0, 0, addBottomMargin ? htmlView.ParagraphMargin : 0);
#endif

                list.Add(textBlock);
                current.Clear();
            }
        }
Exemple #12
0
 /// <summary>Creates a single UI element for the given HTML node and HTML view.</summary>
 /// <param name="node">The node.</param>
 /// <param name="htmlView">The text block.</param>
 /// <returns>The UI element.</returns>
 public override DependencyObject CreateControl(HtmlNode node, IHtmlView htmlView)
 {
     return new Run { Text = ((HtmlTextNode)node).Text };
 }
 /// <summary>Creates a single UI element for the given HTML node and HTML view.</summary>
 /// <param name="node">The node.</param>
 /// <param name="htmlView">The HTML view.</param>
 /// <returns>The UI element.</returns>
 public abstract DependencyObject CreateControl(HtmlNode node, IHtmlView htmlView);
Exemple #14
0
 /// <summary>Adds a child node.</summary>
 /// <param name="node">The node to add.</param>
 public void AddChild(HtmlNode node)
 {
     Children.Add(node);
 }
Exemple #15
0
 /// <summary>Creates the UI elements for the given HTML node and HTML view.</summary>
 /// <param name="node">The HTML node.</param>
 /// <param name="textBlock">The HTML view.</param>
 /// <returns>The UI elements.</returns>
 public DependencyObject[] CreateControls(HtmlNode node, IHtmlView textBlock)
 {
     return new DependencyObject[] { };
 }