PreviewTab.cs 19 KB

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