123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681 |
- using System;
- using System.Windows.Forms;
- using System.Drawing;
- using System.IO;
- using FastReport.Utils;
- using FastReport.Forms;
- using FastReport.Export.Email;
- using FastReport.Auth;
- #if !MONO
- using FastReport.DevComponents.DotNetBar;
- #else
- using FastReport.Controls;
- #endif
- namespace FastReport.Preview
- {
- #if !MONO
- internal class PreviewTab : TabItem
- #else
- internal class PreviewTab : PageControlPage
- #endif
- {
- private PreviewWorkspace workspace;
- private PreviewControl preview;
- private PreparedPages preparedPages;
- private bool fake;
- private UIStyle style;
- private Report report;
- private ReportPage detailReportPage;
- private string hyperlinkValue;
- private string saveInitialDirectory;
- private PreviewSearchForm searchForm;
- #region Properties
- public Report Report
- {
- get { return report; }
- }
- public ReportPage DetailReportPage
- {
- get { return detailReportPage; }
- }
- public string HyperlinkValue
- {
- get { return hyperlinkValue; }
- }
- public SearchInfo SearchInfo
- {
- get { return Workspace.SearchInfo; }
- set { Workspace.SearchInfo = value; }
- }
- private PreviewWorkspace Workspace
- {
- get { return workspace; }
- }
- public PreviewControl Preview
- {
- get { return preview; }
- }
- public PreparedPages PreparedPages
- {
- get { return preparedPages; }
- }
- public int PageNo
- {
- get { return Workspace.PageNo; }
- set { Workspace.PageNo = value; }
- }
- public int PageCount
- {
- get { return Workspace.PageCount; }
- }
- public float Zoom
- {
- get { return Workspace.Zoom; }
- set { Workspace.Zoom = value; }
- }
- public bool Disabled
- {
- get { return Workspace.Disabled; }
- }
- public bool Fake
- {
- get { return fake; }
- set { fake = value; }
- }
- public UIStyle Style
- {
- get { return style; }
- set
- {
- style = value;
- #if !MONO
- Workspace.ColorSchemeStyle = UIStyleUtils.GetDotNetBarStyle(value);
- Workspace.Office2007ColorTable = UIStyleUtils.GetOffice2007ColorScheme(value);
- #endif
- Workspace.BackColor = Preview.BackColor;
- }
- }
- /// <summary>
- /// Gets or sets the initial directory that is displayed by a save file dialog.
- /// </summary>
- public string SaveInitialDirectory
- {
- get { return saveInitialDirectory; }
- set { saveInitialDirectory = value; }
- }
- #endregion
- #region Private Methods
- private void SetHyperlinkInfo(Hyperlink hyperlink)
- {
- if (hyperlink == null)
- return;
- hyperlinkValue = hyperlink.Value;
- if (hyperlink.Kind == HyperlinkKind.DetailPage)
- detailReportPage = Report.FindObject(hyperlink.DetailPageName) as ReportPage;
- }
- private void form_FormClosed(object sender, FormClosedEventArgs e)
- {
- searchForm.Dispose();
- searchForm = null;
- }
- #endregion
- #region Protected Methods
- protected override void Dispose(bool disposing)
- {
- base.Dispose(disposing);
- if (disposing)
- {
- workspace.Dispose();
- preparedPages.Dispose();
- searchForm?.Close();
- }
- }
- #endregion
- #region Public Methods
- public void BindPreparedPages()
- {
- report.SetPreparedPages(preparedPages);
- }
- public void UpdatePages()
- {
- Workspace.UpdatePages();
- }
- public void PositionTo(int pageNo, PointF point)
- {
- Workspace.PositionTo(pageNo, point);
- }
- public void RefreshReport()
- {
- Workspace.RefreshReport();
- }
- #if !MONO
- public void AddToTabControl(FastReport.DevComponents.DotNetBar.TabControl tabControl)
- {
- tabControl.Controls.Add(Workspace);
- tabControl.Tabs.Add(this);
- }
- #else
- public void AddToTabControl(FRTabControl tabControl)
- {
- if (tabControl.Tabs != null)
- tabControl.Tabs.Add(this);
- }
- #endif
- public void Focus()
- {
- Workspace.Focus();
- }
- #if !MONO
- public void Refresh()
- #else
- public override void Refresh()
- #endif
- {
- Workspace.Refresh();
- }
- public void UnlockLayout()
- {
- Workspace.UnlockLayout();
- }
- #endregion
- #region Preview commands
- public bool Print()
- {
- if (Disabled)
- return false;
- return preparedPages.Print(PageNo);
- }
- public void Save()
- {
- if (Disabled)
- return;
- using (SaveFileDialog dialog = new SaveFileDialog())
- {
- dialog.FileName = Path.GetFileNameWithoutExtension(Path.GetFileName(Report.FileName)) + ".fpx";
- dialog.Filter = Res.Get("FileFilters,PreparedReport");
- dialog.DefaultExt = "fpx";
- if (!string.IsNullOrEmpty(SaveInitialDirectory))
- dialog.InitialDirectory = SaveInitialDirectory;
- if (dialog.ShowDialog() == DialogResult.OK)
- Save(dialog.FileName);
- }
- }
- public void Save(string fileName)
- {
- if (Disabled)
- return;
- preparedPages.Save(fileName);
- }
- public void Save(Stream stream)
- {
- if (Disabled)
- return;
- preparedPages.Save(stream);
- }
- public void Load()
- {
- using (OpenFileDialog dialog = new OpenFileDialog())
- {
- dialog.Filter = Res.Get("FileFilters,PreparedReport");
- if (dialog.ShowDialog() == DialogResult.OK)
- Load(dialog.FileName);
- }
- }
- public void Load(string fileName)
- {
- using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
- {
- Load(stream);
- }
- }
- public void Load(Stream stream)
- {
- preparedPages.Load(stream);
- UpdatePages();
- First();
- }
- public void SendEmail()
- {
- EmailExport export = new EmailExport(Report);
- export.Account = Config.EmailSettings;
- export.Address = Report.EmailSettings.FirstRecipient;
- export.CC = Report.EmailSettings.CCRecipients;
- export.Subject = Report.EmailSettings.Subject;
- export.MessageBody = Report.EmailSettings.Message;
- if (export.ShowDialog() == DialogResult.OK)
- {
- Config.DoEvent();
- try
- {
- export.SendEmail(Report);
- if (Config.ReportSettings.ShowPerformance)
- Preview.ShowPerformance(String.Format(Res.Get("Export,Email,SuccessfulEmailSending")));
- }
- catch (Exception e)
- {
- FRMessageBox.Error(e.Message);
- if (Config.ReportSettings.ShowPerformance)
- Preview.ShowPerformance(String.Format(Res.Get("Export,Email,ErrorEmailSending")));
- }
- }
- }
- public void First()
- {
- PageNo = 1;
- }
- public void Prior()
- {
- PageNo--;
- }
- public void Next()
- {
- PageNo++;
- }
- public void Last()
- {
- PageNo = PageCount;
- }
- public void ZoomIn()
- {
- if (Disabled)
- return;
- Zoom += 0.25f;
- }
- public void ZoomOut()
- {
- if (Disabled)
- return;
- Zoom -= 0.25f;
- }
- public void ZoomWholePage()
- {
- if (Disabled)
- return;
- Workspace.ZoomWholePage();
- }
- public void ZoomPageWidth()
- {
- if (Disabled)
- return;
- Workspace.ZoomPageWidth();
- }
- public void Find()
- {
- if (Disabled || searchForm != null)
- return;
- searchForm = new PreviewSearchForm();
- searchForm.Owner = preview.FindForm();
- searchForm.PreviewTab = this;
- searchForm.FormClosed += new FormClosedEventHandler(form_FormClosed);
- searchForm.Show();
- }
- public bool Find(string text, bool matchCase, bool wholeWord)
- {
- if (Disabled)
- return false;
- SearchInfo = new SearchInfo(this);
- return SearchInfo.Find(text, matchCase, wholeWord);
- }
- public bool FindNext(string text, bool matchCase, bool wholeWord)
- {
- if (Disabled)
- return false;
- if (SearchInfo != null)
- return SearchInfo.FindNext(text, matchCase, wholeWord);
- return false;
- }
- public bool FindNext()
- {
- if (Disabled)
- return false;
- if (SearchInfo != null)
- return SearchInfo.FindNext();
- return false;
- }
- public async void EditPage()
- {
- if (Disabled)
- return;
- using (Report report = new Report())
- {
- ReportPage page = preparedPages.GetPage(PageNo - 1);
- OverlayBand overlay = new OverlayBand();
- overlay.Name = "Overlay";
- overlay.Width = page.WidthInPixels - (page.LeftMargin + page.RightMargin) * Units.Millimeters;
- overlay.Height = page.HeightInPixels - (page.TopMargin + page.BottomMargin) * Units.Millimeters;
- // remove bands, convert them to Text objects if necessary
- ObjectCollection allObjects = page.AllObjects;
- foreach (Base c in allObjects)
- {
- if (c is BandBase)
- {
- BandBase band = c as BandBase;
- if (band.HasBorder || band.HasFill)
- {
- TextObject textObj = new TextObject();
- textObj.Bounds = band.Bounds;
- textObj.Border = band.Border.Clone();
- textObj.Fill = band.Fill.Clone();
- overlay.Objects.Add(textObj);
- }
- for (int i = 0; i < band.Objects.Count; i++)
- {
- ReportComponentBase obj = band.Objects[i];
- if (!(obj is BandBase))
- {
- obj.Anchor = AnchorStyles.Left | AnchorStyles.Top;
- obj.Dock = DockStyle.None;
- obj.Left = obj.AbsLeft;
- obj.Top = obj.AbsTop;
- overlay.Objects.Add(obj);
- i--;
- }
- }
- }
- }
- page.Clear();
- page.Overlay = overlay;
- report.Pages.Add(page);
- page.SetReport(report);
- page.SetRunning(false);
- // temporary change paper width if page has unlimited width
- float currentPaperWidth = page.PaperWidth;
- if (page.UnlimitedWidth)
- {
- page.PaperWidth = page.UnlimitedWidthValue / Units.Millimeters;
- }
- if (await report.DesignPreviewPage())
- {
- page = report.Pages[0] as ReportPage;
- if (Design.PageDesigners.Page.ReportWorkspace.ClassicView)
- {
- foreach (Base obj in page.AllObjects)
- {
- if (obj is OverlayBand)
- {
- OverlayBand band = (obj as OverlayBand);
- band.Bounds = new RectangleF(band.Bounds.X, band.Bounds.Y - BandBase.HeaderSize, band.Bounds.Width, band.Bounds.Height);
- break;
- }
- }
- }
- // restore paper width if page has unlimited width
- if (page.UnlimitedWidth)
- {
- page.PaperWidth = currentPaperWidth;
- }
- preparedPages.ModifyPage(PageNo - 1, page);
- Refresh();
- }
- }
- }
- public void CopyPage()
- {
- if (!Disabled)
- {
- preparedPages.CopyPage(PageNo - 1);
- UpdatePages();
- }
- }
- public void DeletePage()
- {
- if (!Disabled && preparedPages.Count > 1)
- {
- preparedPages.RemovePage(PageNo - 1);
- UpdatePages();
- }
- }
- public void EditWatermark()
- {
- if (Disabled)
- return;
- ReportPage page = preparedPages.GetPage(PageNo - 1);
- using (WatermarkEditorForm editor = new WatermarkEditorForm())
- {
- editor.Watermark = page.Watermark;
- if (editor.ShowDialog() == DialogResult.OK)
- {
- if (editor.ApplyToAll)
- {
- // get original report page
- ReportPage originalPage = page.OriginalComponent.OriginalComponent as ReportPage;
- // no original page - probably we load the existing .fpx file
- if (originalPage == null)
- {
- // apply watermark in a fast way
- preparedPages.ApplyWatermark(editor.Watermark);
- Refresh();
- return;
- }
- // update the report template and refresh a report
- originalPage.Watermark = editor.Watermark.Clone();
- RefreshReport();
- }
- else
- {
- page.Watermark = editor.Watermark;
- preparedPages.ModifyPage(PageNo - 1, page);
- Refresh();
- }
- }
- }
- }
- public void PageSetup()
- {
- if (Disabled)
- return;
- using (PreviewPageSetupForm form = new PreviewPageSetupForm())
- {
- ReportPage page = preparedPages.GetPage(PageNo - 1);
- form.Page = page;
- // get original report page
- ReportPage originalPage = page.OriginalComponent.OriginalComponent as ReportPage;
- // no original page - probably we load the existing .fpx file
- if (originalPage == null)
- form.ApplyToAllEnabled = false;
- if (form.ShowDialog() == DialogResult.OK)
- {
- // avoid weird visual effects
- Refresh();
- if (form.ApplyToAll)
- {
- // update the report template and refresh a report
- originalPage.Landscape = page.Landscape;
- originalPage.PaperWidth = page.PaperWidth;
- originalPage.PaperHeight = page.PaperHeight;
- originalPage.UnlimitedHeight = page.UnlimitedHeight;
- originalPage.UnlimitedWidth = page.UnlimitedWidth;
- originalPage.LeftMargin = page.LeftMargin;
- originalPage.TopMargin = page.TopMargin;
- originalPage.RightMargin = page.RightMargin;
- originalPage.BottomMargin = page.BottomMargin;
- originalPage.UpdateBandsWidth();
- RefreshReport();
- }
- else
- {
- // update current page only
- preparedPages.ModifyPage(PageNo - 1, page);
- UpdatePages();
- }
- }
- }
- }
- #endregion
- public PreviewTab(PreviewControl preview, Report report, string text, Hyperlink hyperlink)
- {
- this.preview = preview;
- this.report = report;
- if (this.report != null)
- preparedPages = this.report.PreparedPages;
- else
- preparedPages = new PreparedPages(null);
- workspace = new PreviewWorkspace(this);
- #if !MONO
- AttachedControl = workspace;
- #else
- Controls.Add(workspace);
- #endif
- workspace.Dock = DockStyle.Fill;
- #if MONO
- Dock = DockStyle.Fill;
- #endif
- SetHyperlinkInfo(hyperlink);
- Text = text;
- Zoom = 1;// preview.Zoom;
- Style = preview.UIStyle;
- First();
- }
- }
- internal class SearchInfo
- {
- private PreviewTab previewTab;
- private string text;
- private bool matchCase;
- private bool wholeWord;
- public int pageNo;
- public int objNo;
- public int rangeNo;
- public CharacterRange[] ranges;
- public bool visible;
- public bool Find(string text, bool matchCase, bool wholeWord)
- {
- this.text = text;
- this.matchCase = matchCase;
- this.wholeWord = wholeWord;
- pageNo = previewTab.PageNo;
- rangeNo = -1;
- return FindNext();
- }
- public bool FindNext(string text, bool matchCase, bool wholeWord)
- {
- this.text = text;
- this.matchCase = matchCase;
- this.wholeWord = wholeWord;
- return FindNext();
- }
- public bool FindNext()
- {
- visible = true;
- for (; pageNo <= previewTab.PageCount; pageNo++)
- {
- ReportPage page = previewTab.PreparedPages.GetPage(pageNo - 1);
- ObjectCollection pageObjects = page.AllObjects;
- for (; objNo < pageObjects.Count; objNo++)
- {
- ISearchable obj = pageObjects[objNo] as ISearchable;
- if (obj != null)
- {
- ranges = obj.SearchText(text, matchCase, wholeWord);
- if (ranges != null)
- {
- rangeNo++;
- if (rangeNo < ranges.Length)
- {
- previewTab.PositionTo(pageNo, (obj as ComponentBase).AbsBounds.Location);
- previewTab.Refresh();
- return true;
- }
- }
- }
- rangeNo = -1;
- }
- objNo = 0;
- }
- pageNo = 1;
- visible = false;
- return false;
- }
- public SearchInfo(PreviewTab tab)
- {
- previewTab = tab;
- }
- }
- }
|