using FastReport.Forms;
using FastReport.Utils;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
namespace FastReport
{
partial class Report
{
#region Private Fields
private EmailSettings emailSettings;
private FastReport.Preview.PreviewControl preview;
private BaseForm previewForm;
private PrintSettings printSettings;
private bool isPreviewing;
#endregion Private Fields
#region Public Properties
///
/// Gets the email settings such as recipients, subject, message body.
///
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[SRCategory("Email")]
public EmailSettings EmailSettings
{
get { return emailSettings; }
}
///
/// Gets or sets the report preview control.
///
///
/// Use this property to attach a custom preview to your report. To do this, place the PreviewControl
/// control to your form and set the report's Preview property to this control.
///
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Preview.PreviewControl Preview
{
get { return preview; }
set
{
preview = value;
if (value != null)
value.SetReport(this);
}
}
///
/// Gets the print settings such as printer name, copies, pages to print etc.
///
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[SRCategory("Print")]
public PrintSettings PrintSettings
{
get { return printSettings; }
}
///
///
///
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public bool IsPreviewing
{
get { return isPreviewing; }
}
#endregion Public Properties
#region Public Methods
///
/// Prepares the report and prints it.
///
public void Print()
{
if (Prepare())
PrintPrepared();
}
///
/// Prints the report with the "Print" dialog.
/// Report should be prepared using the method.
///
public void PrintPrepared()
{
if (PreparedPages != null)
PreparedPages.Print();
}
///
/// Prints the report without the "Print" dialog.
/// Report should be prepared using the method.
///
/// Printer-specific settings.
///
/// Use the following code if you want to show the "Print" dialog, then print:
///
/// if (report.Prepare())
/// {
/// PrinterSettings printerSettings = null;
/// if (report.ShowPrintDialog(out printerSettings))
/// {
/// report.PrintPrepared(printerSettings);
/// }
/// }
///
///
public void PrintPrepared(PrinterSettings printerSettings)
{
if (PreparedPages != null)
PreparedPages.Print(printerSettings, 1);
}
///
/// Prepares the report and shows it in the preview window.
///
public void Show()
{
Show(true, null);
}
///
/// Prepares the report and shows it in the preview window.
///
/// A value that specifies whether the preview window should be modal.
public void Show(bool modal)
{
Show(modal, null);
}
///
/// Prepares the report and shows it in the preview window.
///
/// A value that specifies whether the preview window should be modal.
/// The owner of the preview window.
public void Show(bool modal, IWin32Window owner)
{
if (Prepare())
ShowPrepared(modal, null, owner);
else if (Preview != null)
{
Preview.Clear();
Preview.Refresh();
}
}
///
/// Prepares the report and shows it in the preview window.
///
/// The main MDI form which will be a parent for the preview window.
public void Show(Form mdiParent)
{
if (Prepare())
ShowPrepared(false, mdiParent, null);
else if (Preview != null)
{
Preview.Clear();
Preview.Refresh();
}
}
///
/// Previews the report. The report should be prepared using the method.
///
public void ShowPrepared()
{
ShowPrepared(true);
}
///
/// Previews the prepared report.
///
/// A value that specifies whether the preview window should be modal.
public void ShowPrepared(bool modal)
{
ShowPrepared(modal, null, null);
}
///
/// Previews the prepared report.
///
/// A value that specifies whether the preview window should be modal.
/// The owner of the preview window.
public void ShowPrepared(bool modal, IWin32Window owner)
{
ShowPrepared(modal, null, owner);
}
///
/// Previews the prepared report.
///
/// The main MDI form which will be a parent for the preview window.
public void ShowPrepared(Form mdiParent)
{
ShowPrepared(false, mdiParent, null);
}
///
/// Shows the "Print" dialog.
///
/// Printer-specific settings.
/// true if the dialog was closed by "Print" button.
///
/// Use the following code if you want to show the "Print" dialog, then print:
///
/// if (report.Prepare())
/// {
/// PrinterSettings printerSettings = null;
/// if (report.ShowPrintDialog(out printerSettings))
/// {
/// report.PrintPrepared(printerSettings);
/// }
/// }
///
///
public bool ShowPrintDialog(out PrinterSettings printerSettings)
{
printerSettings = null;
using (PrinterSetupForm dialog = new PrinterSetupForm())
{
dialog.Report = this;
dialog.PrintDialog = true;
if (dialog.ShowDialog() != DialogResult.OK)
return false;
printerSettings = dialog.PrinterSettings;
}
return true;
}
#endregion Public Methods
#region Private Methods
private void ClearPreparedPages()
{
if (preview != null)
preview.ClearTabsExceptFirst();
else
if (preparedPages != null)
preparedPages.Clear();
}
private void DisposePreviewForm()
{
previewForm.Dispose();
previewForm = null;
preview = null;
}
private void OnClosePreview(object sender, FormClosedEventArgs e)
{
DisposePreviewForm();
}
private void SavePreviewPicture()
{
ReportPage page = PreparedPages.GetCachedPage(0);
float pageWidth = page.WidthInPixels;
float pageHeight = page.HeightInPixels;
float ratio = ReportInfo.PreviewPictureRatio;
ReportInfo.Picture = new Bitmap((int)Math.Round(pageWidth * ratio), (int)Math.Round(pageHeight * ratio));
using (Graphics g = Graphics.FromImage(ReportInfo.Picture))
{
FRPaintEventArgs args = new FRPaintEventArgs(g, ratio, ratio, GraphicCache);
page.Draw(args);
}
}
private void ShowPrepared(bool modal, Form mdiParent, IWin32Window owner)
{
// create preview form
if (Preview == null)
{
previewForm = new PreviewForm();
previewForm.MdiParent = mdiParent;
previewForm.ShowInTaskbar = Config.PreviewSettings.ShowInTaskbar;
if (Config.PreviewSettings.TopMost)
previewForm.TopMost = true;
previewForm.Icon = Config.PreviewSettings.Icon;
if (String.IsNullOrEmpty(Config.PreviewSettings.Text))
{
previewForm.Text = String.IsNullOrEmpty(ReportInfo.Name) ? "" : ReportInfo.Name + " - ";
previewForm.Text += Res.Get("Preview");
}
else
previewForm.Text = Config.PreviewSettings.Text;
Preview = (previewForm as PreviewForm).Preview;
Preview.UIStyle = Config.UIStyle;
Preview.FastScrolling = Config.PreviewSettings.FastScrolling;
Preview.Buttons = Config.PreviewSettings.Buttons;
Preview.Exports = Config.PreviewSettings.Exports;
Preview.Clouds = Config.PreviewSettings.Clouds;
Preview.SaveInitialDirectory = Config.PreviewSettings.SaveInitialDirectory;
}
if (Config.ReportSettings.ShowPerformance)
{
try
{
// in case the format string is wrong, use try/catch
Preview.ShowPerformance(String.Format(Res.Get("Messages,Performance"), tickCount));
}
catch
{
}
}
Preview.ClearTabsExceptFirst();
if (PreparedPages != null)
Preview.AddPreviewTab(this, GetReportName, null, true);
Config.PreviewSettings.OnPreviewOpened(Preview);
if (ReportInfo.SavePreviewPicture && PreparedPages.Count > 0)
SavePreviewPicture();
isPreviewing = true;
if (previewForm != null && !previewForm.Visible)
{
if (modal)
{
previewForm.ShowDialog(owner);
DisposePreviewForm();
}
else
{
previewForm.FormClosed += new FormClosedEventHandler(OnClosePreview);
if (mdiParent == null)
previewForm.Show(owner);
else
previewForm.Show();
}
isPreviewing = false;
}
}
#endregion Private Methods
}
}