123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- using System.IO;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Forms.Integration;
- using FastReport;
- using FastReport.Design;
- using FastReport.Design.StandardDesigner;
- using FastReport.Export;
- using FastReport.Export.BIFF8;
- using FastReport.Export.Html;
- using FastReport.Export.Image;
- using FastReport.Export.Pdf;
- using FastReport.Export.RichText;
- using FastReport.Export.Text;
- using ICSharpCode.AvalonEdit.Highlighting;
- using InABox.Clients;
- using InABox.Core;
- using InABox.Reports.Common;
- using InABox.WPF;
- using Button = System.Windows.Controls.Button;
- using Image = System.Windows.Controls.Image;
- using SaveFileDialog = Microsoft.Win32.SaveFileDialog;
- using System;
- using InABox.Wpf;
- namespace InABox.Reports
- {
- /// <summary>
- /// Interaction logic for PreviewWindow.xaml
- /// </summary>
- public partial class PreviewWindow : ThemableWindow
- {
- private Report _report;
- private readonly ReportTemplate _template;
- private readonly DataModel _model;
- private bool modelIsPopulated;
- public PreviewWindow(ReportTemplate template, DataModel model)
- {
- InitializeComponent();
- _template = template;
- _model = model;
- modelIsPopulated = false;
- PrintButton.Content = GetImage(Properties.Resources.print);
- SaveButton.Content = GetImage(Properties.Resources.save);
- foreach (var def in ReportUtils.ExportDefinitions)
- {
- var button = new Button();
- button.Content = GetImage(def.Image);
- button.Click += (o, e) =>
- {
- var data = ExportReport(def.Type);
- def.Action.Invoke(model, data);
- };
- Toolbar.Items.Insert(Toolbar.Items.IndexOf(ExportSeparator), button);
- }
- //EmailButton.Content = GetImage(Properties.Resources.email);
- FirstButton.Content = GetImage(Properties.Resources.first);
- BackButton.Content = GetImage(Properties.Resources.back);
- NextButton.Content = GetImage(Properties.Resources.next);
- LastButton.Content = GetImage(Properties.Resources.last);
- ZoomInButton.Content = GetImage(Properties.Resources.zoomin);
- ZoomOutButton.Content = GetImage(Properties.Resources.zoomout);
- DesignButton.Content = GetImage(Properties.Resources.pencil);
- Loaded += PreviewWindow_Loaded;
- Editor.TextChanged += Editor_TextChanged;
- Designer.cmdPreview.CustomAction += CmdPreview_CustomAction;
- Designer.cmdSave.CustomAction += CmdSave_CustomAction;
- Designer.cmdSaveAs.CustomAction += CmdSaveAs_CustomAction;
- }
- private void CmdSaveAs_CustomAction(object? sender, EventArgs e)
- {
- var dialog = new SaveFileDialog();
- dialog.Filter = "Report files (*.frx)|*.frx";
- dialog.FileName = _template.Name + ".frx";
- if (dialog.ShowDialog() == true)
- Designer.Report.Save(dialog.FileName);
- }
- private void CmdSave_CustomAction(object? sender, EventArgs e)
- {
- SaveReport();
- }
- private void Editor_TextChanged(object? sender, EventArgs e)
- {
- }
- public bool IsPreview { get; set; } = true;
- public bool ShouldPopulate { get; set; } = true;
-
- public Report Report
- {
- get => _report;
- set
- {
- _report = value;
- Designer.Report = value;
- Refresh();
- }
- }
- public bool AllowDesign
- {
- get => DesignButton.Visibility == Visibility.Visible;
- set => DesignButton.Visibility = value ? Visibility.Visible : Visibility.Collapsed;
- }
- private Image GetImage(Bitmap bitmap)
- {
- return new Image
- {
- Source = bitmap.AsBitmapImage(),
- Height = 32.0F,
- Width = 32.0F,
- Margin = new Thickness(10)
- };
- }
- private void DisplayLoading()
- {
- PreviewGrid.RowDefinitions[1].Height = new GridLength(0);
- PreviewGrid.RowDefinitions[2].Height = new GridLength(1, GridUnitType.Star);
- DesignButton.IsEnabled = false;
- }
- private void CloseLoading()
- {
- PreviewGrid.RowDefinitions[1].Height = new GridLength(1, GridUnitType.Star);
- PreviewGrid.RowDefinitions[2].Height = new GridLength(0);
- DesignButton.IsEnabled = true;
- }
- private void SetupReport(Action action)
- {
- DisplayLoading();
- Task.Run(() =>
- {
- try
- {
- if (_report == null)
- {
- _report = ReportUtils.SetupReport(_template, _model, ShouldPopulate && !modelIsPopulated);
- }
- return null;
- }
- catch (Exception e)
- {
- return e.Message;
- }
- }).ContinueWith((s) =>
- {
- try
- {
- if (s.Result == null)
- {
- if (Designer.Report == null)
- {
- Designer.Report = _report;
- }
- action();
- CloseLoading();
- }
- else
- {
- ShowEditor(s.Result);
- }
- }
- catch (Exception e)
- {
- ShowEditor(e.Message);
- }
- }, TaskScheduler.FromCurrentSynchronizationContext());
- }
- private void ShowPreview()
- {
- MainGrid.RowDefinitions[0].Height = new GridLength(1, GridUnitType.Star);
- MainGrid.RowDefinitions[1].Height = new GridLength(0);
- MainGrid.RowDefinitions[2].Height = new GridLength(0);
- Title = string.Format("Report Preview - {0}", _template.Name);
- SetupReport(() => Refresh());
- /*Task.Run(() =>
- {
- try
- {
- if(_report == null)
- {
- _report = ReportUtils.SetupReport(_template, _model, !modelIsPopulated);
- }
- return null;
- }
- catch (Exception e)
- {
- return e.Message;
- }
- }).ContinueWith((s) =>
- {
- try
- {
- if (s.Result == null)
- {
- if(Designer.Report == null)
- {
- Designer.Report = _report;
- }
- Refresh();
- }
- else
- {
- ShowEditor(s.Result);
- }
- }
- catch (Exception e)
- {
- ShowEditor(e.Message);
- }
- }, TaskScheduler.FromCurrentSynchronizationContext());*/
- }
- private void ShowDesigner()
- {
- MainGrid.RowDefinitions[0].Height = new GridLength(0);
- MainGrid.RowDefinitions[1].Height = new GridLength(1, GridUnitType.Star);
- MainGrid.RowDefinitions[2].Height = new GridLength(0);
- SetupReport(() => {
- Title = string.Format("Report Designer - {0}", Report.FileName);
- Designer.RefreshLayout();
- });
- }
- private void CmdPreview_CustomAction(object? sender, EventArgs e)
- {
- ShowPreview();
- }
- private void ShowEditor(string err)
- {
- MainGrid.RowDefinitions[0].Height = new GridLength(0);
- MainGrid.RowDefinitions[1].Height = new GridLength(0);
- MainGrid.RowDefinitions[2].Height = new GridLength(1, GridUnitType.Star);
- Title = string.Format("Edit Report Template - {0}", _template.Name);
- Editor.Text = _report?.SaveToString() ?? (String.IsNullOrWhiteSpace(_template.RDL) ? "" : _template.RDL);
- Editor.SyntaxHighlighting = HighlightingManager.Instance.GetDefinition("XML");
- 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), "...")));
- }
- private void PreviewWindow_Loaded(object sender, RoutedEventArgs e)
- {
- if (IsPreview)
- {
- ShowPreview();
- }
- else
- {
- ShowDesigner();
- }
- }
- private byte[] ExportReport(ReportExportType type)
- {
- var _exporters = new Dictionary<ReportExportType, ExportBase>
- {
- { ReportExportType.PDF, new PDFExport() },
- { ReportExportType.HTML, new HTMLExport() },
- { ReportExportType.RTF, new RTFExport() },
- { ReportExportType.Excel, new Excel2003Document() },
- { ReportExportType.Text, new TextExport() },
- { ReportExportType.Image, new ImageExport() }
- };
- byte[] result = null;
- using (var ms = new MemoryStream())
- {
- _report.Export(_exporters[type], ms);
- result = ms.GetBuffer();
- }
- return result;
- }
- private void Refresh()
- {
- _report.Preview = Preview;
- _report.Prepare();
- _report.ShowPrepared();
- Preview.Zoom = 1.0F;
- }
- private void PrintButton_Click(object sender, RoutedEventArgs e)
- {
- Report.PrintPrepared();
- }
- private void SaveButton_Click(object sender, RoutedEventArgs e)
- {
- var dlg = new SaveFileDialog();
- dlg.Filter = "PDF Files (*.pdf)|*.pdf";
- dlg.FileName = Path.ChangeExtension(Report.FileName, ".pdf");
- if (dlg.ShowDialog() == true)
- Report.Export(new PDFExport(), dlg.FileName);
- }
- //private void EmailButton_Click(object sender, RoutedEventArgs e)
- //{
- // String attachment = System.IO.Path.Combine(System.IO.Path.GetTempPath(), System.IO.Path.ChangeExtension(Report.FileName,".pdf"));
- // using (new WaitCursor())
- // {
- // using (var filestream = System.IO.File.Open(attachment, System.IO.FileMode.Create))
- // {
- // _report.Export(new PDFExport(), filestream);
- // filestream.Close();
- // }
- // }
- // //Outlook.Application outlookApp = new Outlook.Application();
- // //Outlook.MailItem mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
- // //mailItem.Subject = System.IO.Path.ChangeExtension(Report.FileName,"");
- // //mailItem.To = "";
- // //mailItem.HTMLBody = "";
- // //mailItem.Attachments.Add(attachment, Outlook.OlAttachmentType.olByValue, Type.Missing, Type.Missing);
- // //mailItem.Display(false);
- //}
- private void FirstButton_Click(object sender, RoutedEventArgs e)
- {
- Preview.First();
- }
- private void BackButton_Click(object sender, RoutedEventArgs e)
- {
- Preview.Prior();
- }
- private void NextButton_Click(object sender, RoutedEventArgs e)
- {
- Preview.Next();
- }
- private void LastButton_Click(object sender, RoutedEventArgs e)
- {
- Preview.Last();
- }
- private void ZoomInButton_Click(object sender, RoutedEventArgs e)
- {
- Preview.ZoomIn();
- }
- private void ZoomOutButton_Click(object sender, RoutedEventArgs e)
- {
- Preview.ZoomOut();
- }
- private void SaveReport()
- {
- _template.RDL = _report.SaveToString();
- new Client<ReportTemplate>().Save(_template, "Updated by Designer");
- Designer.Report = _report;
- }
- private void DesignButton_Click(object sender, RoutedEventArgs e)
- {
- ShowDesigner();
- }
- private void ToolBar_Loaded(object sender, RoutedEventArgs e)
- {
- var toolBar = sender as ToolBar;
- var overflowGrid = toolBar.Template.FindName("OverflowGrid", toolBar) as FrameworkElement;
- if (overflowGrid != null) overflowGrid.Visibility = Visibility.Collapsed;
- var mainPanelBorder = toolBar.Template.FindName("MainPanelBorder", toolBar) as FrameworkElement;
- if (mainPanelBorder != null) mainPanelBorder.Margin = new Thickness();
- var grid = toolBar.Template.FindName("Grid", toolBar) as FrameworkElement;
- if (grid != null) grid.Margin = new Thickness();
- }
- private void SaveEditorButton_Click(object sender, RoutedEventArgs e)
- {
- _template.RDL = Editor.Text;
- new Client<ReportTemplate>().Save(_template, "Updated by Designer");
- _report = null;
- ShowPreview();
- }
- }
- }
|