Inheritance: FrameworkElement, IRichTextBlock
Ejemplo n.º 1
0
		private static void CreateTextBox(List<DependencyObject> list, List<Inline> current, IHtmlTextBlock textBlock)
		{
			if (current.Count > 0)
			{
				var p = new Paragraph();
				foreach (var r in current)
					p.Inlines.Add(r);

#if !WINRT
				var tb = new RichTextBox();
				tb.Background = textBlock.Background;
				tb.Margin = new Thickness(-12, textBlock.ParagraphMargin, -12, textBlock.ParagraphMargin);
#else
				var tb = new RichTextBlock();
				tb.IsTextSelectionEnabled = false;
				tb.Margin = new Thickness(0, textBlock.ParagraphMargin, 0, textBlock.ParagraphMargin);
#endif
				tb.Blocks.Add(p);
				tb.Foreground = textBlock.Foreground;
				tb.FontSize = textBlock.FontSize;
				tb.FontFamily = textBlock.FontFamily;
				
				list.Add(tb);
				current.Clear();
			}
		}
Ejemplo n.º 2
0
        public SyntaxViewer()
        {
            DefaultStyleKey = typeof(SyntaxViewer);

            TextView = new RichTextBlock { /* FontSize = 13, FontFamily = new FontFamily("Consolas") */ };
            LineNumberBlock = new TextBlock { /*FontSize = 13, FontFamily = new FontFamily("Consolas"),  */ Foreground = new SolidColorBrush(Color.FromArgb(255, 43, 145, 175)) };
        }
        public GroupBoxContainer(Span span)
        {
            var grid = new Grid();
            grid.RowDefinitions.Add(new RowDefinition
            {
                Height = GridLength.Auto
            });
            grid.RowDefinitions.Add(new RowDefinition());

            _titleBlock = new TextBlock();
            grid.Children.Add(_titleBlock);

            var border = new Border
            {
                BorderBrush = new SolidColorBrush(Colors.Gray),
                BorderThickness = new Thickness(1, 0, 0, 1),
                Margin = new Thickness(5, 0, 0, 5),
                Padding = new Thickness(3, 0, 0, 3)
            };
            Grid.SetRow(border, 1);
            grid.Children.Add(border);

            var richTextBlock = new RichTextBlock()
            {
                IsTextSelectionEnabled = false
            };
            border.Child = richTextBlock;
            _innerContainer = new RichTextBlockContainer(richTextBlock);

            span.Inlines.Add(new InlineUIContainer()
            {
                Child = grid
            });
        }
Ejemplo n.º 4
0
 public AccountView()
 {
     var usernameBlock = new RichTextBlock();
     var passwordBlock = new RichTextBlock();
     var fontFmy = new FontFamily("Segoe UI");
     usernameBlock.FontFamily = fontFmy;
     usernameBlock.FontSize = 14;
 }
Ejemplo n.º 5
0
 private static double FontSize(Models.IBlock block, RichTextBlock textblock)
 {
     if (block is Models.Header)
         return GetHeaderFontSize(textblock);
     else if (block is Models.Body)
         return GetBodyFontSize(textblock);
     else
         return 16;
 }
 public virtual void Add(Block block)
 {
     var richTextBlock = new RichTextBlock
     {
         Margin = new Thickness(5)
     };
     richTextBlock.Blocks.Add(block);
     Add(richTextBlock);
 }
        private void RefreshView()
        {
            // anything?
            if (string.IsNullOrEmpty(Markup))
            {
                this.Content = null;
                return;
            }

            // get the lines...
            var lines = new List<string>();
            using (var reader = new StringReader(this.Markup))
            {
                while(true)
                {
                    string buf = reader.ReadLine();
                    if (buf == null)
                        break;
                    lines.Add(buf);
                }
            }

            // walk...
            var block = new RichTextBlock();
            for (int index = 0; index < lines.Count; index++)
            {
                string nextLine = null;
                if (index < lines.Count - 1)
                    nextLine = lines[index + 1];

                // create a paragraph... and add it to the block...
                var para = new Paragraph();
                block.Blocks.Add(para);

                // create a "run" and add it to the paragraph...
                var run = new Run();
                run.Text = lines[index];
                para.Inlines.Add(run);

                // heading?
                if (nextLine != null && nextLine.StartsWith("="))
                {
                    // make it bigger, and then skip the next line...
                    para.FontSize = 20;
                    index++;
                }
                else if (nextLine != null && nextLine.StartsWith("-"))
                {
                    para.FontSize = 18;
                    index++;
                }
            }

            // set...
            this.Content = block;
        }
