CPF/CPF.ReoGrid/Print/PrintSession.cs
2024-06-24 10:15:59 +08:00

250 lines
7.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using CPF.Drawing;
using CPF.ReoGrid.Core;
using CPF.ReoGrid.Drawing;
using CPF.ReoGrid.Rendering;
using CPF.ReoGrid.Views;
namespace CPF.ReoGrid.Print
{
public class PrintSession : IDisposable
{
public ICollection<Worksheet> Worksheets
{
get
{
bool flag = this.worksheetCollection == null;
if (flag)
{
this.worksheetCollection = new PrintSessionWorksheetCollection(this);
}
return this.worksheetCollection;
}
}
internal int CurrentWorksheetIndex { get; set; }
internal Worksheet CurrentWorksheet { get; set; }
internal int CurrentRowIndex { get; set; }
internal int CurrentColIndex { get; set; }
internal Rect CurrentPaperBounds { get; set; }
internal PrintSettings CurrentPrintSettings { get; set; }
internal PrintSession()
{
this.CurrentWorksheetIndex = -1;
}
public bool IsPrinting { get; internal set; }
internal CellDrawingContext DrawingContext { get; set; }
internal SheetViewport PrintViewport { get; set; }
internal void NextWorksheet()
{
int currentWorksheetIndex = this.CurrentWorksheetIndex;
this.CurrentWorksheetIndex = currentWorksheetIndex + 1;
bool flag = this.CurrentWorksheetIndex < 0 || this.CurrentWorksheetIndex >= this.worksheets.Count;
if (flag)
{
this.CurrentWorksheet = null;
}
else
{
this.CurrentWorksheet = this.worksheets[this.CurrentWorksheetIndex];
bool flag2 = this.CurrentWorksheet == null;
if (!flag2)
{
Worksheet currentWorksheet = this.CurrentWorksheet;
bool flag3 = currentWorksheet.pageBreakRows == null || currentWorksheet.pageBreakCols == null || currentWorksheet.pageBreakRows.Count == 0 || currentWorksheet.pageBreakCols.Count == 0;
if (flag3)
{
currentWorksheet.AutoSplitPage();
}
bool flag4 = currentWorksheet.pageBreakRows == null || currentWorksheet.pageBreakRows == null || currentWorksheet.pageBreakRows.Count == 0 || currentWorksheet.pageBreakRows.Count == 0;
if (flag4)
{
this.NextWorksheet();
}
else
{
this.DrawingContext = new CellDrawingContext(currentWorksheet, DrawMode.Print);
this.DrawingContext.FullCellClip = true;
this.CurrentPrintSettings = currentWorksheet.PrintSettings;
bool flag5 = this.CurrentPrintSettings == null;
if (flag5)
{
this.CurrentPrintSettings = new PrintSettings();
}
this.CurrentColIndex = 0;
this.CurrentRowIndex = 0;
this.CurrentPaperBounds = currentWorksheet.GetPaperPrintBounds();
PrintSettings printSettings = currentWorksheet.PrintSettings;
}
}
}
}
internal void NextPage(DrawingContext pg)
{
this.hasMorePages = false;
Worksheet sheet = this.CurrentWorksheet;
bool flag = sheet == null;
if (!flag)
{
bool flag2 = this.CurrentRowIndex >= sheet.pageBreakRows.Count && this.CurrentColIndex >= sheet.pageBreakCols.Count;
if (!flag2)
{
int num = sheet.pageBreakRows[this.CurrentRowIndex];
int num2 = sheet.pageBreakCols[this.CurrentColIndex];
int num3 = sheet.pageBreakRows[this.CurrentRowIndex + 1];
int num4 = sheet.pageBreakCols[this.CurrentColIndex + 1];
PrintPageOrder pageOrder = this.CurrentPrintSettings.PageOrder;
PrintPageOrder printPageOrder = pageOrder;
if (printPageOrder == PrintPageOrder.DownThenOver || printPageOrder != PrintPageOrder.OverThenDown)
{
bool flag3 = this.CurrentRowIndex < sheet.pageBreakRows.Count - 2;
if (flag3)
{
int num5 = this.CurrentRowIndex;
this.CurrentRowIndex = num5 + 1;
this.hasMorePages = true;
}
else
{
bool flag4 = this.CurrentColIndex < sheet.pageBreakCols.Count - 2;
if (flag4)
{
this.CurrentRowIndex = 0;
int num5 = this.CurrentColIndex;
this.CurrentColIndex = num5 + 1;
this.hasMorePages = true;
}
else
{
this.hasMorePages = false;
}
}
}
else
{
bool flag5 = this.CurrentColIndex < sheet.pageBreakCols.Count - 2;
if (flag5)
{
int num5 = this.CurrentColIndex;
this.CurrentColIndex = num5 + 1;
this.hasMorePages = true;
}
else
{
bool flag6 = this.CurrentRowIndex < sheet.pageBreakRows.Count - 2;
if (flag6)
{
this.CurrentColIndex = 0;
int num5 = this.CurrentRowIndex;
this.CurrentRowIndex = num5 + 1;
this.hasMorePages = true;
}
else
{
this.hasMorePages = false;
}
}
}
bool flag7 = this.DrawingContext.Graphics == null;
if (!flag7)
{
this.DrawingContext.Graphics.Reset();
}
IRenderer ig = this.DrawingContext.Renderer;
ig.PlatformGraphics = pg;
Debug.WriteLine(string.Format("print page {0,3},{1,3} - {2,3},{3,3}", new object[]
{
num,
num2,
num3,
num4
}));
GridRegion visibleRegion = new GridRegion(num, num2, num3, num4);
bool flag8 = this.printViewportController == null;
if (flag8)
{
this.printViewportController = new ViewportController(sheet);
}
else
{
this.printViewportController.worksheet = sheet;
}
this.PrintViewport = new SheetViewport(this.printViewportController);
this.PrintViewport.Bounds = this.CurrentPaperBounds;
sheet.IterateCells(visibleRegion.ToRange(), delegate(int _unused_r, int _unused_c, Cell cell)
{
sheet.UpdateCellTextBounds(ig, cell, DrawMode.Print, this.CurrentPrintSettings.PageScaling, UpdateFontReason.ScaleChanged);
return true;
});
this.PrintViewport.VisibleRegion = visibleRegion;
this.PrintViewport.ScaleFactor = this.CurrentPrintSettings.PageScaling;
Viewport printViewport = this.PrintViewport;
float num6 = (float)sheet.cols[num2].Left;
float num7 = (float)sheet.rows[num].Top;
printViewport.ViewStart = new Point(ref num6, ref num7);
this.PrintViewport.UpdateView();
this.PrintViewport.Draw(this.DrawingContext);
bool showMargins = this.CurrentPrintSettings.ShowMargins;
if (showMargins)
{
Rect currentPaperBounds = this.CurrentPaperBounds;
CPFPen pen = this.DrawingContext.Renderer.GetPen(Color.Gray);
CPFPen obj = pen;
lock (obj)
{
ig.DrawLine(pen, currentPaperBounds.X - 50f, currentPaperBounds.Y, currentPaperBounds.X, currentPaperBounds.Y);
ig.DrawLine(pen, currentPaperBounds.Right + 50f, currentPaperBounds.Y, currentPaperBounds.Right, currentPaperBounds.Y);
ig.DrawLine(pen, currentPaperBounds.X - 50f, currentPaperBounds.Bottom, currentPaperBounds.X, currentPaperBounds.Bottom);
ig.DrawLine(pen, currentPaperBounds.Right + 50f, currentPaperBounds.Bottom, currentPaperBounds.Right, currentPaperBounds.Bottom);
ig.DrawLine(pen, currentPaperBounds.X, currentPaperBounds.Y - 50f, currentPaperBounds.X, currentPaperBounds.Y);
ig.DrawLine(pen, currentPaperBounds.X, currentPaperBounds.Bottom + 50f, currentPaperBounds.X, currentPaperBounds.Bottom);
ig.DrawLine(pen, currentPaperBounds.Right, currentPaperBounds.Y - 50f, currentPaperBounds.Right, currentPaperBounds.Y);
ig.DrawLine(pen, currentPaperBounds.Right, currentPaperBounds.Bottom + 50f, currentPaperBounds.Right, currentPaperBounds.Bottom);
}
}
bool flag10 = !this.hasMorePages;
if (flag10)
{
this.NextWorksheet();
this.hasMorePages = (this.CurrentWorksheet != null);
}
}
}
}
internal void Init()
{
}
public void Dispose()
{
}
public void Print()
{
throw new NotImplementedException("CPF Print is not implemented yet. Try use Windows Form version to print document as XPS file.");
}
internal List<Worksheet> worksheets = new List<Worksheet>();
private PrintSessionWorksheetCollection worksheetCollection = null;
private ViewportController printViewportController;
internal bool hasMorePages = false;
}
}