private async void FillRichTextBox(string text)
 {
     var start = 0;
     var regex = new Regex("\\$\\[img:[0-9]+\\]\\$");
     var paragraph = new Paragraph();
     foreach (Match match in regex.Matches(text))
     {
         var imageId = Int32.Parse(Regex.Match(match.Value, @"[0-9]+").Value);
         System.Diagnostics.Debug.WriteLine(imageId);
         paragraph.AddText(start, match.Index, text);
         await paragraph.ReplaceLabelWithImage(imageId);
         start = match.Index + match.Length;
     }
     paragraph.AddText(start, text.Length, text);
     //App.ViewModel.Blocks.Add(paragraph);
     RichTextBox.Blocks.Add(paragraph);
 }
Ejemplo n.º 2
0
        internal object BuildObjectTree()
        { 
            IAddChild root;
            switch (_type) 
            { 
                case ElementType.Table:
                    root = new Table(); 
                    break;
                case ElementType.TableRowGroup:
                    root = new TableRowGroup();
                    break; 
                case ElementType.TableRow:
                    root = new TableRow(); 
                    break; 
                case ElementType.TableCell:
                    root = new TableCell(); 
                    break;
                case ElementType.Paragraph:
                    root = new Paragraph();
                    break; 
                case ElementType.Hyperlink:
                    Hyperlink link = new Hyperlink(); 
                    link.NavigateUri = GetValue(NavigateUriProperty) as Uri; 
                    link.RequestNavigate += new RequestNavigateEventHandler(ClickHyperlink);
                    AutomationProperties.SetHelpText(link, (String)this.GetValue(HelpTextProperty)); 
                    AutomationProperties.SetName(link, (String)this.GetValue(NameProperty));
                    root = link;
                    break;
                default: 
                    Debug.Assert(false);
                    root = null; 
                    break; 
            }
 
            ITextPointer pos = ((ITextPointer)_start).CreatePointer();

            while (pos.CompareTo((ITextPointer)_end) < 0)
            { 
                TextPointerContext tpc = pos.GetPointerContext(LogicalDirection.Forward);
                if (tpc == TextPointerContext.Text) 
                { 
                    root.AddText(pos.GetTextInRun(LogicalDirection.Forward));
                } 
                else if (tpc == TextPointerContext.EmbeddedElement)
                {
                    root.AddChild(pos.GetAdjacentElement(LogicalDirection.Forward));
                } 
                else if (tpc == TextPointerContext.ElementStart)
                { 
                    object obj = pos.GetAdjacentElement(LogicalDirection.Forward); 
                    if (obj != null)
                    { 
                        root.AddChild(obj);
                        pos.MoveToNextContextPosition(LogicalDirection.Forward);
                        pos.MoveToElementEdge(ElementEdge.BeforeEnd);
                    } 
                }
 
                pos.MoveToNextContextPosition(LogicalDirection.Forward); 

            } 
            return root;
        }
        internal object BuildObjectTree()
        {
            IAddChild root;

            switch (_type)
            {
            case ElementType.Table:
                root = new Table();
                break;

            case ElementType.TableRowGroup:
                root = new TableRowGroup();
                break;

            case ElementType.TableRow:
                root = new TableRow();
                break;

            case ElementType.TableCell:
                root = new TableCell();
                break;

            case ElementType.Paragraph:
                root = new Paragraph();
                break;

            case ElementType.Hyperlink:
                Hyperlink link = new Hyperlink();
                link.NavigateUri      = GetValue(NavigateUriProperty) as Uri;
                link.RequestNavigate += new RequestNavigateEventHandler(ClickHyperlink);
                AutomationProperties.SetHelpText(link, (String)this.GetValue(HelpTextProperty));
                AutomationProperties.SetName(link, (String)this.GetValue(NameProperty));
                root = link;
                break;

            default:
                Debug.Assert(false);
                root = null;
                break;
            }

            ITextPointer pos = ((ITextPointer)_start).CreatePointer();

            while (pos.CompareTo((ITextPointer)_end) < 0)
            {
                TextPointerContext tpc = pos.GetPointerContext(LogicalDirection.Forward);
                if (tpc == TextPointerContext.Text)
                {
                    root.AddText(pos.GetTextInRun(LogicalDirection.Forward));
                }
                else if (tpc == TextPointerContext.EmbeddedElement)
                {
                    root.AddChild(pos.GetAdjacentElement(LogicalDirection.Forward));
                }
                else if (tpc == TextPointerContext.ElementStart)
                {
                    object obj = pos.GetAdjacentElement(LogicalDirection.Forward);
                    if (obj != null)
                    {
                        root.AddChild(obj);
                        pos.MoveToNextContextPosition(LogicalDirection.Forward);
                        pos.MoveToElementEdge(ElementEdge.BeforeEnd);
                    }
                }

                pos.MoveToNextContextPosition(LogicalDirection.Forward);
            }
            return(root);
        }
        private async System.Threading.Tasks.Task FillRichTextBox(string text)
        {
            var start = 0;
            var regex = new Regex("(\\$[^\\$]+?\\$)|(\\$\\$[^\\$]+?\\$\\$)");
            var paragraph = new Paragraph();
            foreach (Match match in regex.Matches(text))
            {
                var matchVal = match.Value;
                var notInline = matchVal.StartsWith("$$");
                var imageId = notInline
                    ? match.Value.Substring(3, match.Value.Length - 6)
                    : match.Value.Substring(2, match.Value.Length - 4);
                
                paragraph.AddText(start, match.Index, text);

                if (notInline)
                {
                    MathQuestionBox.Blocks.Add(paragraph);
                    var imgParagraph = new Paragraph();
                    await imgParagraph.ReplaceLabelWithLargeImage(imageId);
                    MathQuestionBox.Blocks.Add(imgParagraph);
                    paragraph = new Paragraph();
                }
                else
                {
                    await paragraph.ReplaceLabelWithImage(imageId); 
                }                
                start = match.Index + match.Length;
            }
            paragraph.AddText(start, text.Length, text);
            //App.ViewModel.Blocks.Add(paragraph);
            MathQuestionBox.Blocks.Add(paragraph);
        }