Ejemplo n.º 8
0
        public static BlockCollection ConvertHtmlToRtf(string html)
        {
            RichTextBlock parent = new RichTextBlock();
            XmlDocument document = new XmlDocument();
            document.LoadXml(html);

            ParseElement((XmlElement)(document.GetElementsByTagName("body")[0]), new RichTextBlockTextContainer(parent));

            return parent.Blocks;
        }
 public void Render(RichTextBlock richTextBlock)
 {
     GC.Collect();
     if (richTextBlock == null)
     {
         throw new ArgumentNullException("richTextBlock");
     }
     richTextBlock.Blocks.Clear();
     richTextBlock.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Left;
     RenderNode(Document.Body, new RichTextBlockContainer(richTextBlock));
 }
 public void Render(Border border)
 {
     GC.Collect();
     if (border == null)
     {
         throw new ArgumentNullException("border");
     }
     var richTextBlock = new RichTextBlock();
     border.Child = null;
     border.Child = richTextBlock;
     Render(richTextBlock);
 }
Ejemplo n.º 11
0
        void EscribirEnRichTextBox(string texto, RichTextBlock rtb)
        {
            rtb.Blocks.Clear();

            Run run = new Run();
            run.Text = texto;

            Paragraph parrafo = new Paragraph();
            parrafo.Inlines.Add(run);

            rtb.Blocks.Add(parrafo);
        }
