PreviewWindow.xaml.cs 14 KB

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