PreviewControl.cs 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Drawing.Design;
  6. using System.IO;
  7. using System.Reflection;
  8. using System.Windows.Forms;
  9. using FastReport.Cloud.StorageClient;
  10. using FastReport.Cloud.StorageClient.Box;
  11. using FastReport.Cloud.StorageClient.Dropbox;
  12. using FastReport.Cloud.StorageClient.Ftp;
  13. using FastReport.Cloud.StorageClient.GoogleDrive;
  14. using FastReport.Cloud.StorageClient.SkyDrive;
  15. using FastReport.DevComponents.DotNetBar;
  16. using FastReport.Export;
  17. using FastReport.Export.Email;
  18. using FastReport.Messaging;
  19. using FastReport.Utils;
  20. using Box = FastReport.Cloud.StorageClient.Box;
  21. using GoogleDrive = FastReport.Cloud.StorageClient.GoogleDrive;
  22. using SkyDrive = FastReport.Cloud.StorageClient.SkyDrive;
  23. #if !DEBUG
  24. using FastReport.Forms;
  25. #endif
  26. namespace FastReport.Preview
  27. {
  28. /// <summary>
  29. /// Represents a Windows Forms control used to preview a report.
  30. /// </summary>
  31. /// <remarks>
  32. /// To use this control, place it on a form and link it to a report using the report's
  33. /// <see cref="FastReport.Report.Preview"/> property. To show a report, call
  34. /// the <b>Report.Show</b> method:
  35. /// <code>
  36. /// report1.Preview = previewControl1;
  37. /// report1.Show();
  38. /// </code>
  39. /// <para>Use this control's methods such as <see cref="Print"/>, <see cref="Save()"/> etc. to
  40. /// handle the preview. Call <see cref="Clear"/> method to clear the preview.</para>
  41. /// <para>You can specify whether the standard toolbar is visible in the <see cref="ToolbarVisible"/>
  42. /// property. The <see cref="StatusbarVisible"/> property allows you to hide/show the statusbar.
  43. /// </para>
  44. /// </remarks>
  45. [ToolboxItem(true), ToolboxBitmap(typeof(Report), "Resources.PreviewControl.bmp")]
  46. public partial class PreviewControl : UserControl
  47. {
  48. #region Fields
  49. private Report report;
  50. private List<PreviewTab> documents;
  51. private bool toolbarVisible;
  52. private bool statusbarVisible;
  53. private Color pageBorderColor;
  54. private Color activePageBorderColor;
  55. private PreviewButtons buttons;
  56. private PreviewExports exports;
  57. private PreviewClouds clouds;
  58. private bool updatingZoom;
  59. private Timer updateTimer;
  60. private float zoomToUpdate;
  61. private float defaultZoom;
  62. private bool locked;
  63. private PreviewTab currentPreview;
  64. private bool fastScrolling;
  65. private UIStyle uiStyle;
  66. private bool useBackColor;
  67. private Point pageOffset;
  68. private string saveInitialDirectory;
  69. #endregion
  70. #region Properties
  71. /// <summary>
  72. /// Occurs when current page number is changed.
  73. /// </summary>
  74. public event EventHandler PageChanged;
  75. /// <summary>
  76. /// Occurs when Print button clicked.
  77. /// </summary>
  78. public event EventHandler<PrintEventArgs> OnPrint;
  79. public class PrintEventArgs : EventArgs
  80. {
  81. private PreviewControl preview;
  82. public PreviewControl Preview
  83. {
  84. get { return preview; }
  85. }
  86. public PrintEventArgs(PreviewControl preview)
  87. {
  88. this.preview = preview;
  89. }
  90. }
  91. /// <summary>
  92. /// Occurs when Export button clicked.
  93. /// </summary>
  94. public event EventHandler<ExportEventArgs> OnExport;
  95. public class ExportEventArgs : EventArgs
  96. {
  97. private ExportBase export;
  98. private Report report;
  99. private List<PreparedPages> pagesOfTabs;
  100. public Report Report
  101. {
  102. get { return report; }
  103. }
  104. public ExportBase Export
  105. {
  106. get { return export; }
  107. }
  108. public List<PreparedPages> PagesOfTabs
  109. {
  110. get { return pagesOfTabs; }
  111. }
  112. public ExportEventArgs(ExportBase export, Report report, List<PreparedPages> pagesOfTabs)
  113. {
  114. this.report = report;
  115. this.export = export;
  116. this.pagesOfTabs = pagesOfTabs;
  117. }
  118. }
  119. /// <summary>
  120. /// Gets a reference to the report.
  121. /// </summary>
  122. [Browsable(false)]
  123. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  124. public Report Report
  125. {
  126. get { return report; }
  127. }
  128. /// <summary>
  129. /// Obsolete. Gets or sets the color of page border.
  130. /// </summary>
  131. [Browsable(false)]
  132. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  133. [EditorBrowsable(EditorBrowsableState.Never)]
  134. public Color PageBorderColor
  135. {
  136. get { return pageBorderColor; }
  137. set { pageBorderColor = value; }
  138. }
  139. /// <summary>
  140. /// Gets or sets the color of active page border.
  141. /// </summary>
  142. [DefaultValue(typeof(Color), "255, 199, 60")]
  143. public Color ActivePageBorderColor
  144. {
  145. get { return activePageBorderColor; }
  146. set { activePageBorderColor = value; }
  147. }
  148. /// <summary>
  149. /// Gets or sets the first page offset from the top left corner of the control.
  150. /// </summary>
  151. public Point PageOffset
  152. {
  153. get { return pageOffset; }
  154. set { pageOffset = value; }
  155. }
  156. /// <summary>
  157. /// Gets a reference to the TabControl
  158. /// </summary>
  159. public DevComponents.DotNetBar.TabControl TabControl
  160. {
  161. get { return tabControl; }
  162. }
  163. /// <summary>
  164. /// Gets or sets a value indicating whether the toolbar is visible.
  165. /// </summary>
  166. [DefaultValue(true)]
  167. public bool ToolbarVisible
  168. {
  169. get { return toolbarVisible; }
  170. set
  171. {
  172. toolbarVisible = value;
  173. toolBar.Visible = value;
  174. }
  175. }
  176. /// <summary>
  177. /// Gets or sets a value indicating whether the statusbar is visible.
  178. /// </summary>
  179. [DefaultValue(true)]
  180. public bool StatusbarVisible
  181. {
  182. get { return statusbarVisible; }
  183. set
  184. {
  185. statusbarVisible = value;
  186. statusBar.Visible = value;
  187. }
  188. }
  189. /// <summary>
  190. /// Gets or sets a value indicating whether the outline control is visible.
  191. /// </summary>
  192. [DefaultValue(false)]
  193. public bool OutlineVisible
  194. {
  195. get { return outlineControl.Visible; }
  196. set
  197. {
  198. splitter.Visible = value;
  199. outlineControl.Visible = value;
  200. btnOutline.Checked = value;
  201. }
  202. }
  203. /// <summary>
  204. /// Specifies the set of buttons available in the toolbar.
  205. /// </summary>
  206. [DefaultValue(PreviewButtons.All)]
  207. public PreviewButtons Buttons
  208. {
  209. get { return buttons; }
  210. set
  211. {
  212. buttons = value;
  213. UpdateButtons();
  214. }
  215. }
  216. /// <summary>
  217. /// Specifies the set of exports that will be available in the preview's "save" menu.
  218. /// </summary>
  219. [DefaultValue(PreviewExports.All)]
  220. public PreviewExports Exports
  221. {
  222. get { return exports; }
  223. set
  224. {
  225. exports = value;
  226. Config.PreviewSettings.Exports = exports;
  227. UpdateButtons();
  228. }
  229. }
  230. /// <summary>
  231. /// Specifies the set of exports in clouds that will be available in the preview's "save" menu.
  232. /// </summary>
  233. [DefaultValue(PreviewClouds.All)]
  234. public PreviewClouds Clouds
  235. {
  236. get { return clouds; }
  237. set
  238. {
  239. clouds = value;
  240. Config.PreviewSettings.Clouds = clouds;
  241. UpdateButtons();
  242. }
  243. }
  244. /// <summary>
  245. /// Gets or sets a value indicating whether the fast scrolling method should be used.
  246. /// </summary>
  247. /// <remarks>
  248. /// If you enable this property, the gradient background will be disabled.
  249. /// </remarks>
  250. [DefaultValue(false)]
  251. public bool FastScrolling
  252. {
  253. get { return fastScrolling; }
  254. set { fastScrolling = value; }
  255. }
  256. /// <summary>
  257. /// Gets or sets the visual style.
  258. /// </summary>
  259. [DefaultValue(UIStyle.VisualStudio2012Light)]
  260. public UIStyle UIStyle
  261. {
  262. get { return uiStyle; }
  263. set
  264. {
  265. uiStyle = value;
  266. UpdateUIStyle();
  267. }
  268. }
  269. /// <summary>
  270. /// Gets or sets a value indicating that the BackColor property must be used to draw the background area.
  271. /// </summary>
  272. /// <remarks>
  273. /// By default, the background area is drawn using the color defined in the current <b>UIStyle</b>.
  274. /// </remarks>
  275. [DefaultValue(false)]
  276. public bool UseBackColor
  277. {
  278. get { return useBackColor; }
  279. set
  280. {
  281. useBackColor = value;
  282. UpdateUIStyle();
  283. }
  284. }
  285. /// <summary>
  286. /// Gets the preview window's toolbar.
  287. /// </summary>
  288. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  289. public Bar ToolBar
  290. {
  291. get { return toolBar; }
  292. }
  293. /// <summary>
  294. /// Gets the preview window's statusbar.
  295. /// </summary>
  296. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  297. public Bar StatusBar
  298. {
  299. get { return statusBar; }
  300. }
  301. /// <summary>
  302. /// Gets or sets the initial directory that is displayed by a save file dialog.
  303. /// </summary>
  304. public string SaveInitialDirectory
  305. {
  306. get { return saveInitialDirectory; }
  307. set { saveInitialDirectory = value; }
  308. }
  309. internal float DefaultZoom
  310. {
  311. get { return defaultZoom; }
  312. }
  313. internal PreviewTab CurrentPreview
  314. {
  315. get { return currentPreview; }
  316. }
  317. private bool IsPreviewEmpty
  318. {
  319. get { return CurrentPreview == null || CurrentPreview.Disabled; }
  320. }
  321. #endregion
  322. #region Private Methods
  323. private void UpdateDeletePageButton()
  324. {
  325. // Disable "Delete Page" button if there is only one page in preview.
  326. if (currentPreview != null && currentPreview.PreparedPages != null && currentPreview.PreparedPages.Count > 0)
  327. {
  328. btnDeletePage.Enabled = currentPreview.PreparedPages.Count > 1;
  329. }
  330. else
  331. btnDeletePage.Enabled = false;
  332. }
  333. private void CreateList(List<ObjectInfo> list, ButtonItem button, EventHandler handler)
  334. {
  335. foreach (ObjectInfo info in list)
  336. {
  337. if (info.Object != null && info.Enabled)
  338. {
  339. ButtonItem item = new ButtonItem("", Res.TryGet(info.Text) + "...");
  340. item.ImageIndex = info.ImageIndex;
  341. item.Tag = info;
  342. item.Name = info.Object.Name;
  343. item.Click += handler;
  344. button.SubItems.Add(item);
  345. }
  346. }
  347. }
  348. #if !COMMUNITY
  349. private ButtonItem createButtonItem(ExportsOptions.ExportsTreeNode node)
  350. {
  351. ButtonItem item = new ButtonItem("", node.ToString() + (node.ExportType == null ? "" : "..."));
  352. item.Tag = node.Tag;
  353. item.Name = node.Name;
  354. item.ImageIndex = node.ImageIndex;
  355. return item;
  356. }
  357. private void CreateExportsList(List<ExportsOptions.ExportsTreeNode> list, ButtonItem button, EventHandler handler)
  358. {
  359. foreach (ExportsOptions.ExportsTreeNode node in list)
  360. {
  361. if (node.ExportType != null && node.Enabled &&
  362. (node.ExportType.IsSubclassOf(typeof(ExportBase)) ||
  363. node.ExportType.IsSubclassOf(typeof(CloudStorageClient)) ||
  364. node.ExportType.IsSubclassOf(typeof(MessengerBase))))
  365. {
  366. ButtonItem item = createButtonItem(node);
  367. item.Click += handler;
  368. button.SubItems.Add(item);
  369. }
  370. else if (node.Enabled)
  371. {
  372. ButtonItem categoryItem = createButtonItem(node);
  373. button.SubItems.Add(categoryItem);
  374. CreateExportsList(node.Nodes, categoryItem, handler);
  375. }
  376. }
  377. }
  378. private void CreateCategoriesList(ButtonItem button, EventHandler handler)
  379. {
  380. ButtonItem saveNative = new ButtonItem("Prepared", Res.Get("Preview,SaveNative") + "...");
  381. saveNative.Click += handler;
  382. button.SubItems.Add(saveNative);
  383. ExportsOptions options = ExportsOptions.GetInstance();
  384. CreateExportsList(options.ExportsMenu, button, handler);
  385. }
  386. #endif
  387. private void CreateExportList(ButtonItem button, EventHandler handler)
  388. {
  389. List<ObjectInfo> list = new List<ObjectInfo>();
  390. RegisteredObjects.Exports.EnumItems(list);
  391. ButtonItem saveNative = new ButtonItem("Prepared", Res.Get("Preview,SaveNative") + "...");
  392. saveNative.Click += handler;
  393. button.SubItems.Add(saveNative);
  394. CreateList(list, button, handler);
  395. }
  396. private List<PreparedPages> GetPageOfTabs()
  397. {
  398. List<PreparedPages> list = new List<PreparedPages>();
  399. foreach (var tab in tabControl.Tabs)
  400. {
  401. if(tab is PreviewTab)
  402. list.Add((tab as PreviewTab).PreparedPages);
  403. }
  404. return list;
  405. }
  406. private void CreateCloudList(ButtonItem button, EventHandler handler)
  407. {
  408. #if !COMMUNITY
  409. ExportsOptions options = ExportsOptions.GetInstance();
  410. if (options.CloudMenu.Enabled)
  411. {
  412. ButtonItem categoryItem = createButtonItem(options.CloudMenu);
  413. categoryItem.BeginGroup = true;
  414. button.SubItems.Add(categoryItem);
  415. CreateExportsList(options.CloudMenu.Nodes, categoryItem, handler);
  416. }
  417. #endif
  418. }
  419. private void CreateMessengersList(ButtonItem button, EventHandler handler)
  420. {
  421. #if !COMMUNITY
  422. ExportsOptions options = ExportsOptions.GetInstance();
  423. if (options.MessengerMenu.Enabled)
  424. {
  425. ButtonItem categoryItem = createButtonItem(options.MessengerMenu);
  426. categoryItem.BeginGroup = true;
  427. button.SubItems.Add(categoryItem);
  428. CreateExportsList(options.MessengerMenu.Nodes, categoryItem, handler);
  429. }
  430. #endif
  431. }
  432. private void RemoveSubItems(ButtonItem button)
  433. {
  434. bool firstTime = true;
  435. for (int i = 0; i < button.SubItems.Count; i++)
  436. {
  437. if (button.SubItems[i].SubItems.Count > 0)
  438. {
  439. RemoveSubItems(button.SubItems[i] as ButtonItem);
  440. if (button.SubItems[i].SubItems.Count == 0)
  441. {
  442. button.SubItems.Remove(button.SubItems[i].Name);
  443. --i;
  444. }
  445. continue;
  446. }
  447. string itm_nm = button.SubItems[i].Name;
  448. if (Enum.IsDefined(typeof(PreviewExports), itm_nm) && exports != PreviewExports.All)
  449. {
  450. if (exports != PreviewExports.None)
  451. {
  452. foreach (PreviewExports nm in Enum.GetValues(typeof(PreviewExports)))
  453. {
  454. if (!((exports & nm) == nm) && itm_nm == nm.ToString())
  455. {
  456. button.SubItems.Remove(button.SubItems[i].Name);
  457. i--;
  458. break;
  459. }
  460. }
  461. }
  462. else
  463. {
  464. button.SubItems.Remove(button.SubItems[i].Name);
  465. i--;
  466. }
  467. }
  468. else if (Enum.IsDefined(typeof(PreviewClouds), itm_nm) && clouds != PreviewClouds.All)
  469. {
  470. if (clouds != PreviewClouds.None)
  471. {
  472. foreach (PreviewClouds nm in Enum.GetValues(typeof(PreviewClouds)))
  473. {
  474. if (!((clouds & nm) == nm) && itm_nm == nm.ToString())
  475. {
  476. button.SubItems.Remove(button.SubItems[i].Name);
  477. i--;
  478. break;
  479. }
  480. else if (firstTime && button.SubItems[i].BeginGroup != true)
  481. {
  482. button.SubItems[i].BeginGroup = true;
  483. firstTime = false;
  484. }
  485. }
  486. }
  487. else
  488. {
  489. button.SubItems.Remove(button.SubItems[i].Name);
  490. i--;
  491. }
  492. }
  493. }
  494. }
  495. private void UpdateButtons()
  496. {
  497. #if COMMUNITY
  498. btnPrint.Visible = false;
  499. #else
  500. btnPrint.Visible = (Buttons & PreviewButtons.Print) != 0;
  501. #endif
  502. btnOpen.Visible = (Buttons & PreviewButtons.Open) != 0;
  503. btnSave.Visible = (Buttons & PreviewButtons.Save) != 0;
  504. #if COMMUNITY
  505. btnEmail.Visible = false;
  506. btnEmailMapi.Visible = false;
  507. #else
  508. btnEmail.Visible = (Buttons & PreviewButtons.Email) != 0 && !Config.EmailSettings.UseMAPI;
  509. btnEmailMapi.Visible = (Buttons & PreviewButtons.Email) != 0 && Config.EmailSettings.UseMAPI;
  510. #endif
  511. btnFind.Visible = (Buttons & PreviewButtons.Find) != 0;
  512. btnOutline.Visible = (Buttons & PreviewButtons.Outline) != 0;
  513. btnPageSetup.Visible = (Buttons & PreviewButtons.PageSetup) != 0;
  514. btnDesign.Visible = (Buttons & PreviewButtons.Design) != 0;
  515. btnEdit.Visible = (Buttons & PreviewButtons.Edit) != 0;
  516. btnCopyPage.Visible = (Buttons & PreviewButtons.CopyPage) != 0;
  517. btnDeletePage.Visible = (Buttons & PreviewButtons.DeletePage) != 0;
  518. btnWatermark.Visible = (Buttons & PreviewButtons.Watermark) != 0;
  519. btnFirst.Visible = (Buttons & PreviewButtons.Navigator) != 0;
  520. btnPrior.Visible = (Buttons & PreviewButtons.Navigator) != 0;
  521. tbPageNo.Visible = (Buttons & PreviewButtons.Navigator) != 0;
  522. lblTotalPages.Visible = (Buttons & PreviewButtons.Navigator) != 0;
  523. btnNext.Visible = (Buttons & PreviewButtons.Navigator) != 0;
  524. btnLast.Visible = (Buttons & PreviewButtons.Navigator) != 0;
  525. btnClose.Visible = (Buttons & PreviewButtons.Close) != 0;
  526. btnAbout.Visible = (Buttons & PreviewButtons.About) != 0;
  527. if (!(exports == PreviewExports.All && clouds == PreviewClouds.All))
  528. {
  529. RemoveSubItems(btnSave);
  530. }
  531. }
  532. private void Export_Click(object sender, EventArgs e)
  533. {
  534. if (IsPreviewEmpty)
  535. return;
  536. ObjectInfo info = (sender as ButtonItem).Tag as ObjectInfo;
  537. if (info == null)
  538. Save();
  539. else
  540. {
  541. ExportBase export = Activator.CreateInstance(info.Object) as ExportBase;
  542. export.CurPage = CurrentPreview.PageNo;
  543. export.AllowSaveSettings = true;
  544. export.ShowProgress = true;
  545. Report report = new Report();
  546. PreparedPages preparedPages = new PreparedPages(report);
  547. foreach(var item in documents)
  548. {
  549. }
  550. if (!string.IsNullOrEmpty(SaveInitialDirectory))
  551. export.SaveInitialDirectory = SaveInitialDirectory;
  552. try
  553. {
  554. if (OnExport != null)
  555. OnExport(sender, new ExportEventArgs(export, CurrentPreview.Report, GetPageOfTabs()));
  556. else
  557. export.Export(CurrentPreview.Report, GetPageOfTabs());
  558. }
  559. #if !DEBUG
  560. catch (Exception ex)
  561. {
  562. using (ExceptionForm form = new ExceptionForm(ex))
  563. {
  564. form.ShowDialog();
  565. }
  566. }
  567. #endif
  568. finally
  569. {
  570. }
  571. }
  572. }
  573. private void SaveToCloud_Click(object sender, EventArgs e)
  574. {
  575. if (IsPreviewEmpty)
  576. {
  577. return;
  578. }
  579. ObjectInfo info = (sender as ButtonItem).Tag as ObjectInfo;
  580. if (info != null)
  581. {
  582. CloudStorageClient client = Activator.CreateInstance(info.Object) as CloudStorageClient;
  583. if (client is FtpStorageClient)
  584. {
  585. XmlItem xi = Config.Root.FindItem("FtpServer").FindItem("StorageSettings");
  586. string server = xi.GetProp("FtpServer");
  587. string username = xi.GetProp("FtpUsername");
  588. FtpStorageClientForm form = new FtpStorageClientForm(server, username, "", Report);
  589. form.ShowDialog();
  590. }
  591. else if (client is BoxStorageClient)
  592. {
  593. XmlItem xi = Config.Root.FindItem("BoxCloud").FindItem("StorageSettings");
  594. string id = xi.GetProp("ClientId");
  595. string secret = xi.GetProp("ClientSecret");
  596. if (String.IsNullOrEmpty(id) || String.IsNullOrEmpty(secret))
  597. {
  598. Box.ClientInfoForm clientInfoDialog = new Box.ClientInfoForm();
  599. clientInfoDialog.ShowDialog();
  600. id = clientInfoDialog.Id;
  601. secret = clientInfoDialog.Secret;
  602. }
  603. BoxStorageClientForm form = new BoxStorageClientForm(new SkyDrive.ClientInfo("", id, secret), Report);
  604. form.ShowDialog();
  605. }
  606. else if (client is DropboxStorageClient)
  607. {
  608. XmlItem xi = Config.Root.FindItem("DropboxCloud").FindItem("StorageSettings");
  609. string accessToken = xi.GetProp("AccessToken");
  610. if (String.IsNullOrEmpty(accessToken))
  611. {
  612. ApplicationInfoForm appInfoDialog = new ApplicationInfoForm();
  613. appInfoDialog.ShowDialog();
  614. accessToken = appInfoDialog.AccessToken;
  615. }
  616. DropboxStorageClientForm form = new DropboxStorageClientForm(accessToken, Report);
  617. form.ShowDialog();
  618. }
  619. else if (client is GoogleDriveStorageClient)
  620. {
  621. XmlItem xi = Config.Root.FindItem("GoogleDriveCloud").FindItem("StorageSettings");
  622. string id = xi.GetProp("ClientId");
  623. string secret = xi.GetProp("ClientSecret");
  624. if (String.IsNullOrEmpty(id) || String.IsNullOrEmpty(secret))
  625. {
  626. GoogleDrive.ClientInfoForm clientInfoDialog = new GoogleDrive.ClientInfoForm();
  627. clientInfoDialog.ShowDialog();
  628. if (clientInfoDialog.DialogResult != DialogResult.OK)
  629. return;
  630. id = clientInfoDialog.Id;
  631. secret = clientInfoDialog.Secret;
  632. }
  633. GoogleDriveStorageClientForm form = new GoogleDriveStorageClientForm(new SkyDrive.ClientInfo("", id, secret), Report);
  634. form.ShowDialog();
  635. }
  636. else if (client is SkyDriveStorageClient)
  637. {
  638. XmlItem xi = Config.Root.FindItem("SkyDriveCloud").FindItem("StorageSettings");
  639. string id = xi.GetProp("ClientId");
  640. string secret = xi.GetProp("ClientSecret");
  641. if (String.IsNullOrEmpty(id) || String.IsNullOrEmpty(secret))
  642. {
  643. SkyDrive.ClientInfoForm appInfoDialog = new SkyDrive.ClientInfoForm();
  644. appInfoDialog.ShowDialog();
  645. id = appInfoDialog.Id;
  646. secret = appInfoDialog.Secret;
  647. }
  648. SkyDriveStorageClientForm form = new SkyDriveStorageClientForm(new SkyDrive.ClientInfo("", id, secret), Report);
  649. form.ShowDialog();
  650. }
  651. }
  652. }
  653. private void SendViaMessenger_Click(object sender, EventArgs e)
  654. {
  655. if (IsPreviewEmpty)
  656. {
  657. return;
  658. }
  659. ObjectInfo info = (sender as ButtonItem).Tag as ObjectInfo;
  660. if (info != null)
  661. {
  662. MessengerBase messenger = Activator.CreateInstance(info.Object) as MessengerBase;
  663. //if (messenger is XmppMessenger)
  664. //{
  665. // XmlItem xi = Config.Root.FindItem("XmppMessenger").FindItem("MessengerSettings");
  666. // string jidFrom = xi.GetProp("JidFrom");
  667. // string password = xi.GetProp("Password");
  668. // string jidTo = xi.GetProp("JidTo");
  669. // XmppMessengerForm form = new XmppMessengerForm(jidFrom, password, jidTo, Report);
  670. // form.ShowDialog();
  671. //}
  672. //else
  673. //{
  674. // Type telegramMessenger = RegisteredObjects.FindType("TelegramMessenger");
  675. // if (telegramMessenger != null && messenger.GetType() == telegramMessenger)
  676. // try
  677. // {
  678. // MethodInfo InvokeInfoFormEditor = telegramMessenger.GetMethod("InvokeForm");
  679. // object[] parametrs = new object[1];
  680. // parametrs[0] = Report;
  681. // object o = Activator.CreateInstance(telegramMessenger);
  682. // InvokeInfoFormEditor.Invoke(o, parametrs);
  683. // }
  684. // catch (Exception ex)
  685. // {
  686. // FRMessageBox.Error(ex.Message);
  687. // }
  688. //}
  689. }
  690. }
  691. private void Email_Click(object sender, EventArgs e)
  692. {
  693. if (IsPreviewEmpty)
  694. return;
  695. List<string> fileNames = new List<string>();
  696. ObjectInfo info = (sender as ButtonItem).Tag as ObjectInfo;
  697. if (info == null)
  698. {
  699. using (SaveFileDialog dialog = new SaveFileDialog())
  700. {
  701. dialog.Filter = Res.Get("FileFilters,PreparedReport");
  702. dialog.DefaultExt = "*.fpx";
  703. if (dialog.ShowDialog() == DialogResult.OK)
  704. {
  705. Save(dialog.FileName);
  706. fileNames.Add(dialog.FileName);
  707. }
  708. }
  709. }
  710. else
  711. {
  712. ExportBase export = Activator.CreateInstance(info.Object) as ExportBase;
  713. export.CurPage = CurrentPreview.PageNo;
  714. export.AllowOpenAfter = false;
  715. export.ShowProgress = true;
  716. export.Export(CurrentPreview.Report, GetPageOfTabs());
  717. fileNames = export.GeneratedFiles;
  718. }
  719. if (fileNames.Count > 0)
  720. {
  721. Form form = FindForm();
  722. string[] recipientAddresses = CurrentPreview.Report.EmailSettings.Recipients == null ?
  723. new string[] { } : CurrentPreview.Report.EmailSettings.Recipients;
  724. int error = MAPI.SendMail(form == null ? IntPtr.Zero : form.Handle, fileNames.ToArray(),
  725. CurrentPreview.Report.EmailSettings.Subject,
  726. CurrentPreview.Report.EmailSettings.Message, new string[] { }, recipientAddresses);
  727. if (error > 1)
  728. {
  729. MessageBox.Show("MAPISendMail failed! " + MAPI.GetErrorText(error));
  730. }
  731. }
  732. }
  733. private void FUpdateTimer_Tick(object sender, EventArgs e)
  734. {
  735. updatingZoom = true;
  736. int zoom = (int)(zoomToUpdate * 100);
  737. slZoom.Text = zoom.ToString() + "%";
  738. if (zoom < 100)
  739. zoom = (int)Math.Round((zoom - 25) / 0.75f);
  740. else if (zoom > 100)
  741. zoom = (zoom - 100) / 4 + 100;
  742. slZoom.Value = zoom;
  743. updatingZoom = false;
  744. updateTimer.Stop();
  745. }
  746. private void tabControl1_TabItemClose(object sender, TabStripActionEventArgs e)
  747. {
  748. DeleteTab(currentPreview);
  749. e.Cancel = true;
  750. tabControl.RecalcLayout();
  751. }
  752. private void tabControl1_SelectedTabChanged(object sender, FastReport.DevComponents.DotNetBar.TabStripTabChangedEventArgs e)
  753. {
  754. if (locked)
  755. return;
  756. currentPreview = tabControl.SelectedTab as PreviewTab;
  757. if (currentPreview != null && !currentPreview.Fake)
  758. {
  759. currentPreview.BindPreparedPages();
  760. UpdateOutline();
  761. UpdateZoom(currentPreview.Zoom);
  762. UpdatePageNumbers(currentPreview.PageNo, currentPreview.PageCount);
  763. UpdateDeletePageButton();
  764. }
  765. }
  766. private void tabControl_Resize(object sender, EventArgs e)
  767. {
  768. foreach (PreviewTab tab in documents)
  769. {
  770. tab.UpdatePages();
  771. }
  772. }
  773. private bool CanDisposeTabReport(PreviewTab tab)
  774. {
  775. if (tab == null || tab.Report == null)
  776. return false;
  777. // if the preview is owned by Report, do not dispose
  778. if (report == tab.Report)
  779. return false;
  780. // check if the same Report is used in other tabs
  781. foreach (PreviewTab t in documents)
  782. {
  783. if (t != tab && t.Report == tab.Report)
  784. {
  785. return false;
  786. }
  787. }
  788. return true;
  789. }
  790. private void SetToolTip(BaseItem item, string tttext)
  791. {
  792. item.Tooltip = tttext;
  793. //item.toolTipFont = new Font(SystemFonts.DefaultFont.Name, SystemFonts.DefaultFont.Size * ratio / DrawUtils.ScreenDpiFX, FontStyle.Regular);
  794. }
  795. private void Localize()
  796. {
  797. MyRes res = new MyRes("Preview");
  798. btnDesign.Text = res.Get("DesignText");
  799. SetToolTip(btnDesign, res.Get("Design"));
  800. btnPrint.Text = res.Get("PrintText");
  801. SetToolTip(btnPrint, res.Get("Print"));
  802. SetToolTip(btnOpen, res.Get("Open"));
  803. SetToolTip(btnSave, res.Get("Save"));
  804. btnSave.Text = res.Get("SaveText");
  805. SetToolTip(btnEmail, res.Get("Email"));
  806. SetToolTip(btnEmailMapi, res.Get("Email"));
  807. SetToolTip(btnFind, res.Get("Find"));
  808. SetToolTip(btnOutline, res.Get("Outline"));
  809. SetToolTip(btnPageSetup, res.Get("PageSetup"));
  810. SetToolTip(btnEdit, res.Get("Edit"));
  811. SetToolTip(btnCopyPage, res.Get("CopyPage"));
  812. SetToolTip(btnDeletePage, res.Get("DeletePage"));
  813. SetToolTip(btnWatermark, res.Get("Watermark"));
  814. SetToolTip(btnFirst, res.Get("First"));
  815. SetToolTip(btnPrior, res.Get("Prior"));
  816. SetToolTip(btnNext, res.Get("Next"));
  817. lblTotalPages.Text = String.Format(Res.Get("Misc,ofM"), 1);
  818. SetToolTip(btnLast, res.Get("Last"));
  819. btnClose.Text = Res.Get("Buttons,Close");
  820. btnAbout.Text = new MyRes("Designer,Menu").Get("Help,About");
  821. }
  822. private void UpdateButtonImages(SubItemsCollection items)
  823. {
  824. foreach (BaseItem item in items)
  825. {
  826. ButtonItem b = item as ButtonItem;
  827. if (b != null && b.ImageIndex != -1)
  828. {
  829. b.Image = this.GetImage(b.ImageIndex);
  830. }
  831. UpdateButtonImages(item.SubItems);
  832. }
  833. }
  834. /// <summary>
  835. /// Updates the control appearance and layout on dpi change.
  836. /// </summary>
  837. public void UpdateDpiDependencies()
  838. {
  839. if (DevComponents.DpiHelper.HighDpiEnabled)
  840. DevComponents.DpiHelper.ReInit(this.Dpi(), this);
  841. Font = this.LogicalToDevice(DrawUtils.DefaultFont);
  842. toolBar.SuspendLayout();
  843. statusBar.SuspendLayout();
  844. toolBar.Font = Font;
  845. statusBar.Font = Font;
  846. outlineControl.Font = Font;
  847. outlineControl.UpdateDpiDependencies();
  848. UpdateButtonImages(toolBar.Items);
  849. btnZoomPageWidth.Image = this.GetImage(235);
  850. btnZoomWholePage.Image = this.GetImage(236);
  851. btnZoom100.Image = this.GetImage(237);
  852. toolBar.UpdateDpiDependencies();
  853. statusBar.UpdateDpiDependencies();
  854. toolBar.ResumeLayout();
  855. statusBar.ResumeLayout();
  856. }
  857. private void Init()
  858. {
  859. outlineControl.SetPreview(this);
  860. updateTimer = new Timer();
  861. updateTimer.Interval = 50;
  862. updateTimer.Tick += FUpdateTimer_Tick;
  863. pageBorderColor = Color.FromArgb(80, 80, 80);
  864. activePageBorderColor = Color.FromArgb(255, 199, 60);
  865. pageOffset = new Point(10, 10);
  866. defaultZoom = 1;
  867. buttons = PreviewButtons.All;
  868. exports = Config.PreviewSettings.Exports;
  869. clouds = Config.PreviewSettings.Clouds;
  870. btnDesign.ImageIndex = 68;
  871. btnPrint.ImageIndex = 195;
  872. btnOpen.ImageIndex = 1;
  873. btnSave.ImageIndex = 2;
  874. btnEmail.ImageIndex = 200;
  875. btnEmailMapi.ImageIndex = 200;
  876. btnFind.ImageIndex = 181;
  877. btnOutline.ImageIndex = 196;
  878. btnPageSetup.ImageIndex = 13;
  879. btnEdit.ImageIndex = 198;
  880. btnCopyPage.ImageIndex = 6;
  881. btnDeletePage.ImageIndex = 12;
  882. btnWatermark.ImageIndex = 194;
  883. btnFirst.ImageIndex = 185;
  884. btnPrior.ImageIndex = 186;
  885. btnNext.ImageIndex = 187;
  886. btnLast.ImageIndex = 188;
  887. #if !COMMUNITY
  888. CreateCategoriesList(btnSave, new EventHandler(Export_Click));
  889. #endif
  890. CreateExportList(btnEmailMapi, new EventHandler(Email_Click));
  891. CreateCloudList(btnSave, new EventHandler(SaveToCloud_Click));
  892. //CreateMessengersList(btnSave, new EventHandler(SendViaMessenger_Click));
  893. RestoreState();
  894. UpdateButtons();
  895. UpdateDpiDependencies();
  896. }
  897. private void RestoreState()
  898. {
  899. XmlItem xi = Config.Root.FindItem("Preview");
  900. string zoom = xi.GetProp("Zoom");
  901. if (!String.IsNullOrEmpty(zoom))
  902. defaultZoom = (float)Converter.FromString(typeof(float), zoom);
  903. string width = xi.GetProp("OutlineWidth");
  904. if (!String.IsNullOrEmpty(width))
  905. outlineControl.Width = int.Parse(width);
  906. }
  907. private void SaveState()
  908. {
  909. // Clear();
  910. outlineControl.Hide();
  911. XmlItem xi = Config.Root.FindItem("Preview");
  912. xi.SetProp("Zoom", Converter.ToString(Zoom));
  913. xi.SetProp("OutlineWidth", outlineControl.Width.ToString());
  914. }
  915. private void UpdateUIStyle()
  916. {
  917. switch (UIStyle)
  918. {
  919. case UIStyle.Office2003:
  920. case UIStyle.Office2007Blue:
  921. case UIStyle.Office2010Blue:
  922. StyleManager.ChangeStyle(eStyle.Office2010Blue, Color.Empty);
  923. break;
  924. case UIStyle.Office2007Silver:
  925. case UIStyle.Office2010Silver:
  926. StyleManager.ChangeStyle(eStyle.Office2010Silver, Color.Empty);
  927. break;
  928. case UIStyle.Office2007Black:
  929. case UIStyle.Office2010Black:
  930. StyleManager.ChangeStyle(eStyle.Office2010Black, Color.Empty);
  931. break;
  932. case UIStyle.Office2013:
  933. StyleManager.ChangeStyle(eStyle.Office2013, Color.Empty);
  934. break;
  935. case UIStyle.VisualStudio2005:
  936. case UIStyle.VisualStudio2010:
  937. StyleManager.ChangeStyle(eStyle.VisualStudio2010Blue, Color.Empty);
  938. break;
  939. case UIStyle.VisualStudio2012Light:
  940. StyleManager.ChangeStyle(eStyle.VisualStudio2012Light, Color.Empty);
  941. break;
  942. case UIStyle.VistaGlass:
  943. StyleManager.ChangeStyle(eStyle.Windows7Blue, Color.Empty);
  944. break;
  945. }
  946. //UIStyleUtils.UpdateUIStyle();
  947. eDotNetBarStyle style = UIStyleUtils.GetDotNetBarStyle(UIStyle);
  948. toolBar.Style = style;
  949. statusBar.Style = style;
  950. tabControl.Style = UIStyleUtils.GetTabStripStyle(UIStyle);
  951. outlineControl.Style = UIStyle;
  952. foreach (PreviewTab tab in documents)
  953. {
  954. tab.Style = UIStyle;
  955. }
  956. }
  957. private void UpdateOutline()
  958. {
  959. try
  960. {
  961. outlineControl.PreparedPages = currentPreview.PreparedPages;
  962. OutlineVisible = !currentPreview.PreparedPages.Outline.IsEmpty;
  963. }
  964. catch
  965. {
  966. }
  967. }
  968. private void AddFakeTab()
  969. {
  970. PreviewTab tab = new PreviewTab(this, null, "", null);
  971. tab.Fake = true;
  972. documents.Add(tab);
  973. tab.AddToTabControl(tabControl);
  974. }
  975. private void UpdateTabsVisible()
  976. {
  977. tabControl.TabsVisible = documents.Count > 1 && !documents[0].Fake;
  978. }
  979. private PreviewTab FindTab(string text)
  980. {
  981. foreach (PreviewTab tab in documents)
  982. {
  983. if (tab.Text == text)
  984. return tab;
  985. }
  986. return null;
  987. }
  988. private PreviewTab FindTabByHyperlinkValue(string value)
  989. {
  990. foreach (PreviewTab tab in documents)
  991. {
  992. if (tab.HyperlinkValue == value)
  993. return tab;
  994. }
  995. return null;
  996. }
  997. #endregion
  998. #region Protected Methods
  999. /// <inheritdoc/>
  1000. protected override void Dispose(bool disposing)
  1001. {
  1002. if (disposing)
  1003. {
  1004. if (components != null)
  1005. components.Dispose();
  1006. updateTimer.Dispose();
  1007. SaveState();
  1008. }
  1009. base.Dispose(disposing);
  1010. }
  1011. #endregion
  1012. #region Public Methods
  1013. internal void SetReport(Report report)
  1014. {
  1015. this.report = report;
  1016. }
  1017. internal void UpdatePageNumbers(int pageNo, int totalPages)
  1018. {
  1019. lblStatus.Text = String.Format(Res.Get("Misc,PageNofM"), pageNo, totalPages);
  1020. tbPageNo.Text = pageNo.ToString();
  1021. lblTotalPages.Text = String.Format(Res.Get("Misc,ofM"), totalPages);
  1022. if (PageChanged != null)
  1023. PageChanged(this, EventArgs.Empty);
  1024. }
  1025. internal void UpdateZoom(float zoom)
  1026. {
  1027. zoomToUpdate = zoom;
  1028. updateTimer.Start();
  1029. }
  1030. internal void UpdateUrl(string url)
  1031. {
  1032. lblUrl.Text = url;
  1033. }
  1034. internal void ShowPerformance(string text)
  1035. {
  1036. lblPerformance.Text = text;
  1037. }
  1038. internal void DoClick()
  1039. {
  1040. OnClick(EventArgs.Empty);
  1041. }
  1042. // Clears all tabs except the first one. This method is used in the report.Prepare.
  1043. // It is needed to avoid flickering when using stand-alone PreviewControl.
  1044. // When report is prepared and ShowPrepared method is called, the "fake" tab will
  1045. // be replaced with the new tab.
  1046. internal void ClearTabsExceptFirst()
  1047. {
  1048. while (documents.Count > 1)
  1049. {
  1050. DeleteTab(documents[documents.Count - 1]);
  1051. }
  1052. if (documents.Count == 1)
  1053. documents[0].Fake = true;
  1054. }
  1055. internal PreviewTab AddPreviewTab(Report report, string text, Hyperlink hyperlink, bool setActive)
  1056. {
  1057. PreviewTab tab = new PreviewTab(this, report, text, hyperlink);
  1058. documents.Add(tab);
  1059. //report.PreparedPages.ClearPageCache();
  1060. //OutlineVisible = !report.PreparedPages.Outline.IsEmpty;
  1061. tab.AddToTabControl(tabControl);
  1062. tab.UnlockLayout();
  1063. UpdateTabsVisible();
  1064. tab.UpdatePages();
  1065. if (setActive)
  1066. {
  1067. // do not stole the focus
  1068. tabControl.TabStrip.AutoSelectAttachedControl = false;
  1069. tabControl.SelectedTab = tab;
  1070. tabControl.TabStrip.AutoSelectAttachedControl = true;
  1071. }
  1072. else
  1073. tabControl.Refresh();
  1074. if (documents.Count == 2 && documents[0].Fake)
  1075. DeleteTab(documents[0]);
  1076. return tab;
  1077. }
  1078. /// <summary>
  1079. /// Adds a new report tab to the preview control.
  1080. /// </summary>
  1081. /// <param name="report">The <b>Report</b> object that contains the prepared report.</param>
  1082. /// <param name="text">The title for the new tab.</param>
  1083. /// <remarks>
  1084. /// Prepare the report using its <b>Prepare</b> method before you pass it to the <b>report</b> parameter.
  1085. /// </remarks>
  1086. public void AddTab(Report report, string text)
  1087. {
  1088. AddTab(report, text, true);
  1089. }
  1090. /// <summary>
  1091. /// Adds a new report tab to the preview control.
  1092. /// </summary>
  1093. /// <param name="report">The <b>Report</b> object that contains the prepared report.</param>
  1094. /// <param name="text">The title for the new tab.</param>
  1095. /// <param name="setActive">If <b>true</b>, makes the new tab active.</param>
  1096. /// <remarks>
  1097. /// Prepare the report using its <b>Prepare</b> method before you pass it to the <b>report</b> parameter.
  1098. /// </remarks>
  1099. public void AddTab(Report report, string text, bool setActive)
  1100. {
  1101. if (this.report == null)
  1102. SetReport(report);
  1103. AddPreviewTab(report, text, null, setActive);
  1104. }
  1105. /// <summary>
  1106. /// Switches to the tab with specified text.
  1107. /// </summary>
  1108. /// <param name="text">Text of the tab.</param>
  1109. /// <returns><b>true</b> if the tab with specified text exists, or <b>false</b> if there is no such tab.</returns>
  1110. public bool SwitchToTab(string text)
  1111. {
  1112. PreviewTab tab = FindTab(text);
  1113. if (tab != null)
  1114. {
  1115. tabControl.SelectedTab = tab;
  1116. return true;
  1117. }
  1118. return false;
  1119. }
  1120. internal bool SwitchToTab(Hyperlink hyperlink)
  1121. {
  1122. PreviewTab tab = FindTabByHyperlinkValue(hyperlink.Value);
  1123. if (tab != null)
  1124. {
  1125. tabControl.SelectedTab = tab;
  1126. return true;
  1127. }
  1128. return false;
  1129. }
  1130. /// <summary>
  1131. /// Deletes the report tab with specified text.
  1132. /// </summary>
  1133. /// <param name="text">The text of the tab.</param>
  1134. public void DeleteTab(string text)
  1135. {
  1136. PreviewTab tab = FindTab(text);
  1137. if (tab != null)
  1138. DeleteTab(tab);
  1139. }
  1140. /// <summary>
  1141. /// Checks if the tab with specified text exists.
  1142. /// </summary>
  1143. /// <param name="text">The text of the tab.</param>
  1144. /// <returns><b>true</b> if the tab exists.</returns>
  1145. public bool TabExists(string text)
  1146. {
  1147. return FindTab(text) != null;
  1148. }
  1149. internal void DeleteTab(PreviewTab tab)
  1150. {
  1151. if (CanDisposeTabReport(tab))
  1152. tab.Report.Dispose();
  1153. documents.Remove(tab);
  1154. tabControl.Tabs.Remove(tab);
  1155. tab.Dispose();
  1156. UpdateTabsVisible();
  1157. }
  1158. /// <summary>
  1159. /// Displays the text in the status bar.
  1160. /// </summary>
  1161. /// <param name="text">Text to display.</param>
  1162. public void ShowStatus(string text)
  1163. {
  1164. lblStatus.Text = text;
  1165. statusBar.Refresh();
  1166. }
  1167. internal void Lock()
  1168. {
  1169. locked = true;
  1170. }
  1171. internal void Unlock()
  1172. {
  1173. locked = false;
  1174. }
  1175. /// <summary>
  1176. /// Sets the focus to the preview control.
  1177. /// </summary>
  1178. public new void Focus()
  1179. {
  1180. if (currentPreview != null)
  1181. currentPreview.Focus();
  1182. }
  1183. #endregion
  1184. #region Event handlers
  1185. private void btnDesign_Click(object sender, EventArgs e)
  1186. {
  1187. Design();
  1188. }
  1189. private void btnPrint_Click(object sender, EventArgs e)
  1190. {
  1191. if (OnPrint != null)
  1192. OnPrint(sender, new PrintEventArgs(this));
  1193. else
  1194. Print();
  1195. }
  1196. private void btnOpen_Click(object sender, EventArgs e)
  1197. {
  1198. Load();
  1199. }
  1200. private void btnEmail_Click(object sender, EventArgs e)
  1201. {
  1202. SendEmail();
  1203. }
  1204. private void btnFind_Click(object sender, EventArgs e)
  1205. {
  1206. Find();
  1207. }
  1208. private void slZoom_ValueChanged(object sender, EventArgs e)
  1209. {
  1210. if (updatingZoom)
  1211. return;
  1212. int val = slZoom.Value;
  1213. if (val < 100)
  1214. val = (int)Math.Round(val * 0.75f) + 25;
  1215. else
  1216. val = (val - 100) * 4 + 100;
  1217. Zoom = val / 100f;
  1218. slZoom.Text = val.ToString() + "%";
  1219. }
  1220. private void btnZoomPageWidth_Click(object sender, EventArgs e)
  1221. {
  1222. ZoomPageWidth();
  1223. }
  1224. private void btnZoomWholePage_Click(object sender, EventArgs e)
  1225. {
  1226. ZoomWholePage();
  1227. }
  1228. private void btnZoom100_Click(object sender, EventArgs e)
  1229. {
  1230. Zoom = 1;
  1231. }
  1232. private void btnEdit_Click(object sender, EventArgs e)
  1233. {
  1234. EditPage();
  1235. }
  1236. private void btnCopyPage_Click(object sender, EventArgs e)
  1237. {
  1238. CopyPage();
  1239. }
  1240. private void btnDeletePage_Click(object sender, EventArgs e)
  1241. {
  1242. DeletePage();
  1243. }
  1244. private void btnFirst_Click(object sender, EventArgs e)
  1245. {
  1246. First();
  1247. }
  1248. private void btnPrior_Click(object sender, EventArgs e)
  1249. {
  1250. Prior();
  1251. }
  1252. private void btnNext_Click(object sender, EventArgs e)
  1253. {
  1254. Next();
  1255. }
  1256. private void btnLast_Click(object sender, EventArgs e)
  1257. {
  1258. Last();
  1259. }
  1260. private void tbPageNo_KeyDown(object sender, KeyEventArgs e)
  1261. {
  1262. if (e.KeyCode == Keys.Enter)
  1263. {
  1264. try
  1265. {
  1266. PageNo = int.Parse(tbPageNo.Text);
  1267. }
  1268. catch
  1269. {
  1270. PageNo = PageCount;
  1271. }
  1272. CurrentPreview.Focus();
  1273. }
  1274. }
  1275. private void tbPageNo_KeyPress(object sender, KeyPressEventArgs e)
  1276. {
  1277. if (e.KeyChar != '\b' && (e.KeyChar < '0' || e.KeyChar > '9'))
  1278. e.Handled = true;
  1279. }
  1280. private void btnWatermark_Click(object sender, EventArgs e)
  1281. {
  1282. EditWatermark();
  1283. }
  1284. private void btnOutline_Click(object sender, EventArgs e)
  1285. {
  1286. OutlineVisible = btnOutline.Checked;
  1287. }
  1288. private void btnPageSetup_Click(object sender, EventArgs e)
  1289. {
  1290. PageSetup();
  1291. }
  1292. private void btnClose_Click(object sender, EventArgs e)
  1293. {
  1294. if (FindForm() != null)
  1295. FindForm().Close();
  1296. }
  1297. private void tbPageNo_Click(object sender, EventArgs e)
  1298. {
  1299. tbPageNo.SelectAll();
  1300. }
  1301. private void btnAbout_Click(object sender, EventArgs e)
  1302. {
  1303. using (Forms.AboutForm aboutForm = new Forms.AboutForm())
  1304. {
  1305. aboutForm.ShowDialog();
  1306. }
  1307. }
  1308. #endregion
  1309. #region Preview commands
  1310. /// <summary>
  1311. /// Prints the current report.
  1312. /// </summary>
  1313. /// <returns><b>true</b> if report was printed; <b>false</b> if user cancels the "Print" dialog.</returns>
  1314. public bool Print()
  1315. {
  1316. if (CurrentPreview == null)
  1317. return false;
  1318. return CurrentPreview.Print();
  1319. }
  1320. /// <summary>
  1321. /// Saves the current report to a .fpx file using the "Save FIle" dialog.
  1322. /// </summary>
  1323. public void Save()
  1324. {
  1325. if (CurrentPreview == null)
  1326. return;
  1327. if (!string.IsNullOrEmpty(SaveInitialDirectory))
  1328. CurrentPreview.SaveInitialDirectory = SaveInitialDirectory;
  1329. CurrentPreview.Save();
  1330. }
  1331. /// <summary>
  1332. /// Saves the current report to a specified .fpx file.
  1333. /// </summary>
  1334. public void Save(string fileName)
  1335. {
  1336. if (CurrentPreview == null)
  1337. return;
  1338. if (!string.IsNullOrEmpty(SaveInitialDirectory))
  1339. CurrentPreview.SaveInitialDirectory = SaveInitialDirectory;
  1340. CurrentPreview.Save(fileName);
  1341. }
  1342. /// <summary>
  1343. /// Saves the current report to a stream.
  1344. /// </summary>
  1345. public void Save(Stream stream)
  1346. {
  1347. if (CurrentPreview == null)
  1348. return;
  1349. CurrentPreview.Save(stream);
  1350. }
  1351. private bool PreLoad()
  1352. {
  1353. if (CurrentPreview == null)
  1354. return false;
  1355. if (documents.Count == 1 && documents[0].Fake)
  1356. {
  1357. Report report = new Report();
  1358. report.SetPreparedPages(new PreparedPages(report));
  1359. AddTab(report, "");
  1360. }
  1361. return true;
  1362. }
  1363. private void PostLoad()
  1364. {
  1365. UpdateOutline();
  1366. UpdateDeletePageButton();
  1367. }
  1368. /// <summary>
  1369. /// Loads the report from a .fpx file using the "Open File" dialog.
  1370. /// </summary>
  1371. public new void Load()
  1372. {
  1373. if (!PreLoad())
  1374. return;
  1375. CurrentPreview.Load();
  1376. PostLoad();
  1377. }
  1378. /// <summary>
  1379. /// Loads the report from a specified .fpx file.
  1380. /// </summary>
  1381. public new void Load(string fileName)
  1382. {
  1383. if (!PreLoad())
  1384. return;
  1385. CurrentPreview.Load(fileName);
  1386. PostLoad();
  1387. }
  1388. /// <summary>
  1389. /// Load the report from a stream.
  1390. /// </summary>
  1391. /// <param name="stream">The stream to load from.</param>
  1392. public new void Load(Stream stream)
  1393. {
  1394. if (!PreLoad())
  1395. return;
  1396. CurrentPreview.Load(stream);
  1397. PostLoad();
  1398. }
  1399. /// <summary>
  1400. /// Sends an email.
  1401. /// </summary>
  1402. public void SendEmail()
  1403. {
  1404. if (CurrentPreview == null)
  1405. return;
  1406. CurrentPreview.SendEmail();
  1407. }
  1408. /// <summary>
  1409. /// Finds the text in the current report using the "Find Text" dialog.
  1410. /// </summary>
  1411. public void Find()
  1412. {
  1413. if (CurrentPreview == null)
  1414. return;
  1415. CurrentPreview.Find();
  1416. }
  1417. /// <summary>
  1418. /// Finds the specified text in the current report.
  1419. /// </summary>
  1420. /// <param name="text">Text to find.</param>
  1421. /// <param name="matchCase">A value indicating whether the search is case-sensitive.</param>
  1422. /// <param name="wholeWord">A value indicating whether the search matches whole words only.</param>
  1423. /// <returns><b>true</b> if text found.</returns>
  1424. public bool Find(string text, bool matchCase, bool wholeWord)
  1425. {
  1426. if (CurrentPreview == null)
  1427. return false;
  1428. return CurrentPreview.Find(text, matchCase, wholeWord);
  1429. }
  1430. /// <summary>
  1431. /// Finds the next occurence of text specified in the <b>Find</b> method.
  1432. /// </summary>
  1433. /// <returns><b>true</b> if text found.</returns>
  1434. public bool FindNext()
  1435. {
  1436. if (CurrentPreview == null)
  1437. return false;
  1438. return CurrentPreview.FindNext();
  1439. }
  1440. /// <summary>
  1441. /// Navigates to the first page.
  1442. /// </summary>
  1443. public void First()
  1444. {
  1445. if (CurrentPreview == null)
  1446. return;
  1447. CurrentPreview.First();
  1448. }
  1449. /// <summary>
  1450. /// Navigates to the previuos page.
  1451. /// </summary>
  1452. public void Prior()
  1453. {
  1454. if (CurrentPreview == null)
  1455. return;
  1456. CurrentPreview.Prior();
  1457. }
  1458. /// <summary>
  1459. /// Navigates to the next page.
  1460. /// </summary>
  1461. public void Next()
  1462. {
  1463. if (CurrentPreview == null)
  1464. return;
  1465. CurrentPreview.Next();
  1466. }
  1467. /// <summary>
  1468. /// Navigates to the last page.
  1469. /// </summary>
  1470. public void Last()
  1471. {
  1472. if (CurrentPreview == null)
  1473. return;
  1474. CurrentPreview.Last();
  1475. }
  1476. /// <summary>
  1477. /// Gets or sets the current page number.
  1478. /// </summary>
  1479. /// <remarks>
  1480. /// This value is 1-based.
  1481. /// </remarks>
  1482. [Browsable(false)]
  1483. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1484. public int PageNo
  1485. {
  1486. get
  1487. {
  1488. if (CurrentPreview == null)
  1489. return 1;
  1490. return CurrentPreview.PageNo;
  1491. }
  1492. set
  1493. {
  1494. if (CurrentPreview == null)
  1495. return;
  1496. CurrentPreview.PageNo = value;
  1497. }
  1498. }
  1499. /// <summary>
  1500. /// Gets the pages count in the current report.
  1501. /// </summary>
  1502. [Browsable(false)]
  1503. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1504. public int PageCount
  1505. {
  1506. get
  1507. {
  1508. if (CurrentPreview == null)
  1509. return 0;
  1510. return CurrentPreview.PageCount;
  1511. }
  1512. }
  1513. /// <summary>
  1514. /// Gets or sets the zoom factor.
  1515. /// </summary>
  1516. /// <remarks>
  1517. /// <b>1</b> corresponds to 100% zoom.
  1518. /// </remarks>
  1519. [Browsable(false)]
  1520. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1521. public float Zoom
  1522. {
  1523. get
  1524. {
  1525. if (CurrentPreview == null)
  1526. return 1;
  1527. return CurrentPreview.Zoom;
  1528. }
  1529. set
  1530. {
  1531. if (CurrentPreview != null)
  1532. CurrentPreview.Zoom = value;
  1533. }
  1534. }
  1535. /// <summary>
  1536. /// Zooms in.
  1537. /// </summary>
  1538. public void ZoomIn()
  1539. {
  1540. if (CurrentPreview == null)
  1541. return;
  1542. CurrentPreview.ZoomIn();
  1543. }
  1544. /// <summary>
  1545. /// Zooms out.
  1546. /// </summary>
  1547. public void ZoomOut()
  1548. {
  1549. if (CurrentPreview == null)
  1550. return;
  1551. CurrentPreview.ZoomOut();
  1552. }
  1553. /// <summary>
  1554. /// Zooms to fit the page width.
  1555. /// </summary>
  1556. public void ZoomPageWidth()
  1557. {
  1558. if (CurrentPreview == null)
  1559. return;
  1560. CurrentPreview.ZoomPageWidth();
  1561. }
  1562. /// <summary>
  1563. /// Zooms to fit the whole page.
  1564. /// </summary>
  1565. public void ZoomWholePage()
  1566. {
  1567. if (CurrentPreview == null)
  1568. return;
  1569. CurrentPreview.ZoomWholePage();
  1570. }
  1571. /// <summary>
  1572. /// Edits the current page in the designer.
  1573. /// </summary>
  1574. public void EditPage()
  1575. {
  1576. if (CurrentPreview == null)
  1577. return;
  1578. CurrentPreview.EditPage();
  1579. }
  1580. /// <summary>
  1581. /// Copies the current page in preview.
  1582. /// </summary>
  1583. public void CopyPage()
  1584. {
  1585. if (CurrentPreview != null)
  1586. {
  1587. CurrentPreview.CopyPage();
  1588. UpdateDeletePageButton();
  1589. }
  1590. }
  1591. /// <summary>
  1592. /// Removes the current page in preview.
  1593. /// </summary>
  1594. public void DeletePage()
  1595. {
  1596. if (CurrentPreview != null)
  1597. {
  1598. CurrentPreview.DeletePage();
  1599. UpdateDeletePageButton();
  1600. }
  1601. }
  1602. /// <summary>
  1603. /// Edits the current report in the designer.
  1604. /// </summary>
  1605. public void Design()
  1606. {
  1607. if (Report == null)
  1608. return;
  1609. using (Report designedReport = new Report())
  1610. {
  1611. designedReport.FileName = Report.FileName;
  1612. if (!String.IsNullOrEmpty(Report.FileName))
  1613. designedReport.Load(designedReport.FileName);
  1614. else
  1615. using (MemoryStream repStream = new MemoryStream())
  1616. {
  1617. Report.Save(repStream);
  1618. repStream.Position = 0;
  1619. designedReport.Load(repStream);
  1620. }
  1621. Report.Dictionary.ReRegisterData(designedReport.Dictionary);
  1622. if (designedReport.Design())
  1623. {
  1624. Report.PreparedPages.Clear();
  1625. Report.PreparedPages.ClearPageCache();
  1626. Report.LoadFromString(designedReport.SaveToString());
  1627. if (CurrentPreview != null)
  1628. {
  1629. Report.Preview = CurrentPreview.Preview;
  1630. Report.Show();
  1631. }
  1632. }
  1633. }
  1634. }
  1635. /// <summary>
  1636. /// Edits the watermark.
  1637. /// </summary>
  1638. public void EditWatermark()
  1639. {
  1640. if (CurrentPreview == null)
  1641. return;
  1642. CurrentPreview.EditWatermark();
  1643. }
  1644. /// <summary>
  1645. /// Edits the page settings.
  1646. /// </summary>
  1647. public void PageSetup()
  1648. {
  1649. if (CurrentPreview == null)
  1650. return;
  1651. CurrentPreview.PageSetup();
  1652. }
  1653. /// <summary>
  1654. /// Navigates to the specified position inside a specified page.
  1655. /// </summary>
  1656. /// <param name="pageNo">The page number (1-based).</param>
  1657. /// <param name="point">The position inside a page, in pixels.</param>
  1658. public void PositionTo(int pageNo, PointF point)
  1659. {
  1660. if (CurrentPreview == null)
  1661. return;
  1662. CurrentPreview.PositionTo(pageNo, point);
  1663. }
  1664. /// <summary>
  1665. /// Clears the preview.
  1666. /// </summary>
  1667. public void Clear()
  1668. {
  1669. while (documents.Count > 0)
  1670. {
  1671. DeleteTab(documents[0]);
  1672. }
  1673. lblStatus.Text = "";
  1674. tbPageNo.Text = "";
  1675. }
  1676. /// <summary>
  1677. /// Refresh the report.
  1678. /// </summary>
  1679. public void RefreshReport()
  1680. {
  1681. if (CurrentPreview == null)
  1682. return;
  1683. CurrentPreview.RefreshReport();
  1684. }
  1685. #endregion
  1686. /// <summary>
  1687. /// Initializes a new instance of the <see cref="PreviewControl"/> class.
  1688. /// </summary>
  1689. public PreviewControl()
  1690. {
  1691. // we need this to ensure that static constructor of the Report was called.
  1692. Report report = new Report();
  1693. report.Dispose();
  1694. BarUtilities.UseTextRenderer = true;
  1695. documents = new List<PreviewTab>();
  1696. InitializeComponent();
  1697. toolbarVisible = true;
  1698. statusbarVisible = true;
  1699. OutlineVisible = false;
  1700. UIStyle = Config.UIStyle;
  1701. Localize();
  1702. Init();
  1703. AddFakeTab();
  1704. RightToLeft = Config.RightToLeft ? RightToLeft.Yes : RightToLeft.No;
  1705. }
  1706. }
  1707. }