BandBase.cs 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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. IsLastRow = src.IsLastRow;
  480. }
  481. internal virtual void UpdateWidth()
  482. {
  483. // update band width. It is needed for anchor/dock
  484. ReportPage page = Page as ReportPage;
  485. if (page != null && !(page.UnlimitedWidth && IsDesigning))
  486. {
  487. if (page.Columns.Count <= 1 || !IsColumnDependentBand)
  488. Width = PageWidth;
  489. }
  490. }
  491. /// <inheritdoc/>
  492. public override List<ValidationError> Validate()
  493. {
  494. return new List<ValidationError>();
  495. }
  496. /// <inheritdoc/>
  497. public override void Serialize(FRWriter writer)
  498. {
  499. BandBase c = writer.DiffObject as BandBase;
  500. base.Serialize(writer);
  501. if (writer.SerializeTo == SerializeTo.Preview)
  502. return;
  503. if (StartNewPage != c.StartNewPage)
  504. writer.WriteBool("StartNewPage", StartNewPage);
  505. if (FirstRowStartsNewPage != c.FirstRowStartsNewPage)
  506. writer.WriteBool("FirstRowStartsNewPage", FirstRowStartsNewPage);
  507. if (PrintOnBottom != c.PrintOnBottom)
  508. writer.WriteBool("PrintOnBottom", PrintOnBottom);
  509. if (KeepChild != c.KeepChild)
  510. writer.WriteBool("KeepChild", KeepChild);
  511. if (OutlineExpression != c.OutlineExpression)
  512. writer.WriteStr("OutlineExpression", OutlineExpression);
  513. if (Guides.Count > 0)
  514. writer.WriteValue("Guides", Guides);
  515. if (BeforeLayoutEvent != c.BeforeLayoutEvent)
  516. writer.WriteStr("BeforeLayoutEvent", BeforeLayoutEvent);
  517. if (AfterLayoutEvent != c.AfterLayoutEvent)
  518. writer.WriteStr("AfterLayoutEvent", AfterLayoutEvent);
  519. if (RepeatBandNTimes != c.RepeatBandNTimes)
  520. writer.WriteInt("RepeatBandNTimes", RepeatBandNTimes);
  521. }
  522. internal bool IsColumnDependentBand
  523. {
  524. get
  525. {
  526. BandBase b = this;
  527. if (b is ChildBand)
  528. {
  529. while (b is ChildBand)
  530. {
  531. b = b.Parent as BandBase;
  532. }
  533. }
  534. if (b is DataHeaderBand || b is DataBand || b is DataFooterBand ||
  535. b is GroupHeaderBand || b is GroupFooterBand ||
  536. b is ColumnHeaderBand || b is ColumnFooterBand || b is ReportSummaryBand)
  537. return true;
  538. return false;
  539. }
  540. }
  541. #endregion
  542. #region Report Engine
  543. internal void SetUpdatingLayout(bool value)
  544. {
  545. updatingLayout = value;
  546. }
  547. /// <inheritdoc/>
  548. public override string[] GetExpressions()
  549. {
  550. List<string> expressions = new List<string>();
  551. expressions.AddRange(base.GetExpressions());
  552. if (!String.IsNullOrEmpty(OutlineExpression))
  553. expressions.Add(OutlineExpression);
  554. return expressions.ToArray();
  555. }
  556. /// <inheritdoc/>
  557. public override void SaveState()
  558. {
  559. base.SaveState();
  560. savedOriginalObjectsCount = Objects.Count;
  561. SetRunning(true);
  562. SetDesigning(false);
  563. OnBeforePrint(EventArgs.Empty);
  564. foreach (ReportComponentBase obj in Objects)
  565. {
  566. obj.SaveState();
  567. obj.SetRunning(true);
  568. obj.SetDesigning(false);
  569. obj.OnBeforePrint(EventArgs.Empty);
  570. }
  571. //Report.Engine.TranslatedObjectsToBand(this);
  572. // apply even style
  573. if (RowNo % 2 == 0)
  574. {
  575. ApplyEvenStyle();
  576. foreach (ReportComponentBase obj in Objects)
  577. {
  578. obj.ApplyEvenStyle();
  579. }
  580. }
  581. }
  582. /// <inheritdoc/>
  583. public override void RestoreState()
  584. {
  585. OnAfterPrint(EventArgs.Empty);
  586. base.RestoreState();
  587. while (Objects.Count > savedOriginalObjectsCount)
  588. {
  589. Objects[Objects.Count - 1].Dispose();
  590. }
  591. SetRunning(false);
  592. ReportComponentCollection collection_clone = new ReportComponentCollection();
  593. Objects.CopyTo(collection_clone);
  594. foreach (ReportComponentBase obj in collection_clone)
  595. {
  596. obj.OnAfterPrint(EventArgs.Empty);
  597. obj.RestoreState();
  598. obj.SetRunning(false);
  599. }
  600. }
  601. /// <inheritdoc/>
  602. public override float CalcHeight()
  603. {
  604. OnBeforeLayout(EventArgs.Empty);
  605. // sort objects by Top
  606. ReportComponentCollection sortedObjects = Objects.SortByTop();
  607. // calc height of each object
  608. float[] heights = new float[sortedObjects.Count];
  609. for (int i = 0; i < sortedObjects.Count; i++)
  610. {
  611. ReportComponentBase obj = sortedObjects[i];
  612. float height = obj.Height;
  613. if (obj.Visible && (obj.CanGrow || obj.CanShrink))
  614. {
  615. float height1 = obj.CalcHeight();
  616. if ((obj.CanGrow && height1 > height) || (obj.CanShrink && height1 < height))
  617. height = height1;
  618. }
  619. heights[i] = height;
  620. }
  621. // calc shift amounts
  622. float[] shifts = new float[sortedObjects.Count];
  623. for (int i = 0; i < sortedObjects.Count; i++)
  624. {
  625. ReportComponentBase parent = sortedObjects[i];
  626. float shift = heights[i] - parent.Height;
  627. if (shift == 0)
  628. continue;
  629. for (int j = i + 1; j < sortedObjects.Count; j++)
  630. {
  631. ReportComponentBase child = sortedObjects[j];
  632. if (child.ShiftMode == ShiftMode.Never)
  633. continue;
  634. if (child.Top >= parent.Bottom - 1e-4)
  635. {
  636. if (child.ShiftMode == ShiftMode.WhenOverlapped &&
  637. (child.Left > parent.Right - 1e-4 || parent.Left > child.Right - 1e-4))
  638. continue;
  639. float parentShift = shifts[i];
  640. float childShift = shifts[j];
  641. if (shift > 0)
  642. childShift = Math.Max(shift + parentShift, childShift);
  643. else
  644. childShift = Math.Min(shift + parentShift, childShift);
  645. shifts[j] = childShift;
  646. }
  647. }
  648. }
  649. // update location and size of each component, calc max height
  650. float maxHeight = 0;
  651. for (int i = 0; i < sortedObjects.Count; i++)
  652. {
  653. ReportComponentBase obj = sortedObjects[i];
  654. DockStyle saveDock = obj.Dock;
  655. obj.Dock = DockStyle.None;
  656. obj.Height = heights[i];
  657. obj.Top += shifts[i];
  658. if (obj.Visible && obj.Bottom > maxHeight)
  659. maxHeight = obj.Bottom;
  660. obj.Dock = saveDock;
  661. }
  662. if ((CanGrow && maxHeight > Height) || (CanShrink && maxHeight < Height))
  663. Height = maxHeight;
  664. // perform grow to bottom
  665. foreach (ReportComponentBase obj in Objects)
  666. {
  667. if (obj.GrowToBottom)
  668. {
  669. obj.Height = Height - obj.Top;
  670. // reserve place for border
  671. if (IsLastRow && obj.Border.Lines.HasFlag(BorderLines.Bottom))
  672. obj.Height -= Border.BottomLine.Width;
  673. }
  674. }
  675. OnAfterLayout(EventArgs.Empty);
  676. return Height;
  677. }
  678. /// <inheritdoc/>
  679. public void AddLastToFooter(BreakableComponent breakTo)
  680. {
  681. float maxTop = (AllObjects[0] as ComponentBase).Top;
  682. for (int i = 0; i < AllObjects.Count; i++)
  683. {
  684. if (AllObjects[i] is ComponentBase)
  685. {
  686. ComponentBase obj = AllObjects[i] as ComponentBase;
  687. if (obj.Top > maxTop && !(obj is DataFooterBand))
  688. maxTop = obj.Top;
  689. }
  690. }
  691. float breakLine = maxTop;
  692. List<ReportComponentBase> pasteList = new List<ReportComponentBase>();
  693. foreach (ReportComponentBase obj in Objects)
  694. if (obj.Bottom > breakLine)
  695. pasteList.Add(obj);
  696. int itemsBefore = breakTo.AllObjects.Count;
  697. foreach (ReportComponentBase obj in pasteList)
  698. {
  699. if (obj.Top < breakLine)
  700. {
  701. BreakableComponent breakComp = Activator.CreateInstance(obj.GetType()) as BreakableComponent;
  702. breakComp.AssignAll(obj);
  703. breakComp.Parent = breakTo;
  704. breakComp.CanGrow = true;
  705. breakComp.CanShrink = false;
  706. breakComp.Height -= breakLine - obj.Top;
  707. breakComp.Top = 0;
  708. obj.Height = breakLine - obj.Top;
  709. (obj as BreakableComponent).Break(breakComp);
  710. }
  711. else
  712. {
  713. obj.Top -= breakLine;
  714. obj.Parent = breakTo;
  715. continue;
  716. }
  717. }
  718. float minTop = (breakTo.AllObjects[0] as ComponentBase).Top;
  719. float maxBottom = 0;
  720. for (int i = itemsBefore; i < breakTo.AllObjects.Count; i++)
  721. if (breakTo.AllObjects[i] is ComponentBase)
  722. if ((breakTo.AllObjects[i] as ComponentBase).Top < minTop && breakTo.AllObjects[i] is ReportComponentBase && !(breakTo.AllObjects[i] is Table.TableCell))
  723. minTop = (breakTo.AllObjects[i] as ComponentBase).Top;
  724. for (int i = itemsBefore; i < breakTo.AllObjects.Count; i++)
  725. if (breakTo.AllObjects[i] is ComponentBase)
  726. if ((breakTo.AllObjects[i] as ComponentBase).Bottom > maxBottom && breakTo.AllObjects[i] is ReportComponentBase && !(breakTo.AllObjects[i] is Table.TableCell))
  727. maxBottom = (breakTo.AllObjects[i] as ComponentBase).Bottom;
  728. for (int i = 0; i < itemsBefore; i++)
  729. if (breakTo.AllObjects[i] is ComponentBase)
  730. (breakTo.AllObjects[i] as ComponentBase).Top += maxBottom - minTop;
  731. breakTo.Height += maxBottom - minTop;
  732. Height -= maxBottom - minTop;
  733. }
  734. /// <inheritdoc/>
  735. public override bool Break(BreakableComponent breakTo)
  736. {
  737. // first we find the break line. It's a minimum Top coordinate of the object that cannot break.
  738. float breakLine = Height;
  739. float excessiveHeight = 0;
  740. float maxExcessiveHeight = 0;
  741. Dictionary<RectangleF, float> excessiveHeights = new Dictionary<RectangleF, float>();
  742. bool breakLineFound = true;
  743. do
  744. {
  745. breakLineFound = true;
  746. foreach (ReportComponentBase obj in Objects)
  747. {
  748. bool canBreak = true;
  749. if (obj.Top < breakLine && obj.Bottom > breakLine)
  750. {
  751. canBreak = false;
  752. BreakableComponent breakable = obj as BreakableComponent;
  753. if (breakable != null && breakable.CanBreak)
  754. {
  755. using (BreakableComponent clone = Activator.CreateInstance(breakable.GetType()) as BreakableComponent)
  756. {
  757. clone.AssignAll(breakable);
  758. clone.Height = breakLine - clone.Top;
  759. // to allow access to the Report
  760. clone.Parent = breakTo;
  761. canBreak = clone.Break(null, out excessiveHeight);
  762. if (excessiveHeight > 0)
  763. {
  764. excessiveHeights.Add(clone.Bounds, excessiveHeight);
  765. maxExcessiveHeight = Math.Max(maxExcessiveHeight, excessiveHeight);
  766. }
  767. }
  768. }
  769. }
  770. if (!canBreak)
  771. {
  772. breakLine = Math.Min(obj.Top, breakLine);
  773. // enumerate objects again
  774. breakLineFound = false;
  775. break;
  776. }
  777. }
  778. }
  779. while (!breakLineFound);
  780. // now break the components
  781. int i = 0;
  782. while (i < Objects.Count)
  783. {
  784. ReportComponentBase obj = Objects[i];
  785. if (obj.Bottom > breakLine)
  786. {
  787. if (obj.Top < breakLine)
  788. {
  789. BreakableComponent breakComp = Activator.CreateInstance(obj.GetType()) as BreakableComponent;
  790. breakComp.AssignAll(obj);
  791. breakComp.Parent = breakTo;
  792. breakComp.CanGrow = true;
  793. breakComp.CanShrink = false;
  794. breakComp.Height -= breakLine - obj.Top;
  795. breakComp.Top = 0;
  796. obj.Height = breakLine - obj.Top;
  797. (obj as BreakableComponent).Break(breakComp);
  798. }
  799. else
  800. {
  801. float currentExcessiveHeight = 0;
  802. foreach(var item in excessiveHeights)
  803. {
  804. if ((Math.Round(obj.Left, 2) <= Math.Round(item.Key.Left, 2) && Math.Round(obj.Right, 2) >= Math.Round(item.Key.Left, 2)) ||
  805. (Math.Round(obj.Left, 2) >= Math.Round(item.Key.Left, 2) && Math.Round(item.Key.Right, 2) >= Math.Round(obj.Left, 2)))
  806. currentExcessiveHeight = Math.Max(currentExcessiveHeight, item.Value);
  807. }
  808. // (case: object with Anchor = bottom on a breakable band)
  809. // in case of bottom anchor, do not move the object. It will be moved automatically when we decrease the band height
  810. if ((obj.Anchor & AnchorStyles.Bottom) == 0)
  811. obj.Top -= (breakLine - currentExcessiveHeight);
  812. obj.Parent = breakTo;
  813. continue;
  814. }
  815. }
  816. i++;
  817. }
  818. Height = breakLine;
  819. breakTo.Height -= (breakLine - maxExcessiveHeight);
  820. return Objects.Count > 0;
  821. }
  822. /// <inheritdoc/>
  823. public override void GetData()
  824. {
  825. base.GetData();
  826. FRCollectionBase list = new FRCollectionBase();
  827. Objects.CopyTo(list);
  828. foreach (ReportComponentBase obj in list)
  829. {
  830. obj.GetData();
  831. obj.OnAfterData();
  832. // break the component if it is of BreakableComponent an has non-empty BreakTo property
  833. if (obj is BreakableComponent && (obj as BreakableComponent).BreakTo != null &&
  834. (obj as BreakableComponent).BreakTo.GetType() == obj.GetType())
  835. (obj as BreakableComponent).Break((obj as BreakableComponent).BreakTo);
  836. }
  837. OnAfterData();
  838. }
  839. internal virtual bool IsEmpty()
  840. {
  841. return true;
  842. }
  843. private void AddBookmark(ReportComponentBase obj)
  844. {
  845. if (Report != null)
  846. Report.Engine.AddBookmark(obj.Bookmark);
  847. }
  848. internal void AddBookmarks()
  849. {
  850. AddBookmark(this);
  851. foreach (ReportComponentBase obj in Objects)
  852. {
  853. AddBookmark(obj);
  854. }
  855. }
  856. /// <inheritdoc/>
  857. public override void InitializeComponent()
  858. {
  859. base.InitializeComponent();
  860. AbsRowNo = 0;
  861. }
  862. /// <summary>
  863. /// This method fires the <b>BeforeLayout</b> event and the script code connected to the <b>BeforeLayoutEvent</b>.
  864. /// </summary>
  865. /// <param name="e">Event data.</param>
  866. public void OnBeforeLayout(EventArgs e)
  867. {
  868. if (BeforeLayout != null)
  869. BeforeLayout(this, e);
  870. InvokeEvent(BeforeLayoutEvent, e);
  871. }
  872. /// <summary>
  873. /// This method fires the <b>AfterLayout</b> event and the script code connected to the <b>AfterLayoutEvent</b>.
  874. /// </summary>
  875. /// <param name="e">Event data.</param>
  876. public void OnAfterLayout(EventArgs e)
  877. {
  878. if (AfterLayout != null)
  879. AfterLayout(this, e);
  880. InvokeEvent(AfterLayoutEvent, e);
  881. }
  882. #endregion
  883. /// <summary>
  884. /// Initializes a new instance of the <see cref="BandBase"/> class with default settings.
  885. /// </summary>
  886. public BandBase()
  887. {
  888. objects = new ReportComponentCollection(this);
  889. guides = new FloatCollection();
  890. beforeLayoutEvent = "";
  891. afterLayoutEvent = "";
  892. outlineExpression = "";
  893. CanBreak = false;
  894. ShiftMode = ShiftMode.Never;
  895. if (BaseName.EndsWith("Band"))
  896. BaseName = ClassName.Remove(ClassName.IndexOf("Band"));
  897. SetFlags(Flags.CanMove | Flags.CanChangeOrder | Flags.CanChangeParent | Flags.CanCopy | Flags.CanGroup, false);
  898. FlagUseStartNewPage = true;
  899. FlagCheckFreeSpace = true;
  900. }
  901. }
  902. }