PreviewTab.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. using System;
  2. using System.Windows.Forms;
  3. using System.Drawing;
  4. using System.IO;
  5. using FastReport.Utils;
  6. using FastReport.Forms;
  7. using FastReport.Export.Email;
  8. using FastReport.Auth;
  9. #if !MONO
  10. using FastReport.DevComponents.DotNetBar;
  11. #else
  12. using FastReport.Controls;
  13. #endif
  14. namespace FastReport.Preview
  15. {
  16. #if !MONO
  17. internal class PreviewTab : TabItem
  18. #else
  19. internal class PreviewTab : PageControlPage
  20. #endif
  21. {
  22. private PreviewWorkspace workspace;
  23. private PreviewControl preview;
  24. private PreparedPages preparedPages;
  25. private bool fake;
  26. private UIStyle style;
  27. private Report report;
  28. private ReportPage detailReportPage;
  29. private string hyperlinkValue;
  30. private string saveInitialDirectory;
  31. private PreviewSearchForm searchForm;
  32. #region Properties
  33. public Report Report
  34. {
  35. get { return report; }
  36. }
  37. public ReportPage DetailReportPage
  38. {
  39. get { return detailReportPage; }
  40. }
  41. public string HyperlinkValue
  42. {
  43. get { return hyperlinkValue; }
  44. }
  45. public SearchInfo SearchInfo
  46. {
  47. get { return Workspace.SearchInfo; }
  48. set { Workspace.SearchInfo = value; }
  49. }
  50. private PreviewWorkspace Workspace
  51. {
  52. get { return workspace; }
  53. }
  54. public PreviewControl Preview
  55. {
  56. get { return preview; }
  57. }
  58. public PreparedPages PreparedPages
  59. {
  60. get { return preparedPages; }
  61. }
  62. public int PageNo
  63. {
  64. get { return Workspace.PageNo; }
  65. set { Workspace.PageNo = value; }
  66. }
  67. public int PageCount
  68. {
  69. get { return Workspace.PageCount; }
  70. }
  71. public float Zoom
  72. {
  73. get { return Workspace.Zoom; }
  74. set { Workspace.Zoom = value; }
  75. }
  76. public bool Disabled
  77. {
  78. get { return Workspace.Disabled; }
  79. }
  80. public bool Fake
  81. {
  82. get { return fake; }
  83. set { fake = value; }
  84. }
  85. public UIStyle Style
  86. {
  87. get { return style; }
  88. set
  89. {
  90. style = value;
  91. #if !MONO
  92. Workspace.ColorSchemeStyle = UIStyleUtils.GetDotNetBarStyle(value);
  93. Workspace.Office2007ColorTable = UIStyleUtils.GetOffice2007ColorScheme(value);
  94. #endif
  95. Workspace.BackColor = Preview.BackColor;
  96. }
  97. }
  98. /// <summary>
  99. /// Gets or sets the initial directory that is displayed by a save file dialog.
  100. /// </summary>
  101. public string SaveInitialDirectory
  102. {
  103. get { return saveInitialDirectory; }
  104. set { saveInitialDirectory = value; }
  105. }
  106. #endregion
  107. #region Private Methods
  108. private void SetHyperlinkInfo(Hyperlink hyperlink)
  109. {
  110. if (hyperlink == null)
  111. return;
  112. hyperlinkValue = hyperlink.Value;
  113. if (hyperlink.Kind == HyperlinkKind.DetailPage)
  114. detailReportPage = Report.FindObject(hyperlink.DetailPageName) as ReportPage;
  115. }
  116. private void form_FormClosed(object sender, FormClosedEventArgs e)
  117. {
  118. searchForm.Dispose();
  119. searchForm = null;
  120. }
  121. #endregion
  122. #region Protected Methods
  123. protected override void Dispose(bool disposing)
  124. {
  125. base.Dispose(disposing);
  126. if (disposing)
  127. {
  128. workspace.Dispose();
  129. preparedPages.Dispose();
  130. searchForm?.Close();
  131. }
  132. }
  133. #endregion
  134. #region Public Methods
  135. public void BindPreparedPages()
  136. {
  137. report.SetPreparedPages(preparedPages);
  138. }
  139. public void UpdatePages()
  140. {
  141. Workspace.UpdatePages();
  142. }
  143. public void PositionTo(int pageNo, PointF point)
  144. {
  145. Workspace.PositionTo(pageNo, point);
  146. }
  147. public void RefreshReport()
  148. {
  149. Workspace.RefreshReport();
  150. }
  151. #if !MONO
  152. public void AddToTabControl(FastReport.DevComponents.DotNetBar.TabControl tabControl)
  153. {
  154. tabControl.Controls.Add(Workspace);
  155. tabControl.Tabs.Add(this);
  156. }
  157. #else
  158. public void AddToTabControl(FRTabControl tabControl)
  159. {
  160. if (tabControl.Tabs != null)
  161. tabControl.Tabs.Add(this);
  162. }
  163. #endif
  164. public void Focus()
  165. {
  166. Workspace.Focus();
  167. }
  168. #if !MONO
  169. public void Refresh()
  170. #else
  171. public override void Refresh()
  172. #endif
  173. {
  174. Workspace.Refresh();
  175. }
  176. public void UnlockLayout()
  177. {
  178. Workspace.UnlockLayout();
  179. }
  180. #endregion
  181. #region Preview commands
  182. public bool Print()
  183. {
  184. if (Disabled)
  185. return false;
  186. return preparedPages.Print(PageNo);
  187. }
  188. public void Save()
  189. {
  190. if (Disabled)
  191. return;
  192. using (SaveFileDialog dialog = new SaveFileDialog())
  193. {
  194. dialog.FileName = Path.GetFileNameWithoutExtension(Path.GetFileName(Report.FileName)) + ".fpx";
  195. dialog.Filter = Res.Get("FileFilters,PreparedReport");
  196. dialog.DefaultExt = "fpx";
  197. if (!string.IsNullOrEmpty(SaveInitialDirectory))
  198. dialog.InitialDirectory = SaveInitialDirectory;
  199. if (dialog.ShowDialog() == DialogResult.OK)
  200. Save(dialog.FileName);
  201. }
  202. }
  203. public void Save(string fileName)
  204. {
  205. if (Disabled)
  206. return;
  207. preparedPages.Save(fileName);
  208. }
  209. public void Save(Stream stream)
  210. {
  211. if (Disabled)
  212. return;
  213. preparedPages.Save(stream);
  214. }
  215. public void Load()
  216. {
  217. using (OpenFileDialog dialog = new OpenFileDialog())
  218. {
  219. dialog.Filter = Res.Get("FileFilters,PreparedReport");
  220. if (dialog.ShowDialog() == DialogResult.OK)
  221. Load(dialog.FileName);
  222. }
  223. }
  224. public void Load(string fileName)
  225. {
  226. using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
  227. {
  228. Load(stream);
  229. }
  230. }
  231. public void Load(Stream stream)
  232. {
  233. preparedPages.Load(stream);
  234. UpdatePages();
  235. First();
  236. }
  237. public void SendEmail()
  238. {
  239. EmailExport export = new EmailExport(Report);
  240. export.Account = Config.EmailSettings;
  241. export.Address = Report.EmailSettings.FirstRecipient;
  242. export.CC = Report.EmailSettings.CCRecipients;
  243. export.Subject = Report.EmailSettings.Subject;
  244. export.MessageBody = Report.EmailSettings.Message;
  245. if (export.ShowDialog() == DialogResult.OK)
  246. {
  247. Config.DoEvent();
  248. try
  249. {
  250. export.SendEmail(Report);
  251. if (Config.ReportSettings.ShowPerformance)
  252. Preview.ShowPerformance(String.Format(Res.Get("Export,Email,SuccessfulEmailSending")));
  253. }
  254. catch (Exception e)
  255. {
  256. FRMessageBox.Error(e.Message);
  257. if (Config.ReportSettings.ShowPerformance)
  258. Preview.ShowPerformance(String.Format(Res.Get("Export,Email,ErrorEmailSending")));
  259. }
  260. }
  261. }
  262. public void First()
  263. {
  264. PageNo = 1;
  265. }
  266. public void Prior()
  267. {
  268. PageNo--;
  269. }
  270. public void Next()
  271. {
  272. PageNo++;
  273. }
  274. public void Last()
  275. {
  276. PageNo = PageCount;
  277. }
  278. public void ZoomIn()
  279. {
  280. if (Disabled)
  281. return;
  282. Zoom += 0.25f;
  283. }
  284. public void ZoomOut()
  285. {
  286. if (Disabled)
  287. return;
  288. Zoom -= 0.25f;
  289. }
  290. public void ZoomWholePage()
  291. {
  292. if (Disabled)
  293. return;
  294. Workspace.ZoomWholePage();
  295. }
  296. public void ZoomPageWidth()
  297. {
  298. if (Disabled)
  299. return;
  300. Workspace.ZoomPageWidth();
  301. }
  302. public void Find()
  303. {
  304. if (Disabled || searchForm != null)
  305. return;
  306. searchForm = new PreviewSearchForm();
  307. searchForm.Owner = preview.FindForm();
  308. searchForm.PreviewTab = this;
  309. searchForm.FormClosed += new FormClosedEventHandler(form_FormClosed);
  310. searchForm.Show();
  311. }
  312. public bool Find(string text, bool matchCase, bool wholeWord)
  313. {
  314. if (Disabled)
  315. return false;
  316. SearchInfo = new SearchInfo(this);
  317. return SearchInfo.Find(text, matchCase, wholeWord);
  318. }
  319. public bool FindNext(string text, bool matchCase, bool wholeWord)
  320. {
  321. if (Disabled)
  322. return false;
  323. if (SearchInfo != null)
  324. return SearchInfo.FindNext(text, matchCase, wholeWord);
  325. return false;
  326. }
  327. public bool FindNext()
  328. {
  329. if (Disabled)
  330. return false;
  331. if (SearchInfo != null)
  332. return SearchInfo.FindNext();
  333. return false;
  334. }
  335. public async void EditPage()
  336. {
  337. if (Disabled)
  338. return;
  339. using (Report report = new Report())
  340. {
  341. ReportPage page = preparedPages.GetPage(PageNo - 1);
  342. OverlayBand overlay = new OverlayBand();
  343. overlay.Name = "Overlay";
  344. overlay.Width = page.WidthInPixels - (page.LeftMargin + page.RightMargin) * Units.Millimeters;
  345. overlay.Height = page.HeightInPixels - (page.TopMargin + page.BottomMargin) * Units.Millimeters;
  346. // remove bands, convert them to Text objects if necessary
  347. ObjectCollection allObjects = page.AllObjects;
  348. foreach (Base c in allObjects)
  349. {
  350. if (c is BandBase)
  351. {
  352. BandBase band = c as BandBase;
  353. if (band.HasBorder || band.HasFill)
  354. {
  355. TextObject textObj = new TextObject();
  356. textObj.Bounds = band.Bounds;
  357. textObj.Border = band.Border.Clone();
  358. textObj.Fill = band.Fill.Clone();
  359. overlay.Objects.Add(textObj);
  360. }
  361. for (int i = 0; i < band.Objects.Count; i++)
  362. {
  363. ReportComponentBase obj = band.Objects[i];
  364. if (!(obj is BandBase))
  365. {
  366. obj.Anchor = AnchorStyles.Left | AnchorStyles.Top;
  367. obj.Dock = DockStyle.None;
  368. obj.Left = obj.AbsLeft;
  369. obj.Top = obj.AbsTop;
  370. overlay.Objects.Add(obj);
  371. i--;
  372. }
  373. }
  374. }
  375. }
  376. page.Clear();
  377. page.Overlay = overlay;
  378. report.Pages.Add(page);
  379. page.SetReport(report);
  380. page.SetRunning(false);
  381. // temporary change paper width if page has unlimited width
  382. float currentPaperWidth = page.PaperWidth;
  383. if (page.UnlimitedWidth)
  384. {
  385. page.PaperWidth = page.UnlimitedWidthValue / Units.Millimeters;
  386. }
  387. if (await report.DesignPreviewPage())
  388. {
  389. page = report.Pages[0] as ReportPage;
  390. if (Design.PageDesigners.Page.ReportWorkspace.ClassicView)
  391. {
  392. foreach (Base obj in page.AllObjects)
  393. {
  394. if (obj is OverlayBand)
  395. {
  396. OverlayBand band = (obj as OverlayBand);
  397. band.Bounds = new RectangleF(band.Bounds.X, band.Bounds.Y - BandBase.HeaderSize, band.Bounds.Width, band.Bounds.Height);
  398. break;
  399. }
  400. }
  401. }
  402. // restore paper width if page has unlimited width
  403. if (page.UnlimitedWidth)
  404. {
  405. page.PaperWidth = currentPaperWidth;
  406. }
  407. preparedPages.ModifyPage(PageNo - 1, page);
  408. Refresh();
  409. }
  410. }
  411. }
  412. public void CopyPage()
  413. {
  414. if (!Disabled)
  415. {
  416. preparedPages.CopyPage(PageNo - 1);
  417. UpdatePages();
  418. }
  419. }
  420. public void DeletePage()
  421. {
  422. if (!Disabled && preparedPages.Count > 1)
  423. {
  424. preparedPages.RemovePage(PageNo - 1);
  425. UpdatePages();
  426. }
  427. }
  428. public void EditWatermark()
  429. {
  430. if (Disabled)
  431. return;
  432. ReportPage page = preparedPages.GetPage(PageNo - 1);
  433. using (WatermarkEditorForm editor = new WatermarkEditorForm())
  434. {
  435. editor.Watermark = page.Watermark;
  436. if (editor.ShowDialog() == DialogResult.OK)
  437. {
  438. if (editor.ApplyToAll)
  439. {
  440. // get original report page
  441. ReportPage originalPage = page.OriginalComponent.OriginalComponent as ReportPage;
  442. // no original page - probably we load the existing .fpx file
  443. if (originalPage == null)
  444. {
  445. // apply watermark in a fast way
  446. preparedPages.ApplyWatermark(editor.Watermark);
  447. Refresh();
  448. return;
  449. }
  450. // update the report template and refresh a report
  451. originalPage.Watermark = editor.Watermark.Clone();
  452. RefreshReport();
  453. }
  454. else
  455. {
  456. page.Watermark = editor.Watermark;
  457. preparedPages.ModifyPage(PageNo - 1, page);
  458. Refresh();
  459. }
  460. }
  461. }
  462. }
  463. public void PageSetup()
  464. {
  465. if (Disabled)
  466. return;
  467. using (PreviewPageSetupForm form = new PreviewPageSetupForm())
  468. {
  469. ReportPage page = preparedPages.GetPage(PageNo - 1);
  470. form.Page = page;
  471. // get original report page
  472. ReportPage originalPage = page.OriginalComponent.OriginalComponent as ReportPage;
  473. // no original page - probably we load the existing .fpx file
  474. if (originalPage == null)
  475. form.ApplyToAllEnabled = false;
  476. if (form.ShowDialog() == DialogResult.OK)
  477. {
  478. // avoid weird visual effects
  479. Refresh();
  480. if (form.ApplyToAll)
  481. {
  482. // update the report template and refresh a report
  483. originalPage.Landscape = page.Landscape;
  484. originalPage.PaperWidth = page.PaperWidth;
  485. originalPage.PaperHeight = page.PaperHeight;
  486. originalPage.UnlimitedHeight = page.UnlimitedHeight;
  487. originalPage.UnlimitedWidth = page.UnlimitedWidth;
  488. originalPage.LeftMargin = page.LeftMargin;
  489. originalPage.TopMargin = page.TopMargin;
  490. originalPage.RightMargin = page.RightMargin;
  491. originalPage.BottomMargin = page.BottomMargin;
  492. originalPage.UpdateBandsWidth();
  493. RefreshReport();
  494. }
  495. else
  496. {
  497. // update current page only
  498. preparedPages.ModifyPage(PageNo - 1, page);
  499. UpdatePages();
  500. }
  501. }
  502. }
  503. }
  504. #endregion
  505. public PreviewTab(PreviewControl preview, Report report, string text, Hyperlink hyperlink)
  506. {
  507. this.preview = preview;
  508. this.report = report;
  509. if (this.report != null)
  510. preparedPages = this.report.PreparedPages;
  511. else
  512. preparedPages = new PreparedPages(null);
  513. workspace = new PreviewWorkspace(this);
  514. #if !MONO
  515. AttachedControl = workspace;
  516. #else
  517. Controls.Add(workspace);
  518. #endif
  519. workspace.Dock = DockStyle.Fill;
  520. #if MONO
  521. Dock = DockStyle.Fill;
  522. #endif
  523. SetHyperlinkInfo(hyperlink);
  524. Text = text;
  525. Zoom = 1;// preview.Zoom;
  526. Style = preview.UIStyle;
  527. First();
  528. }
  529. }
  530. internal class SearchInfo
  531. {
  532. private PreviewTab previewTab;
  533. private string text;
  534. private bool matchCase;
  535. private bool wholeWord;
  536. public int pageNo;
  537. public int objNo;
  538. public int rangeNo;
  539. public CharacterRange[] ranges;
  540. public bool visible;
  541. public bool Find(string text, bool matchCase, bool wholeWord)
  542. {
  543. this.text = text;
  544. this.matchCase = matchCase;
  545. this.wholeWord = wholeWord;
  546. pageNo = previewTab.PageNo;
  547. rangeNo = -1;
  548. return FindNext();
  549. }
  550. public bool FindNext(string text, bool matchCase, bool wholeWord)
  551. {
  552. this.text = text;
  553. this.matchCase = matchCase;
  554. this.wholeWord = wholeWord;
  555. return FindNext();
  556. }
  557. public bool FindNext()
  558. {
  559. visible = true;
  560. for (; pageNo <= previewTab.PageCount; pageNo++)
  561. {
  562. ReportPage page = previewTab.PreparedPages.GetPage(pageNo - 1);
  563. ObjectCollection pageObjects = page.AllObjects;
  564. for (; objNo < pageObjects.Count; objNo++)
  565. {
  566. ISearchable obj = pageObjects[objNo] as ISearchable;
  567. if (obj != null)
  568. {
  569. ranges = obj.SearchText(text, matchCase, wholeWord);
  570. if (ranges != null)
  571. {
  572. rangeNo++;
  573. if (rangeNo < ranges.Length)
  574. {
  575. previewTab.PositionTo(pageNo, (obj as ComponentBase).AbsBounds.Location);
  576. previewTab.Refresh();
  577. return true;
  578. }
  579. }
  580. }
  581. rangeNo = -1;
  582. }
  583. objNo = 0;
  584. }
  585. pageNo = 1;
  586. visible = false;
  587. return false;
  588. }
  589. public SearchInfo(PreviewTab tab)
  590. {
  591. previewTab = tab;
  592. }
  593. }
  594. }