PreviewControl.cs 65 KB

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