public static int CountNumberOfFormatInstances(TextElement element, TextFormat format)
 {
     if (format.Equals(new TextFormat(element))) return 1;
     return LogicalTreeHelper.GetChildren(element).OfType<TextElement>().Sum(o => CountNumberOfFormatInstances(o, format));
 }
 private void MapCurrentParagraph()
 {
     var par = mImportRichTextBox.Selection.Start.Paragraph;
     if (par == null)
     {
         MessageBox.Show("No paragraph at current selection");
         return;
     }
     if (!Model.IsBlockInTopLevelOrSection(par))
     {
         MessageBox.Show("Mapped paragraphs must be in document root or one of its nested sections");
         return;
     }
     var format = new TextFormat(par);
     var dialog = new SelectMarkupWindow() { Owner = this, WindowStartupLocation = WindowStartupLocation.CenterOwner };
     dialog.SetPossibleMarkup(Model.PossibleMarkup);
     bool? res = dialog.ShowDialog();
     if (res.HasValue)
     {
         if (res.Value)
         {
             string markup = dialog.mMarkupComboBox.Text.Trim();
             if (String.IsNullOrEmpty(markup))
             {
                 MessageBox.Show("No markup selected");
                 return;
             }
             Model.AddFormatMapping(format, markup);
         }
     }
 }
        private void FixParagraphColors(TextElement elem)
        {
            var par = elem as Paragraph;
            if (par!=null)
            {
                if (par.Inlines.Count>0)
                {
                    bool sameFore = true;
                    bool sameBack = true;
                    var firstFmt = new TextFormat(par.Inlines.FirstInline);
                    foreach (var i in par.Inlines)
                    {
                        var fmt = new TextFormat(i);
                        if (fmt.ForegroundColor != firstFmt.ForegroundColor) sameFore = false;
                        if (fmt.BackgroundColor != firstFmt.BackgroundColor) sameBack = false;
                    }
                    var parFmt = new TextFormat(par);
                    if (parFmt.ForegroundColor == Colors.Black && sameFore) par.Foreground = new SolidColorBrush(firstFmt.ForegroundColor);
                    if (parFmt.BackgroundColor == Colors.White && sameBack) par.Background = new SolidColorBrush(firstFmt.BackgroundColor);
                }

            }
            else
            {
                foreach (var e in LogicalTreeHelper.GetChildren(elem).OfType<TextElement>()) FixParagraphColors(e);
            }
        }
 public void RemoveFormatMapping(TextFormat format)
 {
     if (mFormatMarkupMapping.ContainsKey(format))
     {
         mFormatMarkupMapping.Remove(format);
         FireChanged();
     }
 }
 public int CountNumberOfFormatInstances(TextFormat format)
 {
     return ImportDocument.Blocks.Sum(b => CountNumberOfFormatInstances(b, format));
 }
 public void ApplyFormatTags(TextElement elem)
 {
     var format = new TextFormat(elem);
     if (mFormatMarkupMapping.ContainsKey(format))
     {
         elem.Tag = mFormatMarkupMapping[format];
     }
     else
     {
         foreach (var e in LogicalTreeHelper.GetChildren(elem).OfType<TextElement>()) ApplyFormatTags(e);
     }
 }
 public void AddFormatMapping(TextFormat format, string markup)
 {
     if (mFormatMarkupMapping.ContainsKey(format)) mFormatMarkupMapping.Remove(format);
     mFormatMarkupMapping.Add(format, markup);
     FireChanged();
 }