PreviewWindow.xaml.cs 14 KB

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