Ejemplo n.º 5
0
        private Paragraph CreateParagraph(string message)
        {
            Paragraph para = new Paragraph();
            para.BorderBrush = Brushes.Red;
            var emoticons = emoticonMap.Keys.Cast<string>();
            var color = Brushes.Red;
            para.Padding = new Thickness(0);
            var fontFamilyConvertor = new FontFamilyConverter();
            var font = fontFamilyConvertor.ConvertFromString("Segoe UI") as FontFamily;

            if (message.Contains("[col]")) {
                var colorCode = message.Substring(message.IndexOf("[col]") + 5, message.IndexOf("[/col]") - (message.IndexOf("[col]") + 5));
                message = message.Replace("[col]" + colorCode + "[/col]", "");
                color = GetColor(colorCode);

                var fontCode = message.Substring(message.IndexOf("[fon]") + 5, message.IndexOf("[/fon]") - (message.IndexOf("[fon]") + 5));
                message = message.Replace("[fon]" + fontCode + "[/fon]", "");
                font = fontFamilyConvertor.ConvertFromString(fontCode) as FontFamily;

                para.BorderBrush = color;
                para.FontFamily = font;

                var name = message.TakeWhile(elem => !(elem.Equals(':')));
                var parsedStringName = new string(name.ToArray());
                var regex = new Regex(parsedStringName);
                message = regex.Replace(message, "", 1);
                parsedStringName += message[0];
                message = message.Substring(1);
                para.AddText(parsedStringName, Brushes.Black);
            }
            var emoteExists = emoticons.Any(emote => message.Contains(emote));
            while (emoteExists || message.ToLower().Contains("https") || message.ToLower().Contains("http") || message.ToLower().Contains("www")) {
                message = message.Trim();
                if ("".Equals(message)) break;
                if (message.ToLower().StartsWith("https") || message.ToLower().StartsWith("http") || message.ToLower().StartsWith("www")) {
                    Hyperlink link = new Hyperlink();
                    link.IsEnabled = true;
                    link.Inlines.Add(" (Click) ");
                    var startIndex = message.IndexOf("https:");
                    if (startIndex == -1) startIndex = message.IndexOf("http:");
                    if (startIndex == -1) startIndex = message.IndexOf("www.");
                    if (startIndex != -1) {
                        var endIndex = message.IndexOf(" ", startIndex);
                        if (endIndex == -1) endIndex = message.Count();
                        var prefix = message.Substring(startIndex, endIndex - startIndex).StartsWith("www.") ? "http://" : "";
                        link.NavigateUri = new Uri(prefix + message.Substring(startIndex, endIndex - startIndex));
                        para.Inlines.Add(link);
                        link.RequestNavigate += new RequestNavigateEventHandler(link_RequestNavigate);
                        message = message.Replace(message.Substring(startIndex, endIndex - startIndex), "");
                    }
                } else if (emoticons.Any(emote => message.Substring(0, message.IndexOf(" ") == -1 ? message.Count() : message.IndexOf(" ")).Contains(emote))) {
                    var parsedMessage = message.TakeWhile(elem => !emoteStart.Contains(elem));
                    var parsedStringMessage = new string(parsedMessage.ToArray());
                    if (!parsedStringMessage.Equals("")) {
                        var regex = new Regex(parsedStringMessage);
                        message = regex.Replace(message, "", 1);
                        para.AddText(parsedStringMessage, color);
                    }
                    if (message.Count() == 0) break;
                    var emotes = emoticons.Where(emote => message.Substring(0, message.Count() < MAX_EMOTE_LENGTH ? message.Count() : MAX_EMOTE_LENGTH).Contains(emote));
                    if (emotes.Count() > 0) {
                        para.Inlines.Add(GetImageBySymbol(emotes.Last()));
                        message = message.Substring(emotes.Last().Count());
                    } else {
                        para.AddText("" + message[0], color);
                        message = message.Substring(1);
                    }
                } else {
                    para.AddText(message.Substring(0, message.IndexOf(" ") == -1 ? message.Count() : message.IndexOf(" ")) + " ", color);
                    var regex = new Regex(message.Substring(0, message.IndexOf(" ") == -1 ? message.Count() : message.IndexOf(" ")));
                    message = regex.Replace(message, "", 1);
                }
            }
            if (!"".Equals(message)) {
                para.AddText(message, color);
            }
            return para;
        }