Browse Source
git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@5419 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61pull/1/head
20 changed files with 315 additions and 21 deletions
@ -0,0 +1,67 @@ |
|||||||
|
// <file>
|
||||||
|
// <copyright see="prj:///doc/copyright.txt"/>
|
||||||
|
// <license see="prj:///doc/license.txt"/>
|
||||||
|
// <owner name="Daniel Grunwald"/>
|
||||||
|
// <version>$Revision$</version>
|
||||||
|
// </file>
|
||||||
|
|
||||||
|
using System; |
||||||
|
using System.IO; |
||||||
|
using System.IO.Packaging; |
||||||
|
using System.Printing; |
||||||
|
using System.Windows; |
||||||
|
using System.Windows.Controls; |
||||||
|
using System.Windows.Documents; |
||||||
|
using System.Windows.Xps; |
||||||
|
using System.Windows.Xps.Packaging; |
||||||
|
|
||||||
|
using ICSharpCode.AvalonEdit.Document; |
||||||
|
using ICSharpCode.AvalonEdit.Highlighting; |
||||||
|
|
||||||
|
namespace ICSharpCode.AvalonEdit.AddIn |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Helps printing documents.
|
||||||
|
/// </summary>
|
||||||
|
public static class DocumentPrinter |
||||||
|
{ |
||||||
|
public static Block ConvertTextDocumentToBlock(TextDocument document, IHighlighter highlighter) |
||||||
|
{ |
||||||
|
if (document == null) |
||||||
|
throw new ArgumentNullException("document"); |
||||||
|
// Table table = new Table();
|
||||||
|
// table.Columns.Add(new TableColumn { Width = GridLength.Auto });
|
||||||
|
// table.Columns.Add(new TableColumn { Width = new GridLength(1, GridUnitType.Star) });
|
||||||
|
// TableRowGroup trg = new TableRowGroup();
|
||||||
|
// table.RowGroups.Add(trg);
|
||||||
|
Paragraph p = new Paragraph(); |
||||||
|
foreach (DocumentLine line in document.Lines) { |
||||||
|
int lineNumber = line.LineNumber; |
||||||
|
// TableRow row = new TableRow();
|
||||||
|
// trg.Rows.Add(row);
|
||||||
|
// row.Cells.Add(new TableCell(new Paragraph(new Run(lineNumber.ToString()))) { TextAlignment = TextAlignment.Right });
|
||||||
|
HighlightedInlineBuilder inlineBuilder = new HighlightedInlineBuilder(document.GetText(line)); |
||||||
|
if (highlighter != null) { |
||||||
|
HighlightedLine highlightedLine = highlighter.HighlightLine(lineNumber); |
||||||
|
int lineStartOffset = line.Offset; |
||||||
|
foreach (HighlightedSection section in highlightedLine.Sections) |
||||||
|
inlineBuilder.SetHighlighting(section.Offset - lineStartOffset, section.Length, section.Color); |
||||||
|
} |
||||||
|
// Paragraph p = new Paragraph();
|
||||||
|
// row.Cells.Add(new TableCell(p));
|
||||||
|
p.Inlines.AddRange(inlineBuilder.CreateRuns()); |
||||||
|
p.Inlines.Add(new LineBreak()); |
||||||
|
} |
||||||
|
return p; |
||||||
|
} |
||||||
|
|
||||||
|
public static FlowDocument CreateFlowDocumentForEditor(CodeEditorView editor) |
||||||
|
{ |
||||||
|
IHighlighter highlighter = editor.Adapter.GetService(typeof(IHighlighter)) as IHighlighter; |
||||||
|
FlowDocument doc = new FlowDocument(ConvertTextDocumentToBlock(editor.Document, highlighter)); |
||||||
|
doc.FontFamily = editor.FontFamily; |
||||||
|
doc.FontSize = editor.FontSize; |
||||||
|
return doc; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,105 @@ |
|||||||
|
// <file>
|
||||||
|
// <copyright see="prj:///doc/copyright.txt"/>
|
||||||
|
// <license see="prj:///doc/license.txt"/>
|
||||||
|
// <owner name="Daniel Grunwald"/>
|
||||||
|
// <version>$Revision$</version>
|
||||||
|
// </file>
|
||||||
|
|
||||||
|
using System; |
||||||
|
using System.IO; |
||||||
|
using System.IO.Packaging; |
||||||
|
using System.Linq; |
||||||
|
using System.Windows.Controls; |
||||||
|
using System.Windows.Documents; |
||||||
|
using System.Windows.Input; |
||||||
|
using System.Windows.Media; |
||||||
|
using System.Windows.Xps; |
||||||
|
using System.Windows.Xps.Packaging; |
||||||
|
|
||||||
|
using ICSharpCode.SharpDevelop.Gui; |
||||||
|
|
||||||
|
namespace ICSharpCode.AvalonEdit.AddIn |
||||||
|
{ |
||||||
|
public class PrintPreviewViewContent : AbstractViewContent |
||||||
|
{ |
||||||
|
static readonly PrintDialog printDialog = new PrintDialog(); |
||||||
|
|
||||||
|
public static PrintDialog PrintDialog { |
||||||
|
get { return printDialog; } |
||||||
|
} |
||||||
|
|
||||||
|
Action cleanup; |
||||||
|
DocumentViewer viewer = new DocumentViewer(); |
||||||
|
|
||||||
|
public PrintPreviewViewContent() |
||||||
|
{ |
||||||
|
SetLocalizedTitle("${res:Global.Preview}"); |
||||||
|
viewer.CommandBindings.Add(new CommandBinding(ApplicationCommands.Print, OnPrint)); |
||||||
|
} |
||||||
|
|
||||||
|
public IDocumentPaginatorSource Document { |
||||||
|
get { return viewer.Document; } |
||||||
|
set { |
||||||
|
if (cleanup != null) { |
||||||
|
cleanup(); |
||||||
|
cleanup = null; |
||||||
|
} |
||||||
|
|
||||||
|
if (value is FlowDocument) { |
||||||
|
// DocumentViewer does not support FlowDocument, so we'll convert it
|
||||||
|
MemoryStream xpsStream = new MemoryStream(); |
||||||
|
Package package = Package.Open(xpsStream, FileMode.Create, FileAccess.ReadWrite); |
||||||
|
string packageUriString = "memorystream://data.xps"; |
||||||
|
PackageStore.AddPackage(new Uri(packageUriString), package); |
||||||
|
|
||||||
|
XpsDocument xpsDocument = new XpsDocument(package, CompressionOption.Normal, packageUriString); |
||||||
|
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDocument); |
||||||
|
|
||||||
|
writer.Write(value.DocumentPaginator); |
||||||
|
viewer.Document = xpsDocument.GetFixedDocumentSequence(); |
||||||
|
cleanup = delegate { |
||||||
|
viewer.Document = null; |
||||||
|
xpsDocument.Close(); |
||||||
|
package.Close(); |
||||||
|
PackageStore.RemovePackage(new Uri(packageUriString)); |
||||||
|
}; |
||||||
|
} else { |
||||||
|
viewer.Document = value; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public override void Dispose() |
||||||
|
{ |
||||||
|
base.Dispose(); |
||||||
|
if (cleanup != null) { |
||||||
|
cleanup(); |
||||||
|
cleanup = null; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public string Description { get; set; } |
||||||
|
|
||||||
|
public override object Control { |
||||||
|
get { return viewer; } |
||||||
|
} |
||||||
|
|
||||||
|
void OnPrint(object sender, ExecutedRoutedEventArgs e) |
||||||
|
{ |
||||||
|
viewer.Print(); |
||||||
|
} |
||||||
|
|
||||||
|
public static void ShowDocument(IDocumentPaginatorSource document, string description) |
||||||
|
{ |
||||||
|
PrintPreviewViewContent vc = WorkbenchSingleton.Workbench.ViewContentCollection.OfType<PrintPreviewViewContent>().FirstOrDefault(); |
||||||
|
if (vc != null) { |
||||||
|
vc.WorkbenchWindow.SelectWindow(); |
||||||
|
} else { |
||||||
|
vc = new PrintPreviewViewContent(); |
||||||
|
WorkbenchSingleton.Workbench.ShowView(vc); |
||||||
|
} |
||||||
|
vc.Document = document; |
||||||
|
vc.Description = description; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,39 @@ |
|||||||
|
// <file>
|
||||||
|
// <copyright see="prj:///doc/copyright.txt"/>
|
||||||
|
// <license see="prj:///doc/license.txt"/>
|
||||||
|
// <owner name="Daniel Grunwald"/>
|
||||||
|
// <version>$Revision$</version>
|
||||||
|
// </file>
|
||||||
|
|
||||||
|
using System; |
||||||
|
using ICSharpCode.AvalonEdit.Document; |
||||||
|
using ICSharpCode.AvalonEdit.Utils; |
||||||
|
|
||||||
|
namespace ICSharpCode.AvalonEdit.Highlighting |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Represents a highlighted document.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>This interface is used by the <see cref="HighlightingColorizer"/> to register the highlighter as a TextView service.</remarks>
|
||||||
|
public interface IHighlighter |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Gets the underlying text document.
|
||||||
|
/// </summary>
|
||||||
|
TextDocument Document { get; } |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the span stack at the end of the specified line.
|
||||||
|
/// -> GetSpanStack(1) returns the spans at the start of the second line.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>GetSpanStack(0) is valid and will always return the empty stack.</remarks>
|
||||||
|
ImmutableStack<HighlightingSpan> GetSpanStack(int lineNumber); |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Highlights the specified document line.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="lineNumber">The line to highlight.</param>
|
||||||
|
/// <returns>A <see cref="HighlightedLine"/> line object that represents the highlighted sections.</returns>
|
||||||
|
HighlightedLine HighlightLine(int lineNumber); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue