BandBase.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. using System;
  2. using System.Drawing;
  3. using System.ComponentModel;
  4. using System.Collections.Generic;
  5. using FastReport.Utils;
  6. using System.Windows.Forms;
  7. using System.Drawing.Drawing2D;
  8. using System.Drawing.Design;
  9. namespace FastReport
  10. {
  11. /// <summary>
  12. /// Base class for all bands.
  13. /// </summary>
  14. public abstract partial class BandBase : BreakableComponent, IParent
  15. {
  16. #region Fields
  17. private ChildBand child;
  18. private ReportComponentCollection objects;
  19. private FloatCollection guides;
  20. private bool startNewPage;
  21. private bool firstRowStartsNewPage;
  22. private bool printOnBottom;
  23. private bool keepChild;
  24. private string outlineExpression;
  25. private int rowNo;
  26. private int absRowNo;
  27. private bool isFirstRow;
  28. private bool isLastRow;
  29. private bool repeated;
  30. private bool updatingLayout;
  31. private bool flagUseStartNewPage;
  32. private bool flagCheckFreeSpace;
  33. private bool flagMustBreak;
  34. private int savedOriginalObjectsCount;
  35. private float reprintOffset;
  36. private string beforeLayoutEvent;
  37. private string afterLayoutEvent;
  38. private int repeatBandNTimes = 1;
  39. #endregion
  40. #region Properties
  41. /// <summary>
  42. /// This event occurs before the band layouts its child objects.
  43. /// </summary>
  44. public event EventHandler BeforeLayout;
  45. /// <summary>
  46. /// This event occurs after the child objects layout was finished.
  47. /// </summary>
  48. public event EventHandler AfterLayout;
  49. /// <summary>
  50. /// Gets or sets a value indicating that the band should be printed from a new page.
  51. /// </summary>
  52. /// <remarks>
  53. /// New page is not generated when printing very first group or data row. This is made to avoid empty
  54. /// first page.
  55. /// </remarks>
  56. [DefaultValue(false)]
  57. [Category("Behavior")]
  58. public bool StartNewPage
  59. {
  60. get { return startNewPage; }
  61. set { startNewPage = value; }
  62. }
  63. /// <summary>
  64. /// Gets or sets a value that determines the number of repetitions of the same band.
  65. /// </summary>
  66. [Category("Behavior")]
  67. [DefaultValue(1)]
  68. public int RepeatBandNTimes
  69. {
  70. get { return repeatBandNTimes; }
  71. set { repeatBandNTimes = value; }
  72. }
  73. /// <summary>
  74. /// Gets or sets a value indicating that the first row can start a new report page.
  75. /// </summary>
  76. /// <remarks>
  77. /// Use this property if <see cref="StartNewPage"/> is set to <b>true</b>. Normally the new page
  78. /// is not started when printing the first data row, to avoid empty first page.
  79. /// </remarks>
  80. [DefaultValue(true)]
  81. [Category("Behavior")]
  82. public bool FirstRowStartsNewPage
  83. {
  84. get { return firstRowStartsNewPage; }
  85. set { firstRowStartsNewPage = value; }
  86. }
  87. /// <summary>
  88. /// Gets or sets a value indicating that the band should be printed on the page bottom.
  89. /// </summary>
  90. [DefaultValue(false)]
  91. [Category("Behavior")]
  92. public bool PrintOnBottom
  93. {
  94. get { return printOnBottom; }
  95. set { printOnBottom = value; }
  96. }
  97. /// <summary>
  98. /// Gets or sets a value indicating that the band should be printed together with its child band.
  99. /// </summary>
  100. [DefaultValue(false)]
  101. [Category("Behavior")]
  102. public bool KeepChild
  103. {
  104. get { return keepChild; }
  105. set { keepChild = value; }
  106. }
  107. /// <summary>
  108. /// Gets or sets an outline expression.
  109. /// </summary>
  110. /// <remarks>
  111. /// <para>
  112. /// Outline is a tree control displayed in the preview window. It represents the prepared report structure.
  113. /// Each outline node can be clicked to navigate to the item in the prepared report.
  114. /// </para>
  115. /// <para>
  116. /// To create the outline, set this property to any valid expression that represents the outline node text.
  117. /// This expression will be calculated when band is about to print, and its value will be added to the
  118. /// outline. Thus, nodes' hierarchy in the outline is similar to the bands' hierarchy
  119. /// in a report. That means there will be the main and subordinate outline nodes, corresponding
  120. /// to the main and subordinate bands in a report (a report with two levels of data or with groups can
  121. /// exemplify the point).
  122. /// </para>
  123. /// </remarks>
  124. [Category("Navigation")]
  125. [Editor("FastReport.TypeEditors.ExpressionEditor, FastReport", typeof(UITypeEditor))]
  126. public string OutlineExpression
  127. {
  128. get { return outlineExpression; }
  129. set { outlineExpression = value; }
  130. }
  131. /// <summary>
  132. /// Gets or sets a child band that will be printed right after this band.
  133. /// </summary>
  134. /// <remarks>
  135. /// Typical use of child band is to print several objects that can grow or shrink. It also can be done
  136. /// using the shift feature (via <see cref="ShiftMode"/> property), but in some cases it's not possible.
  137. /// </remarks>
  138. [Browsable(false)]
  139. public ChildBand Child
  140. {
  141. get { return child; }
  142. set
  143. {
  144. SetProp(child, value);
  145. child = value;
  146. }
  147. }
  148. /// <summary>
  149. /// Gets a collection of report objects belongs to this band.
  150. /// </summary>
  151. [Browsable(false)]
  152. public ReportComponentCollection Objects
  153. {
  154. get { return objects; }
  155. }
  156. /// <summary>
  157. /// Gets a value indicating that band is reprinted on a new page.
  158. /// </summary>
  159. /// <remarks>
  160. /// This property is applicable to the <b>DataHeaderBand</b> and <b>GroupHeaderBand</b> only.
  161. /// It returns <b>true</b> if its <b>RepeatOnAllPages</b> property is <b>true</b> and band is
  162. /// reprinted on a new page.
  163. /// </remarks>
  164. [Browsable(false)]
  165. public bool Repeated
  166. {
  167. get { return repeated; }
  168. set
  169. {
  170. repeated = value;
  171. // set this flag for child bands as well
  172. BandBase child = Child;
  173. while (child != null)
  174. {
  175. child.Repeated = value;
  176. child = child.Child;
  177. }
  178. }
  179. }
  180. /// <summary>
  181. /// Gets or sets a script event name that will be fired before the band layouts its child objects.
  182. /// </summary>
  183. [Category("Build")]
  184. public string BeforeLayoutEvent
  185. {
  186. get { return beforeLayoutEvent; }
  187. set { beforeLayoutEvent = value; }
  188. }
  189. /// <summary>
  190. /// Gets or sets a script event name that will be fired after the child objects layout was finished.
  191. /// </summary>
  192. [Category("Build")]
  193. public string AfterLayoutEvent
  194. {
  195. get { return afterLayoutEvent; }
  196. set { afterLayoutEvent = value; }
  197. }
  198. /// <inheritdoc/>
  199. public override float AbsLeft
  200. {
  201. get { return IsRunning ? base.AbsLeft : Left; }
  202. }
  203. /// <inheritdoc/>
  204. public override float AbsTop
  205. {
  206. get { return IsRunning ? base.AbsTop : Top; }
  207. }
  208. /// <summary>
  209. /// Gets or sets collection of guide lines for this band.
  210. /// </summary>
  211. [Browsable(false)]
  212. public FloatCollection Guides
  213. {
  214. get { return guides; }
  215. set { guides = value; }
  216. }
  217. /// <summary>
  218. /// Gets a row number (the same value returned by the "Row#" system variable).
  219. /// </summary>
  220. /// <remarks>
  221. /// This property can be used when running a report. It may be useful to print hierarchical
  222. /// row numbers in a master-detail report, like this:
  223. /// <para/>1.1
  224. /// <para/>1.2
  225. /// <para/>2.1
  226. /// <para/>2.2
  227. /// <para/>To do this, put the Text object on a detail data band with the following text in it:
  228. /// <para/>[Data1.RowNo].[Data2.RowNo]
  229. /// </remarks>
  230. [Browsable(false)]
  231. public int RowNo
  232. {
  233. get { return rowNo; }
  234. set
  235. {
  236. rowNo = value;
  237. if (Child != null)
  238. Child.RowNo = value;
  239. }
  240. }
  241. /// <summary>
  242. /// Gets an absolute row number (the same value returned by the "AbsRow#" system variable).
  243. /// </summary>
  244. [Browsable(false)]
  245. public int AbsRowNo
  246. {
  247. get
  248. {
  249. return absRowNo;
  250. }
  251. set
  252. {
  253. absRowNo = value;
  254. if (Child != null)
  255. Child.AbsRowNo = value;
  256. }
  257. }
  258. /// <summary>
  259. /// Gets a value indicating that this is the first data row.
  260. /// </summary>
  261. [Browsable(false)]
  262. public bool IsFirstRow
  263. {
  264. get { return isFirstRow; }
  265. set { isFirstRow = value; }
  266. }
  267. /// <summary>
  268. /// Gets a value indicating that this is the last data row.
  269. /// </summary>
  270. [Browsable(false)]
  271. public bool IsLastRow
  272. {
  273. get { return isLastRow; }
  274. set { isLastRow = value; }
  275. }
  276. internal bool HasBorder
  277. {
  278. get { return !Border.Equals(new Border()); }
  279. }
  280. internal bool HasFill
  281. {
  282. get { return !Fill.IsTransparent; }
  283. }
  284. internal DataBand ParentDataBand
  285. {
  286. get
  287. {
  288. Base c = Parent;
  289. while (c != null)
  290. {
  291. if (c is DataBand)
  292. return c as DataBand;
  293. if (c is ReportPage && (c as ReportPage).Subreport != null)
  294. c = (c as ReportPage).Subreport;
  295. c = c.Parent;
  296. }
  297. return null;
  298. }
  299. }
  300. internal bool FlagUseStartNewPage
  301. {
  302. get { return flagUseStartNewPage; }
  303. set { flagUseStartNewPage = value; }
  304. }
  305. internal bool FlagCheckFreeSpace
  306. {
  307. get { return flagCheckFreeSpace; }
  308. set
  309. {
  310. flagCheckFreeSpace = value;
  311. // set flag for child bands as well
  312. BandBase child = Child;
  313. while (child != null)
  314. {
  315. child.FlagCheckFreeSpace = value;
  316. child = child.Child;
  317. }
  318. }
  319. }
  320. internal bool FlagMustBreak
  321. {
  322. get { return flagMustBreak; }
  323. set { flagMustBreak = value; }
  324. }
  325. internal float ReprintOffset
  326. {
  327. get { return reprintOffset; }
  328. set { reprintOffset = value; }
  329. }
  330. internal float PageWidth
  331. {
  332. get
  333. {
  334. ReportPage page = Page as ReportPage;
  335. if (page != null)
  336. return page.WidthInPixels - (page.LeftMargin + page.RightMargin) * Units.Millimeters;
  337. return 0;
  338. }
  339. }
  340. #endregion
  341. #region IParent Members
  342. /// <inheritdoc/>
  343. public virtual void GetChildObjects(ObjectCollection list)
  344. {
  345. foreach (ReportComponentBase obj in objects)
  346. {
  347. list.Add(obj);
  348. }
  349. if (!IsRunning)
  350. list.Add(child);
  351. }
  352. /// <inheritdoc/>
  353. public virtual bool CanContain(Base child)
  354. {
  355. if (IsRunning)
  356. return child is ReportComponentBase;
  357. return ((child is ReportComponentBase && !(child is BandBase)) || child is ChildBand);
  358. }
  359. /// <inheritdoc/>
  360. public virtual void AddChild(Base child)
  361. {
  362. if (child is ChildBand && !IsRunning)
  363. Child = child as ChildBand;
  364. else
  365. objects.Add(child as ReportComponentBase);
  366. }
  367. /// <inheritdoc/>
  368. public virtual void RemoveChild(Base child)
  369. {
  370. if (child is ChildBand && this.child == child as ChildBand)
  371. Child = null;
  372. else
  373. objects.Remove(child as ReportComponentBase);
  374. }
  375. /// <inheritdoc/>
  376. public virtual int GetChildOrder(Base child)
  377. {
  378. return objects.IndexOf(child as ReportComponentBase);
  379. }
  380. /// <inheritdoc/>
  381. public virtual void SetChildOrder(Base child, int order)
  382. {
  383. int oldOrder = child.ZOrder;
  384. if (oldOrder != -1 && order != -1 && oldOrder != order)
  385. {
  386. if (order > objects.Count)
  387. order = objects.Count;
  388. if (oldOrder <= order)
  389. order--;
  390. objects.Remove(child as ReportComponentBase);
  391. objects.Insert(order, child as ReportComponentBase);
  392. UpdateLayout(0, 0);
  393. }
  394. }
  395. /// <inheritdoc/>
  396. public virtual void UpdateLayout(float dx, float dy)
  397. {
  398. if (updatingLayout)
  399. return;
  400. updatingLayout = true;
  401. try
  402. {
  403. RectangleF remainingBounds = new RectangleF(0, 0, Width, Height);
  404. remainingBounds.Width += dx;
  405. remainingBounds.Height += dy;
  406. foreach (ReportComponentBase c in Objects)
  407. {
  408. if ((c.Anchor & AnchorStyles.Right) != 0)
  409. {
  410. if ((c.Anchor & AnchorStyles.Left) != 0)
  411. c.Width += dx;
  412. else
  413. c.Left += dx;
  414. }
  415. else if ((c.Anchor & AnchorStyles.Left) == 0)
  416. {
  417. c.Left += dx / 2;
  418. }
  419. if ((c.Anchor & AnchorStyles.Bottom) != 0)
  420. {
  421. if ((c.Anchor & AnchorStyles.Top) != 0)
  422. c.Height += dy;
  423. else
  424. c.Top += dy;
  425. }
  426. else if ((c.Anchor & AnchorStyles.Top) == 0)
  427. {
  428. c.Top += dy / 2;
  429. }
  430. switch (c.Dock)
  431. {
  432. case DockStyle.Left:
  433. c.Bounds = new RectangleF(remainingBounds.Left, remainingBounds.Top, c.Width, remainingBounds.Height);
  434. remainingBounds.X += c.Width;
  435. remainingBounds.Width -= c.Width;
  436. break;
  437. case DockStyle.Top:
  438. c.Bounds = new RectangleF(remainingBounds.Left, remainingBounds.Top, remainingBounds.Width, c.Height);
  439. remainingBounds.Y += c.Height;
  440. remainingBounds.Height -= c.Height;
  441. break;
  442. case DockStyle.Right:
  443. c.Bounds = new RectangleF(remainingBounds.Right - c.Width, remainingBounds.Top, c.Width, remainingBounds.Height);
  444. remainingBounds.Width -= c.Width;
  445. break;
  446. case DockStyle.Bottom:
  447. c.Bounds = new RectangleF(remainingBounds.Left, remainingBounds.Bottom - c.Height, remainingBounds.Width, c.Height);
  448. remainingBounds.Height -= c.Height;
  449. break;
  450. case DockStyle.Fill:
  451. c.Bounds = remainingBounds;
  452. remainingBounds.Width = 0;
  453. remainingBounds.Height = 0;
  454. break;
  455. }
  456. }
  457. }
  458. finally
  459. {
  460. updatingLayout = false;
  461. }
  462. }
  463. #endregion
  464. #region Public Methods
  465. /// <inheritdoc/>
  466. public override void Assign(Base source)
  467. {
  468. base.Assign(source);
  469. BandBase src = source as BandBase;
  470. Guides.Assign(src.Guides);
  471. StartNewPage = src.StartNewPage;
  472. FirstRowStartsNewPage = src.FirstRowStartsNewPage;
  473. PrintOnBottom = src.PrintOnBottom;
  474. KeepChild = src.KeepChild;
  475. OutlineExpression = src.OutlineExpression;
  476. BeforeLayoutEvent = src.BeforeLayoutEvent;
  477. AfterLayoutEvent = src.AfterLayoutEvent;
  478. RepeatBandNTimes = src.RepeatBandNTimes;
  479. }
  480. internal virtual void UpdateWidth()
  481. {
  482. // update band width. It is needed for anchor/dock
  483. ReportPage page = Page as ReportPage;
  484. if (page != null && !(page.UnlimitedWidth && IsDesigning))
  485. {
  486. if (page.Columns.Count <= 1 || !IsColumnDependentBand)
  487. Width = PageWidth;
  488. }
  489. }
  490. internal void FixHeight()
  491. {
  492. float maxHeight = Height;
  493. foreach (ReportComponentBase c in Objects)
  494. {
  495. if (c.Bottom > maxHeight)
  496. maxHeight = c.Bottom;
  497. }
  498. if (maxHeight < 0)
  499. maxHeight = 0;
  500. Height = maxHeight;
  501. Validator.ValidateIntersectionAllObjects(this);
  502. }
  503. internal void FixHeightWithComponentsShift(float deltaY)
  504. {
  505. float minTop = Height;
  506. float maxBottom = 0;
  507. float minHeight = Height;
  508. // Calculate minimum top of all components on this band.
  509. foreach (ReportComponentBase component in Objects)
  510. {
  511. if (component.Top < minTop)
  512. {
  513. minTop = component.Top;
  514. }
  515. }
  516. // Calculate maximum bottom of all components on this band.
  517. foreach (ReportComponentBase component in Objects)
  518. {
  519. if (component.Bottom > maxBottom)
  520. {
  521. maxBottom = component.Bottom;
  522. }
  523. }
  524. // Calculate minimum height of band with components shift.
  525. minHeight = maxBottom - minTop;
  526. // Minimum height with compenents shift can't be negative.
  527. if (minHeight < 0)
  528. {
  529. minHeight = 0;
  530. }
  531. // Prevent incorrect movement of objects when mouse moves too fast.
  532. if (minTop < deltaY)
  533. {
  534. deltaY = minTop;
  535. }
  536. // Size of band should be decreased.
  537. if (deltaY > 0)
  538. {
  539. // There is enough place to move components up.
  540. if (minTop > 0)
  541. {
  542. // Move all components up.
  543. foreach (ReportComponentBase component in Objects)
  544. {
  545. component.Top -= deltaY;
  546. }
  547. }
  548. }
  549. else
  550. {
  551. // Move all components down.
  552. foreach (ReportComponentBase component in Objects)
  553. {
  554. component.Top -= deltaY;
  555. }
  556. }
  557. // Height can't be less then minHeight.
  558. if (Height < minHeight)
  559. {
  560. Height = minHeight;
  561. }
  562. }
  563. /// <inheritdoc/>
  564. public override List<ValidationError> Validate()
  565. {
  566. return new List<ValidationError>();
  567. }
  568. /// <inheritdoc/>
  569. public override void Serialize(FRWriter writer)
  570. {
  571. BandBase c = writer.DiffObject as BandBase;
  572. base.Serialize(writer);
  573. if (writer.SerializeTo == SerializeTo.Preview)
  574. return;
  575. if (StartNewPage != c.StartNewPage)
  576. writer.WriteBool("StartNewPage", StartNewPage);
  577. if (FirstRowStartsNewPage != c.FirstRowStartsNewPage)
  578. writer.WriteBool("FirstRowStartsNewPage", FirstRowStartsNewPage);
  579. if (PrintOnBottom != c.PrintOnBottom)
  580. writer.WriteBool("PrintOnBottom", PrintOnBottom);
  581. if (KeepChild != c.KeepChild)
  582. writer.WriteBool("KeepChild", KeepChild);
  583. if (OutlineExpression != c.OutlineExpression)
  584. writer.WriteStr("OutlineExpression", OutlineExpression);
  585. if (Guides.Count > 0)
  586. writer.WriteValue("Guides", Guides);
  587. if (BeforeLayoutEvent != c.BeforeLayoutEvent)
  588. writer.WriteStr("BeforeLayoutEvent", BeforeLayoutEvent);
  589. if (AfterLayoutEvent != c.AfterLayoutEvent)
  590. writer.WriteStr("AfterLayoutEvent", AfterLayoutEvent);
  591. if (RepeatBandNTimes != c.RepeatBandNTimes)
  592. writer.WriteInt("RepeatBandNTimes", RepeatBandNTimes);
  593. }
  594. internal bool IsColumnDependentBand
  595. {
  596. get
  597. {
  598. BandBase b = this;
  599. if (b is ChildBand)
  600. {
  601. while (b is ChildBand)
  602. {
  603. b = b.Parent as BandBase;
  604. }
  605. }
  606. if (b is DataHeaderBand || b is DataBand || b is DataFooterBand ||
  607. b is GroupHeaderBand || b is GroupFooterBand ||
  608. b is ColumnHeaderBand || b is ColumnFooterBand || b is ReportSummaryBand)
  609. return true;
  610. return false;
  611. }
  612. }
  613. #endregion
  614. #region Report Engine
  615. internal void SetUpdatingLayout(bool value)
  616. {
  617. updatingLayout = value;
  618. }
  619. /// <inheritdoc/>
  620. public override string[] GetExpressions()
  621. {
  622. List<string> expressions = new List<string>();
  623. expressions.AddRange(base.GetExpressions());
  624. if (!String.IsNullOrEmpty(OutlineExpression))
  625. expressions.Add(OutlineExpression);
  626. return expressions.ToArray();
  627. }
  628. /// <inheritdoc/>
  629. public override void SaveState()
  630. {
  631. base.SaveState();
  632. savedOriginalObjectsCount = Objects.Count;
  633. SetRunning(true);
  634. SetDesigning(false);
  635. OnBeforePrint(EventArgs.Empty);
  636. foreach (ReportComponentBase obj in Objects)
  637. {
  638. obj.SaveState();
  639. obj.SetRunning(true);
  640. obj.SetDesigning(false);
  641. obj.OnBeforePrint(EventArgs.Empty);
  642. }
  643. //Report.Engine.TranslatedObjectsToBand(this);
  644. // apply even style
  645. if (RowNo % 2 == 0)
  646. {
  647. ApplyEvenStyle();
  648. foreach (ReportComponentBase obj in Objects)
  649. {
  650. obj.ApplyEvenStyle();
  651. }
  652. }
  653. }
  654. /// <inheritdoc/>
  655. public override void RestoreState()
  656. {
  657. OnAfterPrint(EventArgs.Empty);
  658. base.RestoreState();
  659. while (Objects.Count > savedOriginalObjectsCount)
  660. {
  661. Objects[Objects.Count - 1].Dispose();
  662. }
  663. SetRunning(false);
  664. ReportComponentCollection collection_clone = new ReportComponentCollection();
  665. Objects.CopyTo(collection_clone);
  666. foreach (ReportComponentBase obj in collection_clone)
  667. {
  668. obj.OnAfterPrint(EventArgs.Empty);
  669. obj.RestoreState();
  670. obj.SetRunning(false);
  671. }
  672. }
  673. /// <inheritdoc/>
  674. public override float CalcHeight()
  675. {
  676. OnBeforeLayout(EventArgs.Empty);
  677. // sort objects by Top
  678. ReportComponentCollection sortedObjects = Objects.SortByTop();
  679. // calc height of each object
  680. float[] heights = new float[sortedObjects.Count];
  681. for (int i = 0; i < sortedObjects.Count; i++)
  682. {
  683. ReportComponentBase obj = sortedObjects[i];
  684. float height = obj.Height;
  685. if (obj.Visible && (obj.CanGrow || obj.CanShrink))
  686. {
  687. float height1 = obj.CalcHeight();
  688. if ((obj.CanGrow && height1 > height) || (obj.CanShrink && height1 < height))
  689. height = height1;
  690. }
  691. heights[i] = height;
  692. }
  693. // calc shift amounts
  694. float[] shifts = new float[sortedObjects.Count];
  695. for (int i = 0; i < sortedObjects.Count; i++)
  696. {
  697. ReportComponentBase parent = sortedObjects[i];
  698. float shift = heights[i] - parent.Height;
  699. if (shift == 0)
  700. continue;
  701. for (int j = i + 1; j < sortedObjects.Count; j++)
  702. {
  703. ReportComponentBase child = sortedObjects[j];
  704. if (child.ShiftMode == ShiftMode.Never)
  705. continue;
  706. if (child.Top >= parent.Bottom - 1e-4)
  707. {
  708. if (child.ShiftMode == ShiftMode.WhenOverlapped &&
  709. (child.Left > parent.Right - 1e-4 || parent.Left > child.Right - 1e-4))
  710. continue;
  711. float parentShift = shifts[i];
  712. float childShift = shifts[j];
  713. if (shift > 0)
  714. childShift = Math.Max(shift + parentShift, childShift);
  715. else
  716. childShift = Math.Min(shift + parentShift, childShift);
  717. shifts[j] = childShift;
  718. }
  719. }
  720. }
  721. // update location and size of each component, calc max height
  722. float maxHeight = 0;
  723. for (int i = 0; i < sortedObjects.Count; i++)
  724. {
  725. ReportComponentBase obj = sortedObjects[i];
  726. DockStyle saveDock = obj.Dock;
  727. obj.Dock = DockStyle.None;
  728. obj.Height = heights[i];
  729. obj.Top += shifts[i];
  730. if (obj.Visible && obj.Bottom > maxHeight)
  731. maxHeight = obj.Bottom;
  732. obj.Dock = saveDock;
  733. }
  734. if ((CanGrow && maxHeight > Height) || (CanShrink && maxHeight < Height))
  735. Height = maxHeight;
  736. // perform grow to bottom
  737. foreach (ReportComponentBase obj in Objects)
  738. {
  739. if (obj.GrowToBottom)
  740. obj.Height = Height - obj.Top;
  741. }
  742. OnAfterLayout(EventArgs.Empty);
  743. return Height;
  744. }
  745. /// <inheritdoc/>
  746. public void AddLastToFooter(BreakableComponent breakTo)
  747. {
  748. float maxTop = (AllObjects[0] as ComponentBase).Top;
  749. foreach (ComponentBase obj in AllObjects)
  750. if (obj.Top > maxTop && !(obj is DataFooterBand))
  751. maxTop = obj.Top;
  752. float breakLine = maxTop;
  753. List<ReportComponentBase> pasteList = new List<ReportComponentBase>();
  754. foreach (ReportComponentBase obj in Objects)
  755. if (obj.Bottom > breakLine)
  756. pasteList.Add(obj);
  757. int itemsBefore = breakTo.AllObjects.Count;
  758. foreach (ReportComponentBase obj in pasteList)
  759. {
  760. if (obj.Top < breakLine)
  761. {
  762. BreakableComponent breakComp = Activator.CreateInstance(obj.GetType()) as BreakableComponent;
  763. breakComp.AssignAll(obj);
  764. breakComp.Parent = breakTo;
  765. breakComp.CanGrow = true;
  766. breakComp.CanShrink = false;
  767. breakComp.Height -= breakLine - obj.Top;
  768. breakComp.Top = 0;
  769. obj.Height = breakLine - obj.Top;
  770. (obj as BreakableComponent).Break(breakComp);
  771. }
  772. else
  773. {
  774. obj.Top -= breakLine;
  775. obj.Parent = breakTo;
  776. continue;
  777. }
  778. }
  779. float minTop = (breakTo.AllObjects[0] as ComponentBase).Top;
  780. float maxBottom = 0;
  781. for (int i = itemsBefore; i < breakTo.AllObjects.Count; i++)
  782. if ((breakTo.AllObjects[i] as ComponentBase).Top < minTop && breakTo.AllObjects[i] is ReportComponentBase && !(breakTo.AllObjects[i] is Table.TableCell))
  783. minTop = (breakTo.AllObjects[i] as ComponentBase).Top;
  784. for (int i = itemsBefore; i < breakTo.AllObjects.Count; i++)
  785. if ((breakTo.AllObjects[i] as ComponentBase).Bottom > maxBottom && breakTo.AllObjects[i] is ReportComponentBase && !(breakTo.AllObjects[i] is Table.TableCell))
  786. maxBottom = (breakTo.AllObjects[i] as ComponentBase).Bottom;
  787. for (int i = 0; i < itemsBefore; i++)
  788. (breakTo.AllObjects[i] as ComponentBase).Top += maxBottom - minTop;
  789. breakTo.Height += maxBottom - minTop;
  790. Height -= maxBottom - minTop;
  791. }
  792. /// <inheritdoc/>
  793. public override bool Break(BreakableComponent breakTo)
  794. {
  795. // first we find the break line. It's a minimum Top coordinate of the object that cannot break.
  796. float breakLine = Height;
  797. bool breakLineFound = true;
  798. do
  799. {
  800. breakLineFound = true;
  801. foreach (ReportComponentBase obj in Objects)
  802. {
  803. bool canBreak = true;
  804. if (obj.Top < breakLine && obj.Bottom > breakLine)
  805. {
  806. canBreak = false;
  807. BreakableComponent breakable = obj as BreakableComponent;
  808. if (breakable != null && breakable.CanBreak)
  809. {
  810. using (BreakableComponent clone = Activator.CreateInstance(breakable.GetType()) as BreakableComponent)
  811. {
  812. clone.AssignAll(breakable);
  813. clone.Height = breakLine - clone.Top;
  814. // to allow access to the Report
  815. clone.Parent = breakTo;
  816. canBreak = clone.Break(null);
  817. }
  818. }
  819. }
  820. if (!canBreak)
  821. {
  822. breakLine = Math.Min(obj.Top, breakLine);
  823. // enumerate objects again
  824. breakLineFound = false;
  825. break;
  826. }
  827. }
  828. }
  829. while (!breakLineFound);
  830. // now break the components
  831. int i = 0;
  832. while (i < Objects.Count)
  833. {
  834. ReportComponentBase obj = Objects[i];
  835. if (obj.Bottom > breakLine)
  836. {
  837. if (obj.Top < breakLine)
  838. {
  839. BreakableComponent breakComp = Activator.CreateInstance(obj.GetType()) as BreakableComponent;
  840. breakComp.AssignAll(obj);
  841. breakComp.Parent = breakTo;
  842. breakComp.CanGrow = true;
  843. breakComp.CanShrink = false;
  844. breakComp.Height -= breakLine - obj.Top;
  845. breakComp.Top = 0;
  846. obj.Height = breakLine - obj.Top;
  847. (obj as BreakableComponent).Break(breakComp);
  848. }
  849. else
  850. {
  851. // (case: object with Anchor = bottom on a breakable band)
  852. // in case of bottom anchor, do not move the object. It will be moved automatically when we decrease the band height
  853. if ((obj.Anchor & AnchorStyles.Bottom) == 0)
  854. obj.Top -= breakLine;
  855. obj.Parent = breakTo;
  856. continue;
  857. }
  858. }
  859. i++;
  860. }
  861. Height = breakLine;
  862. breakTo.Height -= breakLine;
  863. return Objects.Count > 0;
  864. }
  865. /// <inheritdoc/>
  866. public override void GetData()
  867. {
  868. base.GetData();
  869. FRCollectionBase list = new FRCollectionBase();
  870. Objects.CopyTo(list);
  871. foreach (ReportComponentBase obj in list)
  872. {
  873. obj.GetData();
  874. obj.OnAfterData();
  875. // break the component if it is of BreakableComponent an has non-empty BreakTo property
  876. if (obj is BreakableComponent && (obj as BreakableComponent).BreakTo != null &&
  877. (obj as BreakableComponent).BreakTo.GetType() == obj.GetType())
  878. (obj as BreakableComponent).Break((obj as BreakableComponent).BreakTo);
  879. }
  880. OnAfterData();
  881. }
  882. internal virtual bool IsEmpty()
  883. {
  884. return true;
  885. }
  886. private void AddBookmark(ReportComponentBase obj)
  887. {
  888. if (Report != null)
  889. Report.Engine.AddBookmark(obj.Bookmark);
  890. }
  891. internal void AddBookmarks()
  892. {
  893. AddBookmark(this);
  894. foreach (ReportComponentBase obj in Objects)
  895. {
  896. AddBookmark(obj);
  897. }
  898. }
  899. /// <inheritdoc/>
  900. public override void InitializeComponent()
  901. {
  902. base.InitializeComponent();
  903. AbsRowNo = 0;
  904. }
  905. /// <summary>
  906. /// This method fires the <b>BeforeLayout</b> event and the script code connected to the <b>BeforeLayoutEvent</b>.
  907. /// </summary>
  908. /// <param name="e">Event data.</param>
  909. public void OnBeforeLayout(EventArgs e)
  910. {
  911. if (BeforeLayout != null)
  912. BeforeLayout(this, e);
  913. InvokeEvent(BeforeLayoutEvent, e);
  914. }
  915. /// <summary>
  916. /// This method fires the <b>AfterLayout</b> event and the script code connected to the <b>AfterLayoutEvent</b>.
  917. /// </summary>
  918. /// <param name="e">Event data.</param>
  919. public void OnAfterLayout(EventArgs e)
  920. {
  921. if (AfterLayout != null)
  922. AfterLayout(this, e);
  923. InvokeEvent(AfterLayoutEvent, e);
  924. }
  925. #endregion
  926. /// <summary>
  927. /// Initializes a new instance of the <see cref="BandBase"/> class with default settings.
  928. /// </summary>
  929. public BandBase()
  930. {
  931. objects = new ReportComponentCollection(this);
  932. guides = new FloatCollection();
  933. beforeLayoutEvent = "";
  934. afterLayoutEvent = "";
  935. outlineExpression = "";
  936. CanBreak = false;
  937. ShiftMode = ShiftMode.Never;
  938. if (BaseName.EndsWith("Band"))
  939. BaseName = ClassName.Remove(ClassName.IndexOf("Band"));
  940. SetFlags(Flags.CanMove | Flags.CanChangeOrder | Flags.CanChangeParent | Flags.CanCopy | Flags.CanGroup, false);
  941. FlagUseStartNewPage = true;
  942. FlagCheckFreeSpace = true;
  943. }
  944. }
  945. }