123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589 |
- using FastReport.Engine;
- using FastReport.Forms;
- using FastReport.Preview;
- using FastReport.Utils;
- using System;
- using System.ComponentModel;
- using System.Drawing;
- using System.Drawing.Printing;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace FastReport
- {
- partial class Report
- {
- #region Private Fields
- private int tickCount;
- private EmailSettings emailSettings;
- private FastReport.Preview.PreviewControl preview;
- private BaseForm previewForm;
- private PrintSettings printSettings;
- private bool isPreviewing;
- #endregion Private Fields
- #region Public Properties
- /// <summary>
- /// Gets the email settings such as recipients, subject, message body.
- /// </summary>
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- [SRCategory("Email")]
- public EmailSettings EmailSettings
- {
- get { return emailSettings; }
- }
- /// <summary>
- /// Gets or sets the report preview control.
- /// </summary>
- /// <remarks>
- /// 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 <b>Preview</b> property to this control.
- /// </remarks>
- [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- public Preview.PreviewControl Preview
- {
- get { return preview; }
- set
- {
- preview = value;
- if (value != null)
- value.SetReport(this);
- }
- }
- /// <summary>
- /// Gets the print settings such as printer name, copies, pages to print etc.
- /// </summary>
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- [SRCategory("Print")]
- public PrintSettings PrintSettings
- {
- get { return printSettings; }
- }
- /// <summary>
- ///
- /// </summary>
- [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- public bool IsPreviewing
- {
- get { return isPreviewing; }
- }
- #endregion Public Properties
- #region Public Methods
- /// <summary>
- /// Prepares the report and prints it.
- /// </summary>
- public void Print()
- {
- if (Prepare())
- PrintPrepared();
- }
- /// <summary>
- /// Prints the report with the "Print" dialog.
- /// Report should be prepared using the <see cref="Prepare()"/> method.
- /// </summary>
- public void PrintPrepared()
- {
- if (PreparedPages != null)
- PreparedPages.Print();
- }
- /// <summary>
- /// Prints the report without the "Print" dialog.
- /// Report should be prepared using the <see cref="Prepare()"/> method.
- /// </summary>
- /// <param name="printerSettings">Printer-specific settings.</param>
- /// <example>
- /// Use the following code if you want to show the "Print" dialog, then print:
- /// <code>
- /// if (report.Prepare())
- /// {
- /// PrinterSettings printerSettings = null;
- /// if (report.ShowPrintDialog(out printerSettings))
- /// {
- /// report.PrintPrepared(printerSettings);
- /// }
- /// }
- /// </code>
- /// </example>
- public void PrintPrepared(PrinterSettings printerSettings)
- {
- if (PreparedPages != null)
- PreparedPages.Print(printerSettings, 1);
- }
- /// <summary>
- /// Prepares the report and shows it in the preview window.
- /// </summary>
- public void Show()
- {
- Show(true, null);
- }
- /// <summary>
- /// Prepares the report and shows it in the preview window.
- /// </summary>
- /// <param name="modal">A value that specifies whether the preview window should be modal.</param>
- public void Show(bool modal)
- {
- Show(modal, null);
- }
- /// <summary>
- /// Prepares the report and shows it in the preview window.
- /// </summary>
- /// <param name="modal">A value that specifies whether the preview window should be modal.</param>
- /// <param name="owner">The owner of the preview window.</param>
- public void Show(bool modal, IWin32Window owner)
- {
- if (Prepare())
- ShowPrepared(modal, null, owner);
- else if (Preview != null)
- {
- Preview.Clear();
- Preview.Refresh();
- }
- }
- /// <summary>
- /// Prepares the report and shows it in the preview window.
- /// </summary>
- /// <param name="mdiParent">The main MDI form which will be a parent for the preview window.</param>
- public void Show(Form mdiParent)
- {
- if (Prepare())
- ShowPrepared(false, mdiParent, null);
- else if (Preview != null)
- {
- Preview.Clear();
- Preview.Refresh();
- }
- }
- /// <summary>
- /// Previews the report. The report should be prepared using the <see cref="Prepare()"/> method.
- /// </summary>
- public void ShowPrepared()
- {
- ShowPrepared(true);
- }
- /// <summary>
- /// Previews the prepared report.
- /// </summary>
- /// <param name="modal">A value that specifies whether the preview window should be modal.</param>
- public void ShowPrepared(bool modal)
- {
- ShowPrepared(modal, null, null);
- }
- /// <summary>
- /// Previews the prepared report.
- /// </summary>
- /// <param name="modal">A value that specifies whether the preview window should be modal.</param>
- /// <param name="owner">The owner of the preview window.</param>
- public void ShowPrepared(bool modal, IWin32Window owner)
- {
- ShowPrepared(modal, null, owner);
- }
- /// <summary>
- /// Previews the prepared report.
- /// </summary>
- /// <param name="mdiParent">The main MDI form which will be a parent for the preview window.</param>
- public void ShowPrepared(Form mdiParent)
- {
- ShowPrepared(false, mdiParent, null);
- }
- /// <summary>
- /// Shows the "Print" dialog.
- /// </summary>
- /// <param name="printerSettings">Printer-specific settings.</param>
- /// <returns><b>true</b> if the dialog was closed by "Print" button.</returns>
- /// <example>
- /// Use the following code if you want to show the "Print" dialog, then print:
- /// <code>
- /// if (report.Prepare())
- /// {
- /// PrinterSettings printerSettings = null;
- /// if (report.ShowPrintDialog(out printerSettings))
- /// {
- /// report.PrintPrepared(printerSettings);
- /// }
- /// }
- /// </code>
- /// </example>
- 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 Async Prepare/Show methods
- /* proposed usage:
- try
- {
- if (AsyncPrepareStart()) // startup, compile, run dialogs. May throw a compiler exception
- AsyncPrepareRun(); // run report pages (may be run pseudo async way)
- }
- finally
- {
- AsyncPrepareFinish(); // cleanup
- }
- */
- internal async Task<bool> AsyncPrepareStart()
- {
- SetRunning(true);
- ClearPreparedPages();
- SetPreparedPages(new Preview.PreparedPages(this));
- engine = new ReportEngine(this);
- StartPerformanceCounter();
- Compile();
- Engine.RunPhase1();
- // WPF: dialogs should be run here, before we set up modal preview window (deadlock issue)
- return await Engine.RunDialogsAsync();
- }
- internal void AsyncPrepareRun()
- {
- Engine.RunPhase2(false, null);
- }
- internal void AsyncPrepareFinish()
- {
- Engine.RunFinished();
- StopPerformanceCounter();
- SetRunning(false);
- if (Config.ReportSettings.ShowPerformance)
- Preview?.ShowPerformance(String.Format(Res.Get("Messages,Performance"), tickCount));
- if (ReportInfo.SavePreviewPicture && PreparedPages.Count > 0)
- SavePreviewPicture();
- }
- /// <summary>
- /// Prepares the report and shows it in the provided PreviewControl (async way).
- /// </summary>
- /// <param name="preview">The preview control.</param>
- public async Task PrepareAsync(PreviewControl preview)
- {
- if (preview == null)
- throw new ArgumentNullException(nameof(preview));
- bool saveProgress = Config.ReportSettings.ShowProgress;
- Config.ReportSettings.ShowProgress = false;
- var saveButtons = preview.Buttons;
- bool finished = false;
- try
- {
- if (await AsyncPrepareStart()) // startup, compile, run dialogs. May throw a compiler exception
- {
- // setup preview control
- preview.SetAsyncReportRunning();
- preview.Buttons |= PreviewButtons.Close;
- Preview = preview;
- SetupPreviewControl();
- preview.AsyncReportStart();
- var tcs = new System.Threading.Tasks.TaskCompletionSource<bool>();
- var timer = new Timer() { Interval = 50 };
- timer.Tick += (s, args) =>
- {
- timer.Dispose();
- try
- {
- AsyncPrepareRun(); // run report pages (pseudo async way). May throw a runtime exception
- }
- finally
- {
- AsyncPrepareFinish(); // cleanup
- preview.AsyncReportFinish();
- finished = true;
- tcs.SetResult(true);
- }
- };
- timer.Start();
- Config.PreviewSettings.OnPreviewOpened(Preview);
- // wait for report to finish
- await tcs.Task;
- }
- }
- finally
- {
- if (!finished)
- AsyncPrepareFinish(); // cleanup
- Config.ReportSettings.ShowProgress = saveProgress;
- preview.Buttons = saveButtons;
- }
- }
- private async Task ShowAsync(bool modal, Form mdiParent, IWin32Window owner)
- {
- bool saveProgress = Config.ReportSettings.ShowProgress;
- Config.ReportSettings.ShowProgress = false;
- bool finished = false;
- try
- {
- if (await AsyncPrepareStart()) // startup, compile, run dialogs. May throw a compiler exception
- {
- // create preview form
- previewForm = CreatePreviewForm();
- previewForm.MdiParent = mdiParent;
- // setup preview control
- var preview = Preview;
- preview.SetAsyncReportRunning();
- preview.Buttons |= PreviewButtons.Close;
- SetupPreviewControl();
- preview.AsyncReportStart();
- previewForm.Shown += (sender, e) =>
- {
- var timer = new Timer() { Interval = 50 };
- timer.Tick += (s, args) =>
- {
- timer.Dispose();
- try
- {
- AsyncPrepareRun(); // run report pages (pseudo async way). May throw a runtime exception
- }
- finally
- {
- AsyncPrepareFinish(); // cleanup
- preview.AsyncReportFinish();
- finished = true;
- }
- };
- timer.Start();
- };
- #if AVALONIA
- await ShowPreviewFormAsync(modal, mdiParent, owner);
- #else
- ShowPreviewForm(modal, mdiParent, owner);
- #endif
- }
- }
- finally
- {
- if (!finished)
- AsyncPrepareFinish(); // cleanup
- Config.ReportSettings.ShowProgress = saveProgress;
- finished = true;
- }
- }
- /// <summary>
- /// Prepares the report and shows it in the preview window (async way).
- /// </summary>
- public async Task ShowAsync() => await ShowAsync(true, null);
- /// <summary>
- /// Prepares the report and shows it in the preview window (async way).
- /// </summary>
- /// <param name="modal">A value that specifies whether the preview window should be modal.</param>
- public async Task ShowAsync(bool modal) => await ShowAsync(modal, null);
- /// <summary>
- /// Prepares the report and shows it in the preview window (async way).
- /// </summary>
- /// <param name="modal">A value that specifies whether the preview window should be modal.</param>
- /// <param name="owner">The owner of the preview window.</param>
- public async Task ShowAsync(bool modal, IWin32Window owner) => await ShowAsync(modal, null, owner);
- /// <summary>
- /// Prepares the report and shows it in the preview window (async way).
- /// </summary>
- /// <param name="mdiParent">The main MDI form which will be a parent for the preview window.</param>
- public async Task ShowAsync(Form mdiParent) => await ShowAsync(false, mdiParent, null);
- #endregion
- #region Private Methods
- private void StartPerformanceCounter()
- {
- tickCount = Environment.TickCount;
- }
- private void StopPerformanceCounter()
- {
- tickCount = Environment.TickCount - tickCount;
- }
- 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 = CreatePreviewForm();
- }
- SetupPreviewControl();
- 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
- {
- }
- }
- if (ReportInfo.SavePreviewPicture && PreparedPages.Count > 0)
- SavePreviewPicture();
- ShowPreviewForm(modal, mdiParent, owner);
- }
- private PreviewForm CreatePreviewForm()
- {
- var previewForm = new PreviewForm();
- 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.Preview;
- Preview.UIStyle = Config.UIStyle;
- Preview.FastScrolling = Config.PreviewSettings.FastScrolling;
- Preview.Buttons = Config.PreviewSettings.Buttons;
- Preview.SaveInitialDirectory = Config.PreviewSettings.SaveInitialDirectory;
- return previewForm;
- }
- private void ShowPreviewForm(bool modal, Form mdiParent, IWin32Window owner)
- {
- Config.PreviewSettings.OnPreviewOpened(Preview);
- isPreviewing = true;
- if (previewForm != null && !previewForm.Visible)
- {
- previewForm.MdiParent = mdiParent;
- if (modal)
- {
- previewForm.ShowDialog(owner);
- DisposePreviewForm();
- }
- else
- {
- previewForm.FormClosed += new FormClosedEventHandler(OnClosePreview);
- if (mdiParent == null)
- previewForm.Show(owner);
- else
- previewForm.Show();
- }
- isPreviewing = false;
- }
- }
- #if AVALONIA
- private async Task ShowPreviewFormAsync(bool modal, Form mdiParent, IWin32Window owner)
- {
- Config.PreviewSettings.OnPreviewOpened(Preview);
- isPreviewing = true;
- if (previewForm != null && !previewForm.Visible)
- {
- previewForm.MdiParent = mdiParent;
- if (modal)
- {
- await previewForm.ShowDialogAsync(owner);
- DisposePreviewForm();
- }
- else
- {
- previewForm.FormClosed += new FormClosedEventHandler(OnClosePreview);
- if (mdiParent == null)
- previewForm.Show(owner);
- else
- previewForm.Show();
- }
- isPreviewing = false;
- }
- }
- #endif
- internal void SetupPreviewControl()
- {
- Preview.ClearTabsExceptFirst();
- if (PreparedPages != null)
- Preview.AddPreviewTab(this, GetReportName, null, true);
- }
- #endregion Private Methods
- }
- }
|