|
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TextRangeExtensions.cs" company="Catel development team">
// Copyright (c) 2008 - 2011 Catel development team. All rights reserved.
// </copyright>
// <summary>
// Extension methods for <see cref="TextRange" />.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
using System;
using System.IO;
using System.Windows;
using System.Windows.Documents;
using log4net;
namespace Catel.Windows.Documents
{
/// <summary>
/// Extension methods for <see cref="TextRange"/>.
/// </summary>
public static class TextRangeExtensions
{
#region Variables
/// <summary>
/// The <see cref="ILog">log</see> object.
/// </summary>
private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
#endregion
/// <summary>
/// Checks whether a <see cref="TextRange"/> is empty or not.
/// </summary>
/// <param name="textRange">TextRange to check.</param>
/// <returns>True if TextRange is empty, otherwhise false.</returns>
public static bool IsEmpty(this TextRange textRange)
{
return ((textRange.Text.Length == 0) || (textRange.Text == Environment.NewLine));
}
/// <summary>
/// Sets text into a text range.
/// </summary>
/// <param name="range"><see cref="TextRange"/> to set the text into.</param>
/// <param name="text">Text to set into the text range.</param>
public static void SetText(this TextRange range, string text)
{
try
{
using (MemoryStream memoryStream = new MemoryStream())
{
using (StreamWriter writer = new StreamWriter(memoryStream))
{
writer.Write(text);
writer.Flush();
memoryStream.Position = 0;
string dataFormat = text.GetDataFormat();
range.Load(memoryStream, dataFormat);
if (text.GetDataFormat() == System.Windows.DataFormats.Rtf)
{
// Does the Rtf text contain formatting?
if (!text.Contains(Rtf.RtfFontStyleMarker))
{
// No, since the Rtf does not pass any formatting, clear it
range.ClearFormatting();
}
range.ClearTextMargins();
}
}
}
}
catch (Exception ex)
{
Log.Warn(ex, Properties.TraceMessages.LoadTextFailed);
}
}
/// <summary>
/// Gets the text from a text range.
/// </summary>
/// <param name="range"><see cref="TextRange"/> to extract the text from.</param>
/// <returns>Text in the text range.</returns>
public static string GetText(this TextRange range)
{
string result = string.Empty;
if (range != null)
{
using (MemoryStream contentStream = new MemoryStream())
{
range.Save(contentStream, System.Windows.DataFormats.Rtf);
contentStream.Position = 0L;
StreamReader reader = new StreamReader(contentStream);
result = reader.ReadToEnd();
}
}
return result;
}
/// <summary>
/// Clears the margins of a <see cref="TextRange"/>.
/// </summary>
/// <param name="range"><see cref="TextRange"/> to clear the margins of.</param>
public static void ClearTextMargins(this TextRange range)
{
if (range == null)
{
return;
}
if (range.Start.Paragraph != null)
{
TextRange curRange = null;
Block cur = range.Start.Paragraph;
do
{
// In case of a list item, set special margin
if (cur.Parent is ListItem)
{
List list = ((ListItem)cur.Parent).Parent as List;
if (list != null)
{
list.Margin = new Thickness(list.Margin.Left, list.Margin.Top, list.Margin.Right, Double.NaN);
cur = list;
}
}
else
{
// Clear the margin (let document decide the margins)
cur.ClearValue(Block.MarginProperty);
}
curRange = new TextRange(cur.ContentStart, cur.ContentEnd);
} while ((range.End.Paragraph == null || !curRange.Contains(range.End.Paragraph.ContentEnd)) && (cur = cur.NextBlock) != null);
}
}
/// <summary>
/// Clears the formatting of a <see cref="TextRange"/>.
/// </summary>
/// <param name="range"><see cref="TextRange"/> to clear the formatting of.</param>
public static void ClearFormatting(this TextRange range)
{
if (range == null)
{
return;
}
if (range.Start.Paragraph != null)
{
TextRange curRange;
Block cur = range.Start.Paragraph;
do
{
// Clear formatting
cur.ClearValue(Block.FontFamilyProperty);
cur.ClearValue(Block.FontSizeProperty);
curRange = new TextRange(cur.ContentStart, cur.ContentEnd);
} while ((range.End.Paragraph == null || !curRange.Contains(range.End.Paragraph.ContentEnd)) && (cur = cur.NextBlock) != null);
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.