Report.PreviewExt.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. using FastReport.Forms;
  2. using FastReport.Utils;
  3. using System;
  4. using System.ComponentModel;
  5. using System.Drawing;
  6. using System.Drawing.Printing;
  7. using System.Windows.Forms;
  8. namespace FastReport
  9. {
  10. partial class Report
  11. {
  12. #region Private Fields
  13. private EmailSettings emailSettings;
  14. private FastReport.Preview.PreviewControl preview;
  15. private BaseForm previewForm;
  16. private PrintSettings printSettings;
  17. private bool isPreviewing;
  18. #endregion Private Fields
  19. #region Public Properties
  20. /// <summary>
  21. /// Gets the email settings such as recipients, subject, message body.
  22. /// </summary>
  23. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  24. [SRCategory("Email")]
  25. public EmailSettings EmailSettings
  26. {
  27. get { return emailSettings; }
  28. }
  29. /// <summary>
  30. /// Gets or sets the report preview control.
  31. /// </summary>
  32. /// <remarks>
  33. /// Use this property to attach a custom preview to your report. To do this, place the PreviewControl
  34. /// control to your form and set the report's <b>Preview</b> property to this control.
  35. /// </remarks>
  36. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  37. public Preview.PreviewControl Preview
  38. {
  39. get { return preview; }
  40. set
  41. {
  42. preview = value;
  43. if (value != null)
  44. value.SetReport(this);
  45. }
  46. }
  47. /// <summary>
  48. /// Gets the print settings such as printer name, copies, pages to print etc.
  49. /// </summary>
  50. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  51. [SRCategory("Print")]
  52. public PrintSettings PrintSettings
  53. {
  54. get { return printSettings; }
  55. }
  56. /// <summary>
  57. ///
  58. /// </summary>
  59. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  60. public bool IsPreviewing
  61. {
  62. get { return isPreviewing; }
  63. }
  64. #endregion Public Properties
  65. #region Public Methods
  66. /// <summary>
  67. /// Prepares the report and prints it.
  68. /// </summary>
  69. public void Print()
  70. {
  71. if (Prepare())
  72. PrintPrepared();
  73. }
  74. /// <summary>
  75. /// Prints the report with the "Print" dialog.
  76. /// Report should be prepared using the <see cref="Prepare()"/> method.
  77. /// </summary>
  78. public void PrintPrepared()
  79. {
  80. if (PreparedPages != null)
  81. PreparedPages.Print();
  82. }
  83. /// <summary>
  84. /// Prints the report without the "Print" dialog.
  85. /// Report should be prepared using the <see cref="Prepare()"/> method.
  86. /// </summary>
  87. /// <param name="printerSettings">Printer-specific settings.</param>
  88. /// <example>
  89. /// Use the following code if you want to show the "Print" dialog, then print:
  90. /// <code>
  91. /// if (report.Prepare())
  92. /// {
  93. /// PrinterSettings printerSettings = null;
  94. /// if (report.ShowPrintDialog(out printerSettings))
  95. /// {
  96. /// report.PrintPrepared(printerSettings);
  97. /// }
  98. /// }
  99. /// </code>
  100. /// </example>
  101. public void PrintPrepared(PrinterSettings printerSettings)
  102. {
  103. if (PreparedPages != null)
  104. PreparedPages.Print(printerSettings, 1);
  105. }
  106. /// <summary>
  107. /// Prepares the report and shows it in the preview window.
  108. /// </summary>
  109. public void Show()
  110. {
  111. Show(true, null);
  112. }
  113. /// <summary>
  114. /// Prepares the report and shows it in the preview window.
  115. /// </summary>
  116. /// <param name="modal">A value that specifies whether the preview window should be modal.</param>
  117. public void Show(bool modal)
  118. {
  119. Show(modal, null);
  120. }
  121. /// <summary>
  122. /// Prepares the report and shows it in the preview window.
  123. /// </summary>
  124. /// <param name="modal">A value that specifies whether the preview window should be modal.</param>
  125. /// <param name="owner">The owner of the preview window.</param>
  126. public void Show(bool modal, IWin32Window owner)
  127. {
  128. if (Prepare())
  129. ShowPrepared(modal, null, owner);
  130. else if (Preview != null)
  131. {
  132. Preview.Clear();
  133. Preview.Refresh();
  134. }
  135. }
  136. /// <summary>
  137. /// Prepares the report and shows it in the preview window.
  138. /// </summary>
  139. /// <param name="mdiParent">The main MDI form which will be a parent for the preview window.</param>
  140. public void Show(Form mdiParent)
  141. {
  142. if (Prepare())
  143. ShowPrepared(false, mdiParent, null);
  144. else if (Preview != null)
  145. {
  146. Preview.Clear();
  147. Preview.Refresh();
  148. }
  149. }
  150. /// <summary>
  151. /// Previews the report. The report should be prepared using the <see cref="Prepare()"/> method.
  152. /// </summary>
  153. public void ShowPrepared()
  154. {
  155. ShowPrepared(true);
  156. }
  157. /// <summary>
  158. /// Previews the prepared report.
  159. /// </summary>
  160. /// <param name="modal">A value that specifies whether the preview window should be modal.</param>
  161. public void ShowPrepared(bool modal)
  162. {
  163. ShowPrepared(modal, null, null);
  164. }
  165. /// <summary>
  166. /// Previews the prepared report.
  167. /// </summary>
  168. /// <param name="modal">A value that specifies whether the preview window should be modal.</param>
  169. /// <param name="owner">The owner of the preview window.</param>
  170. public void ShowPrepared(bool modal, IWin32Window owner)
  171. {
  172. ShowPrepared(modal, null, owner);
  173. }
  174. /// <summary>
  175. /// Previews the prepared report.
  176. /// </summary>
  177. /// <param name="mdiParent">The main MDI form which will be a parent for the preview window.</param>
  178. public void ShowPrepared(Form mdiParent)
  179. {
  180. ShowPrepared(false, mdiParent, null);
  181. }
  182. /// <summary>
  183. /// Shows the "Print" dialog.
  184. /// </summary>
  185. /// <param name="printerSettings">Printer-specific settings.</param>
  186. /// <returns><b>true</b> if the dialog was closed by "Print" button.</returns>
  187. /// <example>
  188. /// Use the following code if you want to show the "Print" dialog, then print:
  189. /// <code>
  190. /// if (report.Prepare())
  191. /// {
  192. /// PrinterSettings printerSettings = null;
  193. /// if (report.ShowPrintDialog(out printerSettings))
  194. /// {
  195. /// report.PrintPrepared(printerSettings);
  196. /// }
  197. /// }
  198. /// </code>
  199. /// </example>
  200. public bool ShowPrintDialog(out PrinterSettings printerSettings)
  201. {
  202. printerSettings = null;
  203. using (PrinterSetupForm dialog = new PrinterSetupForm())
  204. {
  205. dialog.Report = this;
  206. dialog.PrintDialog = true;
  207. if (dialog.ShowDialog() != DialogResult.OK)
  208. return false;
  209. printerSettings = dialog.PrinterSettings;
  210. }
  211. return true;
  212. }
  213. #endregion Public Methods
  214. #region Private Methods
  215. private void ClearPreparedPages()
  216. {
  217. if (preview != null)
  218. preview.ClearTabsExceptFirst();
  219. else
  220. if (preparedPages != null)
  221. preparedPages.Clear();
  222. }
  223. private void DisposePreviewForm()
  224. {
  225. previewForm.Dispose();
  226. previewForm = null;
  227. preview = null;
  228. }
  229. private void OnClosePreview(object sender, FormClosedEventArgs e)
  230. {
  231. DisposePreviewForm();
  232. }
  233. private void SavePreviewPicture()
  234. {
  235. ReportPage page = PreparedPages.GetCachedPage(0);
  236. float pageWidth = page.WidthInPixels;
  237. float pageHeight = page.HeightInPixels;
  238. float ratio = ReportInfo.PreviewPictureRatio;
  239. ReportInfo.Picture = new Bitmap((int)Math.Round(pageWidth * ratio), (int)Math.Round(pageHeight * ratio));
  240. using (Graphics g = Graphics.FromImage(ReportInfo.Picture))
  241. {
  242. FRPaintEventArgs args = new FRPaintEventArgs(g, ratio, ratio, GraphicCache);
  243. page.Draw(args);
  244. }
  245. }
  246. private void ShowPrepared(bool modal, Form mdiParent, IWin32Window owner)
  247. {
  248. // create preview form
  249. if (Preview == null)
  250. {
  251. previewForm = new PreviewForm();
  252. previewForm.MdiParent = mdiParent;
  253. previewForm.ShowInTaskbar = Config.PreviewSettings.ShowInTaskbar;
  254. if (Config.PreviewSettings.TopMost)
  255. previewForm.TopMost = true;
  256. previewForm.Icon = Config.PreviewSettings.Icon;
  257. if (String.IsNullOrEmpty(Config.PreviewSettings.Text))
  258. {
  259. previewForm.Text = String.IsNullOrEmpty(ReportInfo.Name) ? "" : ReportInfo.Name + " - ";
  260. previewForm.Text += Res.Get("Preview");
  261. }
  262. else
  263. previewForm.Text = Config.PreviewSettings.Text;
  264. Preview = (previewForm as PreviewForm).Preview;
  265. Preview.UIStyle = Config.UIStyle;
  266. Preview.FastScrolling = Config.PreviewSettings.FastScrolling;
  267. Preview.Buttons = Config.PreviewSettings.Buttons;
  268. Preview.Exports = Config.PreviewSettings.Exports;
  269. Preview.Clouds = Config.PreviewSettings.Clouds;
  270. Preview.SaveInitialDirectory = Config.PreviewSettings.SaveInitialDirectory;
  271. }
  272. if (Config.ReportSettings.ShowPerformance)
  273. {
  274. try
  275. {
  276. // in case the format string is wrong, use try/catch
  277. Preview.ShowPerformance(String.Format(Res.Get("Messages,Performance"), tickCount));
  278. }
  279. catch
  280. {
  281. }
  282. }
  283. Preview.ClearTabsExceptFirst();
  284. if (PreparedPages != null)
  285. Preview.AddPreviewTab(this, GetReportName, null, true);
  286. Config.PreviewSettings.OnPreviewOpened(Preview);
  287. if (ReportInfo.SavePreviewPicture && PreparedPages.Count > 0)
  288. SavePreviewPicture();
  289. isPreviewing = true;
  290. if (previewForm != null && !previewForm.Visible)
  291. {
  292. if (modal)
  293. {
  294. previewForm.ShowDialog(owner);
  295. DisposePreviewForm();
  296. }
  297. else
  298. {
  299. previewForm.FormClosed += new FormClosedEventHandler(OnClosePreview);
  300. if (mdiParent == null)
  301. previewForm.Show(owner);
  302. else
  303. previewForm.Show();
  304. }
  305. isPreviewing = false;
  306. }
  307. }
  308. #endregion Private Methods
  309. }
  310. }