DesignerSettings.cs 21 KB

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