DesignerSettings.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.IO;
  7. using System.Data.Common;
  8. using FastReport.Data;
  9. using FastReport.Preview;
  10. using FastReport.Utils;
  11. using System.Threading.Tasks;
  12. namespace FastReport.Design
  13. {
  14. /// <summary>
  15. /// This class contains settings that will be applied to the report designer.
  16. /// </summary>
  17. [TypeConverter(typeof(FastReport.TypeConverters.FRExpandableObjectConverter))]
  18. public class DesignerSettings
  19. {
  20. private Icon icon;
  21. private Font defaultFont;
  22. private bool showInTaskbar;
  23. private string text;
  24. private DesignerRestrictions restrictions;
  25. private List<ConnectionEntry> customConnections;
  26. private DbConnection applicationConnection;
  27. private Type applicationConnectionType;
  28. private ToolStripRenderer toolStripRenderer;
  29. private static int openDialogFilterIndex = 1;
  30. /// <summary>
  31. /// Occurs when the designer is loaded.
  32. /// </summary>
  33. /// <remarks>
  34. /// Use this event if you want to customize some aspects of the designer, for example,
  35. /// to hide some menu items.
  36. /// </remarks>
  37. /// <example>
  38. /// This example demonstrates how to hide the "File|Select Language..." menu item.
  39. /// <code>
  40. /// Config.DesignerSettings.DesignerLoaded += new EventHandler(DesignerSettings_DesignerLoaded);
  41. ///
  42. /// void DesignerSettings_DesignerLoaded(object sender, EventArgs e)
  43. /// {
  44. /// (sender as DesignerControl).MainMenu.miFileSelectLanguage.Visible = false;
  45. /// }
  46. /// </code>
  47. /// </example>
  48. public event EventHandler DesignerLoaded;
  49. /// <summary>
  50. /// Occurs when the designer is closed.
  51. /// </summary>
  52. public event EventHandler DesignerClosed;
  53. /// <summary>
  54. /// Occurs when the report is loaded.
  55. /// </summary>
  56. public event ReportLoadedEventHandler ReportLoaded;
  57. /// <summary>
  58. /// Occurs when a report page or a dialog form is added to the report.
  59. /// </summary>
  60. /// <remarks>
  61. /// Use this event if you want to customize the page properties.
  62. /// </remarks>
  63. /// <example>
  64. /// This example demonstrates how to change the default page margins.
  65. /// <code>
  66. /// Config.DesignerSettings.PageAdded += new EventHandler(DesignerSettings_PageAdded);
  67. ///
  68. /// void DesignerSettings_PageAdded(object sender, EventArgs e)
  69. /// {
  70. /// if (sender is ReportPage)
  71. /// (sender as ReportPage).TopMargin = 0;
  72. /// }
  73. /// </code>
  74. /// </example>
  75. public event EventHandler PageAdded;
  76. /// <summary>
  77. /// Occurs when object is inserted.
  78. /// </summary>
  79. public event ObjectInsertedEventHandler ObjectInserted;
  80. /// <include file='../../FastReport/Resources/doc.xml' path='//CodeDoc/Topics/EnvironmentSettings/CustomOpenDialog/*'/>
  81. /// <include file='../../FastReport/Resources/doc.xml' path='//CodeDoc/Examples/EnvironmentSettings/*'/>
  82. public event OpenSaveDialogEventHandler CustomOpenDialog;
  83. /// <include file='../../FastReport/Resources/doc.xml' path='//CodeDoc/Topics/EnvironmentSettings/CustomSaveDialog/*'/>
  84. /// <include file='../../FastReport/Resources/doc.xml' path='//CodeDoc/Examples/EnvironmentSettings/*'/>
  85. public event OpenSaveDialogEventHandler CustomSaveDialog;
  86. /// <include file='../../FastReport/Resources/doc.xml' path='//CodeDoc/Topics/EnvironmentSettings/CustomOpenReport/*'/>
  87. /// <include file='../../FastReport/Resources/doc.xml' path='//CodeDoc/Examples/EnvironmentSettings/*'/>
  88. public event OpenSaveReportEventHandler CustomOpenReport;
  89. /// <include file='../../FastReport/Resources/doc.xml' path='//CodeDoc/Topics/EnvironmentSettings/CustomSaveReport/*'/>
  90. /// <include file='../../FastReport/Resources/doc.xml' path='//CodeDoc/Examples/EnvironmentSettings/*'/>
  91. public event OpenSaveReportEventHandler CustomSaveReport;
  92. /// <summary>
  93. /// Occurs when previewing a report from the designer.
  94. /// </summary>
  95. /// <remarks>
  96. /// Use this event to show own preview window.
  97. /// </remarks>
  98. /// <example>
  99. /// <code>
  100. /// Config.DesignerSettings.CustomPreviewReport += new EventHandler(MyPreviewHandler);
  101. ///
  102. /// private void MyPreviewHandler(object sender, EventArgs e)
  103. /// {
  104. /// Report report = sender as Report;
  105. /// using (MyPreviewForm form = new MyPreviewForm())
  106. /// {
  107. /// report.Preview = form.previewControl1;
  108. /// report.ShowPreparedReport();
  109. /// form.ShowDialog();
  110. /// }
  111. /// }
  112. /// </code>
  113. /// </example>
  114. public event EventHandler CustomPreviewReport;
  115. /// <summary>
  116. /// Occurs when getting available table names from the connection.
  117. /// </summary>
  118. /// <remarks>
  119. /// Use this handler to filter the list of tables returned by the connection object.
  120. /// </remarks>
  121. /// <example>
  122. /// This example demonstrates how to hide the table with "Table 1" name from the Data Wizard.
  123. /// <code>
  124. /// Config.DesignerSettings.FilterConnectionTables += DesignerSettings_FilterConnectionTables;
  125. ///
  126. /// private void DesignerSettings_FilterConnectionTables(object sender, FilterConnectionTablesEventArgs e)
  127. /// {
  128. /// if (e.TableName == "Table 1")
  129. /// e.Skip = true;
  130. /// }
  131. /// </code>
  132. /// </example>
  133. public event FilterConnectionTablesEventHandler FilterConnectionTables;
  134. /// <summary>
  135. /// Occurs when the query builder is called.
  136. /// </summary>
  137. /// <remarks>
  138. /// Subscribe to this event if you want to replace the embedded query builder with your own one.
  139. /// </remarks>
  140. public event CustomQueryBuilderEventHandler CustomQueryBuilder;
  141. /// <summary>
  142. /// Gets or sets the icon for the designer window.
  143. /// </summary>
  144. public Icon Icon
  145. {
  146. get { return icon; }
  147. set { icon = value; }
  148. }
  149. /// <summary>
  150. /// Gets or sets the default font used in a report.
  151. /// </summary>
  152. public Font DefaultFont
  153. {
  154. get { return defaultFont; }
  155. set { defaultFont = value; }
  156. }
  157. /// <summary>
  158. /// Gets or sets a value indicating whether the designer window is displayed in the Windows taskbar.
  159. /// </summary>
  160. [DefaultValue(false)]
  161. public bool ShowInTaskbar
  162. {
  163. get { return showInTaskbar; }
  164. set { showInTaskbar = value; }
  165. }
  166. /// <summary>
  167. /// Gets the designer restrictions flags.
  168. /// </summary>
  169. public DesignerRestrictions Restrictions
  170. {
  171. get { return restrictions; }
  172. set { restrictions = value; }
  173. }
  174. /// <summary>
  175. /// Gets or sets the title text for the designer window.
  176. /// </summary>
  177. /// <remarks>
  178. /// If no text is set, the default text "FastReport -" will be used.
  179. /// </remarks>
  180. public string Text
  181. {
  182. get { return text; }
  183. set { text = value; }
  184. }
  185. /// <summary>
  186. /// Gets or sets a value indicating whether the preview window is embedded into the designer form.
  187. /// </summary>
  188. public bool EmbeddedPreview { get; set; }
  189. /// <summary>
  190. /// Gets or sets application-defined DbConnection object that will be used in the designer
  191. /// to create a new datasource.
  192. /// </summary>
  193. /// <remarks>
  194. /// The application connection object is used in the "Data Wizard" to create new datasources.
  195. /// In this mode, you can't create any other connections in the wizard; only application
  196. /// connection is available. You still able to choose tables or create a new queries inside
  197. /// this connection. The connection information (ConnectionString) is not stored in the report file.
  198. /// </remarks>
  199. public DbConnection ApplicationConnection
  200. {
  201. get { return applicationConnection; }
  202. set
  203. {
  204. // be sure that add-ins were initialized
  205. Report dummyReport = new Report();
  206. dummyReport.Dispose();
  207. applicationConnection = value;
  208. FindConnectorType();
  209. }
  210. }
  211. /// <summary>
  212. /// Gets the toolstrip renderer.
  213. /// </summary>
  214. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  215. public ToolStripRenderer ToolStripRenderer
  216. {
  217. get
  218. {
  219. if (toolStripRenderer == null)
  220. {
  221. ProfessionalColorTable vs2005ColorTable = new ProfessionalColorTable();
  222. vs2005ColorTable.UseSystemColors = true;
  223. toolStripRenderer = new ToolStripProfessionalRenderer(vs2005ColorTable);
  224. }
  225. return toolStripRenderer;
  226. }
  227. }
  228. internal Type ApplicationConnectionType
  229. {
  230. get { return applicationConnectionType; }
  231. }
  232. internal List<ConnectionEntry> CustomConnections
  233. {
  234. get { return customConnections; }
  235. }
  236. private void FindConnectorType()
  237. {
  238. applicationConnectionType = null;
  239. if (applicationConnection == null)
  240. return;
  241. // find appropriate connector
  242. var dataConnections = new List<DataConnectionInfo>();
  243. RegisteredObjects.DataConnections.EnumItems(dataConnections);
  244. foreach (var dataConnection in dataConnections)
  245. {
  246. if (dataConnection.Object != null)
  247. {
  248. // MsAccessDataConnection is the subclass of OleDBDataConnection, skip it
  249. if (dataConnection.Object.Name == "MsAccessDataConnection")
  250. continue;
  251. using (DataConnectionBase conn = Activator.CreateInstance(dataConnection.Object) as DataConnectionBase)
  252. {
  253. if (conn.GetConnectionType() == applicationConnection.GetType())
  254. {
  255. applicationConnectionType = conn.GetType();
  256. return;
  257. }
  258. }
  259. }
  260. }
  261. throw new Exception(applicationConnection.GetType().Name + " connection is not supported.");
  262. }
  263. internal void OnDesignerLoaded(object sender, EventArgs e)
  264. {
  265. if (DesignerLoaded != null)
  266. DesignerLoaded(sender, e);
  267. }
  268. internal void OnDesignerClosed(object sender, EventArgs e)
  269. {
  270. if (DesignerClosed != null)
  271. DesignerClosed(sender, e);
  272. }
  273. internal void OnReportLoaded(object sender, ReportLoadedEventArgs e)
  274. {
  275. if (ReportLoaded != null)
  276. ReportLoaded(sender, e);
  277. }
  278. internal void OnPageAdded(object sender, EventArgs e)
  279. {
  280. if (PageAdded != null)
  281. PageAdded(sender, e);
  282. }
  283. internal void OnObjectInserted(object sender, ObjectInsertedEventArgs e)
  284. {
  285. if (ObjectInserted != null)
  286. ObjectInserted(sender, e);
  287. }
  288. internal void OnCustomOpenDialog(object sender, OpenSaveDialogEventArgs e)
  289. {
  290. if (CustomOpenDialog != null)
  291. CustomOpenDialog(sender, e);
  292. else
  293. {
  294. // standard open dialog
  295. using (OpenFileDialog dialog = new OpenFileDialog())
  296. {
  297. List<ImportPlugin> importPlugins = new List<ImportPlugin>();
  298. string filter = Res.Get("FileFilters,Report");
  299. foreach (IDesignerPlugin plugin in e.Designer.Plugins)
  300. {
  301. if (plugin is ImportPlugin)
  302. {
  303. importPlugins.Add(plugin as ImportPlugin);
  304. filter += "|" + (plugin as ImportPlugin).Filter;
  305. }
  306. }
  307. dialog.Filter = filter;
  308. dialog.FilterIndex = openDialogFilterIndex;
  309. e.Cancel = dialog.ShowDialog() != DialogResult.OK;
  310. e.FileName = dialog.FileName;
  311. if (!e.Cancel)
  312. {
  313. openDialogFilterIndex = dialog.FilterIndex;
  314. }
  315. if (dialog.FilterIndex > 1 && dialog.FilterIndex < (importPlugins.Count + 2))
  316. {
  317. e.Data = importPlugins[dialog.FilterIndex - 2];
  318. }
  319. }
  320. }
  321. }
  322. internal void OnCustomSaveDialog(object sender, OpenSaveDialogEventArgs e)
  323. {
  324. if (CustomSaveDialog != null)
  325. CustomSaveDialog(sender, e);
  326. else
  327. {
  328. // standard save dialog
  329. using (SaveFileDialog dialog = new SaveFileDialog())
  330. {
  331. dialog.InitialDirectory = Config.SaveFolder;
  332. string filter = Res.Get("FileFilters,Report");
  333. if (e.Designer.ActiveReport.ScriptLanguage == Language.CSharp)
  334. filter += "|" + Res.Get("FileFilters,CsFile");
  335. else
  336. filter += "|" + Res.Get("FileFilters,VbFile");
  337. List<ExportPlugin> exportPlugins = new List<ExportPlugin>();
  338. foreach (IDesignerPlugin plugin in e.Designer.Plugins)
  339. {
  340. if (plugin is ExportPlugin)
  341. {
  342. exportPlugins.Add(plugin as ExportPlugin);
  343. filter += "|" + (plugin as ExportPlugin).Filter;
  344. }
  345. }
  346. dialog.Filter = filter;
  347. dialog.FileName = e.FileName;
  348. dialog.DefaultExt = "frx";
  349. e.Cancel = dialog.ShowDialog() != DialogResult.OK;
  350. // SaveFileDialog bug workaround
  351. string[] filters = dialog.Filter.Split('|');
  352. string ext = Path.GetExtension(filters[(dialog.FilterIndex - 1) * 2 + 1]);
  353. e.FileName = Path.ChangeExtension(dialog.FileName, ext);
  354. if (dialog.FilterIndex != 1)
  355. {
  356. if (dialog.FilterIndex > 2)
  357. {
  358. e.Data = exportPlugins[dialog.FilterIndex - 3];
  359. }
  360. else
  361. {
  362. e.Data = dialog.FilterIndex;
  363. }
  364. e.IsPlugin = true;
  365. }
  366. }
  367. }
  368. }
  369. internal void OnOpenViaCloudReport(object sender, OpenSaveReportEventArgs e, Stream reportStream = null)
  370. {
  371. // standard open report
  372. if (e.Data == null)
  373. {
  374. e.Report.Load(reportStream);
  375. }
  376. OnReportLoaded(sender, new ReportLoadedEventArgs(e.Report));
  377. }
  378. internal void OnCustomOpenReport(object sender, OpenSaveReportEventArgs e)
  379. {
  380. if (CustomOpenReport != null)
  381. CustomOpenReport(sender, e);
  382. else
  383. {
  384. if (e.Data == null)
  385. {
  386. // standard open report
  387. try
  388. {
  389. e.Report.Load(e.FileName);
  390. }
  391. catch (DecryptException)
  392. {
  393. e.Report.Password = e.Report.ShowPasswordForm();
  394. e.Report.Load(e.FileName);
  395. }
  396. }
  397. else
  398. {
  399. (e.Data as ImportPlugin).LoadReport(e.Report, e.FileName);
  400. }
  401. }
  402. OnReportLoaded(sender, new ReportLoadedEventArgs(e.Report));
  403. }
  404. internal void OnCustomSaveReport(object sender, OpenSaveReportEventArgs e)
  405. {
  406. if (CustomSaveReport != null)
  407. CustomSaveReport(sender, e);
  408. else
  409. {
  410. // standard save report
  411. if (e.Data == null)
  412. e.Report.Save(e.FileName);
  413. else
  414. {
  415. if (e.Data is ExportPlugin)
  416. {
  417. (e.Data as ExportPlugin).SaveReport(e.Report, e.FileName);
  418. }
  419. else
  420. {
  421. e.Report.GenerateReportAssembly(e.FileName);
  422. }
  423. }
  424. }
  425. }
  426. internal void OnSaveReportWithRandomData(object sender, OpenSaveReportEventArgs e)
  427. {
  428. e.Report.SaveWithRandomData(e.FileName);
  429. }
  430. internal async Task OnCustomPreviewReport(object sender, EventArgs e)
  431. {
  432. #if Demo && !AVALONIA
  433. Throttle.Execute(() => FRMessageBox.Information("Demo version"), 1500);
  434. #endif
  435. Report report = sender as Report;
  436. if (CustomPreviewReport != null)
  437. {
  438. if (report.Prepare())
  439. CustomPreviewReport(report, e);
  440. }
  441. else if (EmbeddedPreview && report.Designer.FindForm().Name == "DesignerForm")
  442. {
  443. await report.Designer.ShowEmbeddedPreview(report);
  444. }
  445. else
  446. {
  447. PreviewControl savePreview = report.Preview;
  448. report.Preview = null;
  449. try
  450. {
  451. report.Show();
  452. }
  453. finally
  454. {
  455. report.Preview = savePreview;
  456. }
  457. }
  458. }
  459. internal void OnFilterConnectionTables(object sender, FilterConnectionTablesEventArgs e)
  460. {
  461. if (FilterConnectionTables != null)
  462. FilterConnectionTables(sender, e);
  463. }
  464. internal void OnCustomQueryBuilder(object sender, CustomQueryBuilderEventArgs e)
  465. {
  466. if (CustomQueryBuilder != null)
  467. CustomQueryBuilder(sender, e);
  468. else
  469. {
  470. FastReport.FastQueryBuilder.QueryBuilder qb = new FastReport.FastQueryBuilder.QueryBuilder(e.Connection);
  471. qb.UseJoin = true;
  472. qb.SetSql(e.SQL);
  473. if (qb.DesignQuery() == DialogResult.OK)
  474. e.SQL = qb.GetSql();
  475. }
  476. }
  477. /// <summary>
  478. /// Adds a custom connection used in the "Data Wizard" window.
  479. /// </summary>
  480. /// <remarks>
  481. /// Use this method to provide own connection strings for the "Data Wizard" dialog. To do this, you need
  482. /// to pass the type of connection object and connection string associated with it. You must use one of the
  483. /// connection objects registered in FastReport that inherit from the
  484. /// <see cref="FastReport.Data.DataConnectionBase"/> class.
  485. /// <para/>To clear the custom connections, use the <see cref="ClearCustomConnections"/> method.
  486. /// </remarks>
  487. /// <example>
  488. /// This example shows how to add own connection string.
  489. /// <code>
  490. /// Config.DesignerSettings.AddCustomConnection(typeof(MsAccessDataConnection), @"Data Source=c:\data.mdb");
  491. /// </code>
  492. /// </example>
  493. public void AddCustomConnection(Type connectionType, string connectionString)
  494. {
  495. if (!connectionType.IsSubclassOf(typeof(DataConnectionBase)))
  496. throw new Exception("The 'connectionType' parameter should be of the 'DataConnectionBase' type.");
  497. customConnections.Add(new ConnectionEntry(connectionType, connectionString));
  498. }
  499. /// <summary>
  500. /// Clears the custom connections added by the <b>AddCustomConnection</b> method.
  501. /// </summary>
  502. public void ClearCustomConnections()
  503. {
  504. customConnections.Clear();
  505. }
  506. /// <summary>
  507. /// Initializes a new instance of the <see cref="DesignerSettings"/> class.
  508. /// </summary>
  509. public DesignerSettings()
  510. {
  511. icon = ResourceLoader.GetIcon("icon16.ico");
  512. defaultFont = DrawUtils.DefaultReportFont;
  513. restrictions = new DesignerRestrictions();
  514. text = "";
  515. customConnections = new List<ConnectionEntry>();
  516. }
  517. }
  518. internal class ConnectionEntry
  519. {
  520. public Type type;
  521. public string connectionString;
  522. public ConnectionEntry(Type type, string connectionString)
  523. {
  524. this.type = type;
  525. this.connectionString = connectionString;
  526. }
  527. }
  528. }