PreviewWindow.xaml.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. using System.IO;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using FastReport;
  5. using FastReport.Design;
  6. using FastReport.Design.StandardDesigner;
  7. using FastReport.Export;
  8. using FastReport.Export.BIFF8;
  9. using FastReport.Export.Html;
  10. using FastReport.Export.Image;
  11. using FastReport.Export.Pdf;
  12. using FastReport.Export.RichText;
  13. using FastReport.Export.Text;
  14. using InABox.Clients;
  15. using InABox.Core;
  16. using InABox.WPF;
  17. using Button = System.Windows.Controls.Button;
  18. using Image = System.Windows.Controls.Image;
  19. using SaveFileDialog = Microsoft.Win32.SaveFileDialog;
  20. using System;
  21. using InABox.Wpf;
  22. using TextBox = System.Windows.Controls.TextBox;
  23. using InABox.Core.Reports;
  24. using ICSharpCode.AvalonEdit.Highlighting;
  25. using System.Drawing;
  26. using System.Collections.Generic;
  27. using System.Threading.Tasks;
  28. namespace InABox.Wpf.Reports
  29. {
  30. /// <summary>
  31. /// Interaction logic for PreviewWindow.xaml
  32. /// </summary>
  33. public partial class PreviewWindow : ThemableWindow
  34. {
  35. private Report _report;
  36. private readonly ReportTemplate _template;
  37. private readonly DataModel _model;
  38. private bool modelIsPopulated;
  39. public Action<ReportTemplate>? SaveTemplate { get; set; } = null;
  40. public PreviewWindow(ReportTemplate template, DataModel model)
  41. {
  42. InitializeComponent();
  43. _template = template;
  44. _model = model;
  45. modelIsPopulated = false;
  46. PrintButton.Content = ImageUtils.CreatePreviewWindowButtonContent("Print",Wpf.Resources.print);
  47. SavePDFButton.Content = ImageUtils.CreatePreviewWindowButtonContent("Save PDF", Wpf.Resources.save);
  48. SaveRTFButton.Content = ImageUtils.CreatePreviewWindowButtonContent("Save RTF", Wpf.Resources.save);
  49. SaveExcelButton.Content = ImageUtils.CreatePreviewWindowButtonContent("Save Excel", Wpf.Resources.save);
  50. foreach (var def in ReportUtils.ExportDefinitions)
  51. {
  52. var button = new Button();
  53. if (def.Control != null)
  54. button.Content = def.Control;
  55. else
  56. button.Content = GetImage(def.Image);
  57. button.VerticalAlignment = VerticalAlignment.Top;
  58. button.Click += (o, e) =>
  59. {
  60. var data = ExportReport(def.Type);
  61. def.Action.Invoke(model, data);
  62. };
  63. Toolbar.Items.Insert(Toolbar.Items.IndexOf(ExportSeparator), button);
  64. }
  65. //EmailButton.Content = GetImage(Wpf.Resources.email);
  66. FirstButton.Content = ImageUtils.CreatePreviewWindowButtonContent("First",Wpf.Resources.first);
  67. BackButton.Content = ImageUtils.CreatePreviewWindowButtonContent("Back",Wpf.Resources.back);
  68. NextButton.Content = ImageUtils.CreatePreviewWindowButtonContent("Next",Wpf.Resources.next);
  69. LastButton.Content = ImageUtils.CreatePreviewWindowButtonContent("Last",Wpf.Resources.last);
  70. ZoomInButton.Content = ImageUtils.CreatePreviewWindowButtonContent("Zoom In",Wpf.Resources.zoomin);
  71. ZoomOutButton.Content = ImageUtils.CreatePreviewWindowButtonContent("Zoom Out",Wpf.Resources.zoomout);
  72. DesignButton.Content = ImageUtils.CreatePreviewWindowButtonContent("Design",Wpf.Resources.pencil);
  73. Loaded += PreviewWindow_Loaded;
  74. Editor.TextChanged += Editor_TextChanged;
  75. Designer.cmdPreview.CustomAction += CmdPreview_CustomAction;
  76. Designer.cmdSave.CustomAction += CmdSave_CustomAction;
  77. Designer.cmdSaveAs.CustomAction += CmdSaveAs_CustomAction;
  78. }
  79. private void CmdSaveAs_CustomAction(object? sender, EventArgs e)
  80. {
  81. var dialog = new SaveFileDialog();
  82. dialog.Filter = "Report files (*.frx)|*.frx";
  83. dialog.FileName = _template.Name + ".frx";
  84. if (dialog.ShowDialog() == true)
  85. Designer.Report.Save(dialog.FileName);
  86. }
  87. private void CmdSave_CustomAction(object? sender, EventArgs e)
  88. {
  89. SaveReport();
  90. }
  91. private void Editor_TextChanged(object? sender, EventArgs e)
  92. {
  93. }
  94. public bool IsPreview { get; set; } = true;
  95. public bool ShouldPopulate { get; set; } = true;
  96. public Report Report
  97. {
  98. get => _report;
  99. set
  100. {
  101. _report = value;
  102. Designer.Report = value;
  103. Refresh();
  104. }
  105. }
  106. public bool AllowDesign
  107. {
  108. get => DesignButton.Visibility == Visibility.Visible;
  109. set => DesignButton.Visibility = value ? Visibility.Visible : Visibility.Collapsed;
  110. }
  111. private Image GetImage(Bitmap bitmap)
  112. {
  113. return new Image
  114. {
  115. Source = bitmap.AsBitmapImage(),
  116. Height = 32.0F,
  117. Width = 32.0F,
  118. Margin = new Thickness(10)
  119. };
  120. }
  121. private void DisplayLoading()
  122. {
  123. PreviewGrid.RowDefinitions[1].Height = new GridLength(0);
  124. PreviewGrid.RowDefinitions[2].Height = new GridLength(1, GridUnitType.Star);
  125. DesignButton.IsEnabled = false;
  126. }
  127. private void CloseLoading()
  128. {
  129. PreviewGrid.RowDefinitions[1].Height = new GridLength(1, GridUnitType.Star);
  130. PreviewGrid.RowDefinitions[2].Height = new GridLength(0);
  131. DesignButton.IsEnabled = true;
  132. }
  133. private void SetupReport(Action action)
  134. {
  135. DisplayLoading();
  136. Task.Run(() =>
  137. {
  138. try
  139. {
  140. _report ??= ReportUtils.SetupReport(_template, _model, ShouldPopulate && !modelIsPopulated);
  141. return null;
  142. }
  143. catch (Exception e)
  144. {
  145. return e.Message;
  146. }
  147. }).ContinueWith((s) =>
  148. {
  149. try
  150. {
  151. if (s.Result == null)
  152. {
  153. Designer.Report ??= _report;
  154. action();
  155. CloseLoading();
  156. }
  157. else
  158. {
  159. ShowEditor(s.Result);
  160. }
  161. }
  162. catch (Exception e)
  163. {
  164. ShowEditor(e.Message);
  165. }
  166. }, TaskScheduler.FromCurrentSynchronizationContext());
  167. }
  168. private void ShowPreview()
  169. {
  170. MainGrid.RowDefinitions[0].Height = new GridLength(1, GridUnitType.Star);
  171. MainGrid.RowDefinitions[1].Height = new GridLength(0);
  172. MainGrid.RowDefinitions[2].Height = new GridLength(0);
  173. Title = string.Format("Report Preview - {0}", _template.Name);
  174. SetupReport(() => Refresh());
  175. /*Task.Run(() =>
  176. {
  177. try
  178. {
  179. if(_report == null)
  180. {
  181. _report = ReportUtils.SetupReport(_template, _model, !modelIsPopulated);
  182. }
  183. return null;
  184. }
  185. catch (Exception e)
  186. {
  187. return e.Message;
  188. }
  189. }).ContinueWith((s) =>
  190. {
  191. try
  192. {
  193. if (s.Result == null)
  194. {
  195. if(Designer.Report == null)
  196. {
  197. Designer.Report = _report;
  198. }
  199. Refresh();
  200. }
  201. else
  202. {
  203. ShowEditor(s.Result);
  204. }
  205. }
  206. catch (Exception e)
  207. {
  208. ShowEditor(e.Message);
  209. }
  210. }, TaskScheduler.FromCurrentSynchronizationContext());*/
  211. }
  212. private void ShowDesigner()
  213. {
  214. MainGrid.RowDefinitions[0].Height = new GridLength(0);
  215. MainGrid.RowDefinitions[1].Height = new GridLength(1, GridUnitType.Star);
  216. MainGrid.RowDefinitions[2].Height = new GridLength(0);
  217. SetupReport(() =>
  218. {
  219. Title = string.Format("Report Designer - {0}", Report.FileName);
  220. Designer.RefreshLayout();
  221. });
  222. }
  223. private void CmdPreview_CustomAction(object? sender, EventArgs e)
  224. {
  225. ShowPreview();
  226. }
  227. private void ShowEditor(string err)
  228. {
  229. MainGrid.RowDefinitions[0].Height = new GridLength(0);
  230. MainGrid.RowDefinitions[1].Height = new GridLength(0);
  231. MainGrid.RowDefinitions[2].Height = new GridLength(1, GridUnitType.Star);
  232. Title = string.Format("Edit Report Template - {0}", _template.Name);
  233. Editor.Text = _report?.SaveToString() ?? (String.IsNullOrWhiteSpace(_template.RDL) ? "" : _template.RDL);
  234. Editor.SyntaxHighlighting = HighlightingManager.Instance.GetDefinition("XML");
  235. System.Windows.MessageBox.Show("There was an error loading the report. The raw XML data has been loaded here.\n\nError Message:\n" + (err.Length < 200 ? err : string.Concat(err.AsSpan(0, 200), "...")));
  236. }
  237. private void PreviewWindow_Loaded(object sender, RoutedEventArgs e)
  238. {
  239. if (IsPreview)
  240. {
  241. ShowPreview();
  242. }
  243. else
  244. {
  245. ShowDesigner();
  246. }
  247. }
  248. private class Exporter
  249. {
  250. public Type Export { get; }
  251. public string FileExtension { get; }
  252. public string FileFilter { get; }
  253. public Exporter(Type export, string fileExtension, string fileFilter)
  254. {
  255. Export = export;
  256. FileExtension = fileExtension;
  257. FileFilter = fileFilter;
  258. }
  259. }
  260. private static readonly Dictionary<ReportExportType, Exporter> _exporters = new Dictionary<ReportExportType, Exporter>
  261. {
  262. { ReportExportType.PDF, new Exporter(typeof(PDFExport), ".pdf", "PDF Files (*.pdf)|*.pdf") },
  263. { ReportExportType.HTML, new Exporter(typeof(HTMLExport), ".html", "HTML Files (*.html)|*.html") },
  264. { ReportExportType.RTF, new Exporter(typeof(RTFExport), ".rtf", "RTF Files (*.rtf)|*.rtf") },
  265. { ReportExportType.Excel, new Exporter(typeof(Excel2003Document), ".xls", "Excel Files (*.xls)|*.xls") },
  266. { ReportExportType.Text, new Exporter(typeof(TextExport), ".txt", "Text Files (*.txt)|*.txt") },
  267. { ReportExportType.Image, new Exporter(typeof(ImageExport), ".jpg", "JPEG Files (*.jpg, *.jpeg)|*.jpg;*.jpeg") }
  268. };
  269. private byte[] ExportReport(ReportExportType type)
  270. {
  271. using var ms = new MemoryStream();
  272. _report.Export(Activator.CreateInstance(_exporters[type].Export) as ExportBase, ms);
  273. return ms.GetBuffer();
  274. }
  275. private void Refresh()
  276. {
  277. _report.Preview = Preview;
  278. _report.Prepare();
  279. _report.ShowPrepared();
  280. Preview.Zoom = 1.0F;
  281. }
  282. private void PrintButton_Click(object sender, RoutedEventArgs e)
  283. {
  284. Report.PrintPrepared();
  285. }
  286. private void SaveButton_Click(object sender, RoutedEventArgs e)
  287. {
  288. var exporter = _exporters[(ReportExportType)((Button)sender).Tag];
  289. var dlg = new SaveFileDialog();
  290. dlg.Filter = exporter.FileFilter;
  291. dlg.FileName = Path.ChangeExtension(Report.FileName, exporter.FileExtension);
  292. if (dlg.ShowDialog() == true)
  293. Report.Export(Activator.CreateInstance(exporter.Export) as ExportBase, dlg.FileName);
  294. }
  295. //private void EmailButton_Click(object sender, RoutedEventArgs e)
  296. //{
  297. // String attachment = System.IO.Path.Combine(System.IO.Path.GetTempPath(), System.IO.Path.ChangeExtension(Report.FileName,".pdf"));
  298. // using (new WaitCursor())
  299. // {
  300. // using (var filestream = System.IO.File.Open(attachment, System.IO.FileMode.Create))
  301. // {
  302. // _report.Export(new PDFExport(), filestream);
  303. // filestream.Close();
  304. // }
  305. // }
  306. // //Outlook.Application outlookApp = new Outlook.Application();
  307. // //Outlook.MailItem mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
  308. // //mailItem.Subject = System.IO.Path.ChangeExtension(Report.FileName,"");
  309. // //mailItem.To = "";
  310. // //mailItem.HTMLBody = "";
  311. // //mailItem.Attachments.Add(attachment, Outlook.OlAttachmentType.olByValue, Type.Missing, Type.Missing);
  312. // //mailItem.Display(false);
  313. //}
  314. private void FirstButton_Click(object sender, RoutedEventArgs e)
  315. {
  316. Preview.First();
  317. }
  318. private void BackButton_Click(object sender, RoutedEventArgs e)
  319. {
  320. Preview.Prior();
  321. }
  322. private void NextButton_Click(object sender, RoutedEventArgs e)
  323. {
  324. Preview.Next();
  325. }
  326. private void LastButton_Click(object sender, RoutedEventArgs e)
  327. {
  328. Preview.Last();
  329. }
  330. private void ZoomInButton_Click(object sender, RoutedEventArgs e)
  331. {
  332. Preview.ZoomIn();
  333. }
  334. private void ZoomOutButton_Click(object sender, RoutedEventArgs e)
  335. {
  336. Preview.ZoomOut();
  337. }
  338. private void SaveReport()
  339. {
  340. _template.RDL = _report.SaveToString();
  341. DoSave(_template);
  342. Designer.Report = _report;
  343. }
  344. private void DesignButton_Click(object sender, RoutedEventArgs e)
  345. {
  346. ShowDesigner();
  347. }
  348. private void ToolBar_Loaded(object sender, RoutedEventArgs e)
  349. {
  350. var toolBar = sender as ToolBar;
  351. var overflowGrid = toolBar.Template.FindName("OverflowGrid", toolBar) as FrameworkElement;
  352. if (overflowGrid != null) overflowGrid.Visibility = Visibility.Collapsed;
  353. var mainPanelBorder = toolBar.Template.FindName("MainPanelBorder", toolBar) as FrameworkElement;
  354. if (mainPanelBorder != null) mainPanelBorder.Margin = new Thickness();
  355. var grid = toolBar.Template.FindName("Grid", toolBar) as FrameworkElement;
  356. if (grid != null) grid.Margin = new Thickness();
  357. }
  358. private void DoSave(ReportTemplate template)
  359. {
  360. if (SaveTemplate is null)
  361. new Client<ReportTemplate>().Save(template, "Updated by Designer");
  362. else
  363. SaveTemplate?.Invoke(template);
  364. }
  365. private void SaveEditorButton_Click(object sender, RoutedEventArgs e)
  366. {
  367. _template.RDL = Editor.Text;
  368. DoSave(_template);
  369. _report = null;
  370. ShowPreview();
  371. }
  372. }
  373. }