Ejemplo n.º 12
0
 public void updateContent()
 {
     flipView.Items.Clear();
     Overflows.Clear();
     TextBlock = new RichTextBlock();
     TextBlock.Margin = new Thickness(50);
     TextBlock.Loaded += new RoutedEventHandler(textBlockLoaded);
     foreach (Block block in Chapter.GetBlocks())
     {
         TextBlock.Blocks.Add(block);
     }
     flipView.Items.Add(TextBlock);
 }
        public virtual void RenderElement(IElement element, ITextContainer parent, RenderContextBase context)
        {
            var border = new Border
            {
                BorderThickness = new Thickness(1),
                BorderBrush = new SolidColorBrush(Colors.Gray),
                Margin = new Thickness(0, 5, 0, 5),
                Padding = new Thickness(10)
            };
            parent.Add(border);

            var richTextBlock = new RichTextBlock();
            border.Child = richTextBlock;
            context.RenderNode(element, new RichTextBlockContainer(richTextBlock));
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Parses the given markdown into an AST.
        /// </summary>
        /// <param name="markdown"></param>
        /// <returns></returns>
        protected string RenderMarkdown(string markdown)
        {
            var parser = new Markdown();
            parser.Parse(markdown);

            var richTextBlock = new RichTextBlock();
            var renderer = new RenderToRichTextBlock(richTextBlock, new DummyLinkRegister());
            renderer.Render(parser);

            var result = new StringBuilder();
            foreach (var block in richTextBlock.Blocks)
            {
                SerializeElement(result, block, indentLevel: 0);
            }
            return result.ToString();
        }
Ejemplo n.º 15
0
        private static void OnHtmlChanged(DependencyObject sender, DependencyPropertyChangedEventArgs eventArgs)
        {
            Windows.UI.Xaml.Controls.RichTextBlock parent = (Windows.UI.Xaml.Controls.RichTextBlock)sender;
            parent.Blocks.Clear();

            XmlDocument document = new XmlDocument();
            var         text     = (string)eventArgs.NewValue;

            if (!text.StartsWith(XML_FIRST_NODE))
            {
                text = XML_FIRST_NODE + $"<body>{text}</body>";
            }
            document.LoadXml(text);

            ParseElement((XmlElement)document.GetElementsByTagName("body")[0], new RichTextBlockTextContainer(parent));
        }
Ejemplo n.º 16
0
 private static void Refresh(RichTextBlock textblock)
 {
     var itemsSource = GetItemsSource(textblock) as IEnumerable<Models.IBlock>;
     if (itemsSource == null)
         return;
     var items = itemsSource.Select((x, i) => new { Block = x, Index = i });
     foreach (var block in (textblock.Blocks).Select((x, i) => new { Paragraph = x as Paragraph, Index = i }))
     {
         var item = items.First(x => x.Index == block.Index).Block;
         var size = FontSize(item, textblock);
         if (!Equals(block.Paragraph.FontSize, size))
             block.Paragraph.FontSize = size;
         var indent = Indent(item, textblock);
         if (!Equals(block.Paragraph.TextIndent, indent))
             block.Paragraph.TextIndent = indent;
     }
 }
Ejemplo n.º 17
0
 private static void Fill(RichTextBlock textblock)
 {
     var itemsSource = GetItemsSource(textblock) as IEnumerable<Models.IBlock>;
     if (itemsSource == null)
         return;
     textblock.Blocks.Clear();
     foreach (var item in itemsSource)
     {
         var paragraph = new Paragraph
         {
             Margin = new Thickness(0, 0, 0, 16d),
             FontSize = FontSize(item, textblock),
             TextIndent = Indent(item, textblock),
         };
         if (item is Models.Header)
             paragraph.Foreground = Colors.Black.ToSolidColorBrush();
         paragraph.Inlines.Add(item.ToRun());
         textblock.Blocks.Add(paragraph);
     }
 }
        public virtual void RenderElement(IElement element, ITextContainer parent, RenderContextBase context)
        {
            var table = (IHtmlTableElement)element;

            var grid = new Grid();

            foreach (var child in table.Rows)
            {
                var row = child as IHtmlTableRowElement;
                if (row != null)
                {
                    grid.RowDefinitions.Add(new RowDefinition());
                    int columnCount = 0;
                    foreach (var column in row.Cells)
                    {
                        var richTextBlock = new RichTextBlock()
                        {
                            IsTextSelectionEnabled = false
                        };
                        var cellRender = new Border()
                        {
                            Child = richTextBlock,
                            Padding = new Thickness(5)
                        };
                        context.RenderNode(column, new RichTextBlockContainer(richTextBlock));
                        Grid.SetColumn(cellRender, columnCount);
                        Grid.SetRow(cellRender, grid.RowDefinitions.Count - 1);
                        grid.Children.Add(cellRender);

                        columnCount++;

                        if (grid.ColumnDefinitions.Count < columnCount)
                        {
                            grid.ColumnDefinitions.Add(new ColumnDefinition());
                        }
                    }
                }
            }

            parent.Add(grid);
        }
Ejemplo n.º 19
0
        private static void UpdateCacheAndCheckIfNewValue(RichTextBlock richTextBlock, ObservableCollection<BaseContentModel> newValue)
        {
            var added = false;
            if (!RichTextBlockCache.ContainsKey(richTextBlock))
            {
                RichTextBlockCache.TryAdd(richTextBlock, newValue);
                added = true;
            }

            var oldValue = RichTextBlockCache[richTextBlock];
            if (oldValue != newValue || added)
            {
                NotifyCollectionChangedEventHandler func =
                    (sender, ev) => NewValueOnCollectionChanged(sender, ev, richTextBlock);
                if (oldValue != null && !added)
                    oldValue.CollectionChanged -= func;
                if (newValue != null)
                    newValue.CollectionChanged += func;

                RichTextBlockCache[richTextBlock] = newValue;
            }
        }
Ejemplo n.º 20
0
        public static void FromHtml(RichTextBlock rtb, string html)
        {
            var oldHtml = html;
            html = System.Net.WebUtility.HtmlDecode(html);
            html = html.Replace("&bull;", "<br />    - ");
            html = html.Replace("&middot;", "<br />    - ");
            html = html.Replace("<li>", "    - ").Replace("</li>", "<br />").Replace("<ul>","<p>").Replace("</ul>","</p>");
            
            html = html.Replace("<br /><br /><p>", "<br /><p>");

            html = $"<html><body><p>{html}</p></body></html>";
            var v = new XmlConvert();
            var htmlFromDoc = v.Convert(html);
            htmlFromDoc = htmlFromDoc.Replace("<p />", "");
            XmlDocument document = new XmlDocument();
            document.LoadXml(htmlFromDoc);

            XmlElement elem = (XmlElement)(document.GetElementsByTagName("body")[0]);

            var container = new RichTextBlockTextContainer(rtb);
            
            ParseElement(elem, container);
        }
 public PageToPrint()
 {
     this.InitializeComponent();
     TextContentBlock = TextContent;
 }
Ejemplo n.º 22
0
        private static async void HtmlChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            // Get the target RichTextBlock
            Windows.UI.Xaml.Controls.RichTextBlock richText = d as Windows.UI.Xaml.Controls.RichTextBlock;
            if (richText == null)
            {
                return;
            }

            // Wrap the value of the Html property in a div and convert it to a new RichTextBlock
            string xhtml = string.Format("<div><p>{0}</p></div>", e.NewValue as string);

            xhtml = xhtml.Replace("\r", "").Replace("\n", "<br />");
            xhtml = Regex.Replace(xhtml, @"<br\s*>", "<br/>", RegexOptions.IgnoreCase);
            Windows.UI.Xaml.Controls.RichTextBlock newRichText = null;
            if (Windows.ApplicationModel.DesignMode.DesignModeEnabled)
            {
                // In design mode we swallow all exceptions to make editing more friendly
                string xaml = "";
                try
                {
                    xaml = await ConvertHtmlToXamlRichTextBlock(xhtml);

                    newRichText = (Windows.UI.Xaml.Controls.RichTextBlock)XamlReader.Load(xaml);
                }
                catch (Exception ex)
                {
                    string errorxaml = string.Format(@"
                        <RichTextBlock 
                         xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
                         xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
                        >
                            <Paragraph>An exception occurred while converting HTML to XAML: {0}</Paragraph>
                            <Paragraph />
                            <Paragraph>HTML:</Paragraph>
                            <Paragraph>{1}</Paragraph>
                            <Paragraph />
                            <Paragraph>XAML:</Paragraph>
                            <Paragraph>{2}</Paragraph>
                        </RichTextBlock>",
                                                     ex.Message,
                                                     EncodeXml(xhtml),
                                                     EncodeXml(xaml)
                                                     );
                    newRichText = (Windows.UI.Xaml.Controls.RichTextBlock)XamlReader.Load(errorxaml);
                } // Display a friendly error in design mode.
            }
            else
            {
                // When not in design mode, we let the application handle any exceptions
                string xaml = "";
                try
                {
                    xaml = await ConvertHtmlToXamlRichTextBlock(xhtml);

                    newRichText = (Windows.UI.Xaml.Controls.RichTextBlock)XamlReader.Load(xaml);
                }
                catch (Exception)
                {
                    var htmlStripedTags = Regex.Replace(xhtml, "<.*?>", "");
                    htmlStripedTags = System.Net.WebUtility.HtmlEncode(htmlStripedTags);
                    string errorxaml = $@"
                        <RichTextBlock 
                         xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
                         xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
                        >
                            <Paragraph>{htmlStripedTags}</Paragraph>
                        </RichTextBlock>";

                    newRichText = (Windows.UI.Xaml.Controls.RichTextBlock)XamlReader.Load(errorxaml);
                } // Display a friendly error in design mode.
            }

            // Move the blocks in the new RichTextBlock to the target RichTextBlock
            richText.Blocks.Clear();
            if (newRichText != null)
            {
                for (int i = newRichText.Blocks.Count - 1; i >= 0; i--)
                {
                    Block b = newRichText.Blocks[i];
                    newRichText.Blocks.RemoveAt(i);
                    richText.Blocks.Insert(0, b);
                }
            }
        }
