SubreportObject.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System.ComponentModel;
  2. using System.Drawing;
  3. using System.Drawing.Design;
  4. using FastReport.Utils;
  5. namespace FastReport
  6. {
  7. /// <summary>
  8. /// Represents a subreport object.
  9. /// </summary>
  10. /// <remarks>
  11. /// To create a subreport in code, you should create the report page first and
  12. /// connect it to the subreport using the <see cref="ReportPage"/> property.
  13. /// </remarks>
  14. /// <example>The following example shows how to create a subreport object in code.
  15. /// <code>
  16. /// // create the main report page
  17. /// ReportPage reportPage = new ReportPage();
  18. /// reportPage.Name = "Page1";
  19. /// report.Pages.Add(reportPage);
  20. /// // create report title band
  21. /// reportPage.ReportTitle = new ReportTitleBand();
  22. /// reportPage.ReportTitle.Name = "ReportTitle1";
  23. /// reportPage.ReportTitle.Height = Units.Millimeters * 10;
  24. /// // add subreport on it
  25. /// SubreportObject subreport = new SubreportObject();
  26. /// subreport.Name = "Subreport1";
  27. /// subreport.Bounds = new RectangleF(0, 0, Units.Millimeters * 25, Units.Millimeters * 5);
  28. /// reportPage.ReportTitle.Objects.Add(subreport);
  29. /// // create subreport page
  30. /// ReportPage subreportPage = new ReportPage();
  31. /// subreportPage.Name = "SubreportPage1";
  32. /// report.Pages.Add(subreportPage);
  33. /// // connect the subreport to the subreport page
  34. /// subreport.ReportPage = subreportPage;
  35. /// </code>
  36. /// </example>
  37. public partial class SubreportObject : ReportComponentBase
  38. {
  39. private ReportPage reportPage;
  40. private bool printOnParent;
  41. #region Properties
  42. /// <summary>
  43. /// Gets or sets a report page that contains the subreport bands and objects.
  44. /// </summary>
  45. //[Browsable(false)]
  46. [Editor("FastReport.TypeEditors.SubreportPageEditor, FastReport", typeof(UITypeEditor))]
  47. [TypeConverter(typeof(FastReport.TypeConverters.ComponentRefConverter))]
  48. public ReportPage ReportPage
  49. {
  50. get { return reportPage; }
  51. set
  52. {
  53. if (value == Page)
  54. return;
  55. if (reportPage != null && value != reportPage)
  56. {
  57. RemoveSubReport(false);
  58. }
  59. if (value != null)
  60. {
  61. value.Subreport = this;
  62. value.PageName = Name;
  63. }
  64. reportPage = value;
  65. }
  66. }
  67. /// <summary>
  68. /// Gets or sets a value indicating that subreport must print its objects on a parent band to which it belongs.
  69. /// </summary>
  70. /// <remarks>
  71. /// Default behavior of the subreport is to print subreport objects they own separate bands.
  72. /// </remarks>
  73. [DefaultValue(false)]
  74. [Category("Behavior")]
  75. public bool PrintOnParent
  76. {
  77. get { return printOnParent; }
  78. set { printOnParent = value; }
  79. }
  80. #endregion
  81. private void RemoveSubReport(bool delete)
  82. {
  83. if (reportPage != null)
  84. {
  85. if (Report != null)
  86. {
  87. foreach (Base obj in Report.AllObjects)
  88. {
  89. if (obj is SubreportObject && obj != this)
  90. {
  91. SubreportObject subReport = obj as SubreportObject;
  92. if (subReport.ReportPage == reportPage)
  93. {
  94. reportPage.Subreport = subReport;
  95. reportPage.PageName = subReport.Name;
  96. reportPage = null;
  97. break;
  98. }
  99. }
  100. }
  101. }
  102. if (reportPage != null)
  103. {
  104. if (delete && Report != null)
  105. {
  106. reportPage.Dispose();
  107. }
  108. else
  109. {
  110. reportPage.Subreport = null;
  111. reportPage.PageName = reportPage.Name;
  112. }
  113. reportPage = null;
  114. }
  115. }
  116. }
  117. #region Public Methods
  118. /// <inheritdoc/>
  119. public override void Assign(Base source)
  120. {
  121. base.Assign(source);
  122. SubreportObject src = source as SubreportObject;
  123. PrintOnParent = src.PrintOnParent;
  124. }
  125. /// <inheritdoc/>
  126. public override void Serialize(FRWriter writer)
  127. {
  128. SubreportObject c = writer.DiffObject as SubreportObject;
  129. base.Serialize(writer);
  130. writer.WriteRef("ReportPage", ReportPage);
  131. if (PrintOnParent != c.PrintOnParent)
  132. writer.WriteBool("PrintOnParent", PrintOnParent);
  133. }
  134. #endregion
  135. /// <summary>
  136. /// Initializes a new instance of the <see cref="SubreportObject"/> class with default settings.
  137. /// </summary>
  138. public SubreportObject()
  139. {
  140. Fill = new SolidFill(SystemColors.Control);
  141. FlagUseBorder = false;
  142. FlagUseFill = false;
  143. FlagPreviewVisible = false;
  144. SetFlags(Flags.CanCopy, false);
  145. }
  146. }
  147. }