PreviewWindow.xaml.cs 13 KB

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