Ejemplo n.º 23
0
 public static void SetItemsSource(RichTextBlock obj, IEnumerable<object> value) { obj.SetValue(ItemsSourceProperty, value); }
Ejemplo n.º 24
0
 public static IEnumerable<object> GetItemsSource(RichTextBlock obj) { return (IEnumerable<object>)obj.GetValue(ItemsSourceProperty); }
Ejemplo n.º 25
0
 public static void SetBodyIndent(RichTextBlock obj, double value) { obj.SetValue(BodyIndentProperty, value); }
Ejemplo n.º 26
0
 public static double GetBodyIndent(RichTextBlock obj) { return (double)obj.GetValue(BodyIndentProperty); }
Ejemplo n.º 27
0
 public static void SetBodyFontSize(RichTextBlock obj, double value) { obj.SetValue(BodyFontSizeProperty, value); }
Ejemplo n.º 28
0
 public RichTextBlockTextContainer(Windows.UI.Xaml.Controls.RichTextBlock richTextBlock)
 {
     _richTextBlock = richTextBlock;
 }
 public RichTextBlockTextContainer(RichTextBlock richTextBlock)
 {
     _richTextBlock = richTextBlock;
 }
Ejemplo n.º 30
0
 private static double Indent(Models.IBlock block, RichTextBlock textblock)
 {
     if (block is Models.Header)
         return GetHeaderIndent(textblock);
     else if (block is Models.Body)
         return GetBodyIndent(textblock);
     else
         return 16;
 }
 public void Add(Block block)
 {
     var richTextBlock = new RichTextBlock();
     richTextBlock.Blocks.Add(block);
     Add(richTextBlock);
 }
Ejemplo n.º 32
0
 public DefaultOutput(RichTextBlock richTextBlock)
 {
     RichTextBlock = richTextBlock;
     RichTextBlock.Blocks.Add(Paragraph);
 }
Ejemplo n.º 33
0
 public static double GetBodyFontSize(RichTextBlock obj) { return (double)obj.GetValue(BodyFontSizeProperty); }