Generalized plaintext-to-FlowDocument methods.

This commit is contained in:
morkt 2015-05-16 17:12:50 +04:00
parent 8cd51ae896
commit cb870c592a

View File

@ -40,7 +40,7 @@ namespace GARbro.GUI
public AboutBox() public AboutBox()
{ {
InitializeComponent(); InitializeComponent();
LicenseTabText.Document = GetLicenseDoc(); LicenseTabText.Document = GetResourceDoc ("GARbro.GUI.LICENSE");
} }
private void Button_Click(object sender, RoutedEventArgs e) private void Button_Click(object sender, RoutedEventArgs e)
@ -50,40 +50,48 @@ namespace GARbro.GUI
#region License text backend #region License text backend
static FlowDocument GetLicenseDoc () static FlowDocument GetResourceDoc (string resource)
{ {
var assembly = Assembly.GetExecutingAssembly(); var assembly = Assembly.GetExecutingAssembly();
using (var stream = assembly.GetManifestResourceStream ("GARbro.GUI.LICENSE")) using (var stream = assembly.GetManifestResourceStream (resource))
{ {
if (null == stream) if (null == stream)
return new FlowDocument(); return new FlowDocument();
using (var reader = new StreamReader (stream)) return ReadPlainText (stream);
}
}
/// <summary>
/// Read plain text from stream and return it as FlowDocument.
/// </summary>
static FlowDocument ReadPlainText (Stream stream)
{
using (var reader = new StreamReader (stream))
{
var doc = new FlowDocument();
var para = new Paragraph();
for (;;)
{ {
var doc = new FlowDocument(); var line = reader.ReadLine();
var para = new Paragraph(); if (null == line)
for (;;) break;
if (line.Length > 0)
{ {
var line = reader.ReadLine(); if (para.Inlines.Count > 0)
if (null == line) para.Inlines.Add (" ");
break; para.Inlines.Add (line);
if (line.Length > 0)
{
if (para.Inlines.Count > 0)
para.Inlines.Add (" ");
para.Inlines.Add (line);
}
else
{
if (0 == para.Inlines.Count)
para.Inlines.Add (new LineBreak());
doc.Blocks.Add (para);
para = new Paragraph();
}
} }
if (para.Inlines.Count > 0) else
{
if (0 == para.Inlines.Count)
para.Inlines.Add (new LineBreak());
doc.Blocks.Add (para); doc.Blocks.Add (para);
return doc; para = new Paragraph();
}
} }
if (para.Inlines.Count > 0)
doc.Blocks.Add (para);
return doc;
} }
} }