MatrixButtons.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. using System;
  2. using System.ComponentModel;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Collections.Generic;
  6. using FastReport.Utils;
  7. using FastReport.Table;
  8. using System.Windows.Forms;
  9. namespace FastReport.AdvMatrix
  10. {
  11. /// <summary>
  12. /// Represents a base class for matrix buttons such as expand or sort button.
  13. /// </summary>
  14. public class MatrixButton : ReportComponentBase
  15. {
  16. /// <summary>
  17. /// Determines the symbol size, in pixels. 0 indicates the auto size.
  18. /// </summary>
  19. [Category("Appearance")]
  20. public int SymbolSize { get; set; }
  21. /// <summary>
  22. /// Determines whether this buttons belongs to column header. For internal use only.
  23. /// </summary>
  24. [Browsable(false)]
  25. public bool IsColumn { get; set; }
  26. /// <summary>
  27. /// Gets or set the index of this button. For internal use only.
  28. /// </summary>
  29. [Browsable(false)]
  30. public int Index { get; set; }
  31. internal AdvMatrixObject Matrix
  32. {
  33. get
  34. {
  35. Base obj = this;
  36. while (obj != null && !(obj is AdvMatrixObject))
  37. obj = obj.Parent;
  38. if (obj is AdvMatrixObject)
  39. return Report.FindObject(obj.Name) as AdvMatrixObject;
  40. return null;
  41. }
  42. }
  43. internal TableCell Cell
  44. {
  45. get
  46. {
  47. Base obj = this;
  48. while (obj != null && !(obj is TableCell))
  49. obj = obj.Parent;
  50. if (obj is TableCell)
  51. return obj as TableCell;
  52. return null;
  53. }
  54. }
  55. /// <inheritdoc/>
  56. public override void Assign(Base source)
  57. {
  58. base.Assign(source);
  59. MatrixButton src = source as MatrixButton;
  60. if (src != null)
  61. {
  62. SymbolSize = src.SymbolSize;
  63. IsColumn = src.IsColumn;
  64. Index = src.Index;
  65. }
  66. }
  67. /// <inheritdoc/>
  68. public override void Serialize(FRWriter writer)
  69. {
  70. base.Serialize(writer);
  71. MatrixButton c = writer.DiffObject as MatrixButton;
  72. if (SymbolSize != c.SymbolSize)
  73. writer.WriteInt("SymbolSize", SymbolSize);
  74. if (IsColumn != c.IsColumn)
  75. writer.WriteBool("IsColumn", IsColumn);
  76. if (Index != c.Index)
  77. writer.WriteInt("Index", Index);
  78. }
  79. /// <summary>
  80. /// Initializes a new instance of the <see cref="MatrixButton"/> class.
  81. /// </summary>
  82. public MatrixButton()
  83. {
  84. Cursor = System.Windows.Forms.Cursors.Hand;
  85. Printable = false;
  86. Exportable = false;
  87. SymbolSize = 0;
  88. }
  89. }
  90. /// <summary>
  91. /// Represents the symbol used to display the matrix expand/collapse state.
  92. /// </summary>
  93. public enum CollapseSymbol
  94. {
  95. /// <summary>
  96. /// Plus/minus.
  97. /// </summary>
  98. PlusMinus,
  99. /// <summary>
  100. /// The pointer.
  101. /// </summary>
  102. Pointer,
  103. /// <summary>
  104. /// The arrow.
  105. /// </summary>
  106. Arrow
  107. }
  108. /// <summary>
  109. /// Represents the matrix button used to toggle expand/collapse state of matrix headers.
  110. /// </summary>
  111. public partial class MatrixCollapseButton : MatrixButton
  112. {
  113. /// <summary>
  114. /// Determines whether this button has a collapsed state. For internal use only.
  115. /// </summary>
  116. [Browsable(false)]
  117. public bool Collapsed { get; set; }
  118. /// <summary>
  119. /// Determines whether to show collapse/expand menu on right click.
  120. /// </summary>
  121. public bool ShowCollapseExpandMenu { get; set; }
  122. /// <summary>
  123. /// Determines the symbol used to display the button's state.
  124. /// </summary>
  125. [Category("Appearance")]
  126. public CollapseSymbol Symbol { get; set; }
  127. /// <summary>
  128. /// Determines if only one button in the group can be expanded.
  129. /// </summary>
  130. [Category("Behavior")]
  131. public bool Exclusive { get; set; }
  132. /// <inheritdoc/>
  133. public override void Assign(Base source)
  134. {
  135. base.Assign(source);
  136. MatrixCollapseButton src = source as MatrixCollapseButton;
  137. if (src != null)
  138. {
  139. Collapsed = src.Collapsed;
  140. Symbol = src.Symbol;
  141. Exclusive = src.Exclusive;
  142. ShowCollapseExpandMenu = src.ShowCollapseExpandMenu;
  143. }
  144. }
  145. /// <inheritdoc/>
  146. public override void Draw(FRPaintEventArgs e)
  147. {
  148. if (Parent == null || (Parent as ReportComponentBase).Width == 0 || (Parent as ReportComponentBase).Height == 0)
  149. return;
  150. IGraphics g = e.Graphics;
  151. IGraphicsState state = g.Save();
  152. g.SmoothingMode = SmoothingMode.AntiAlias;
  153. PointF[] points = null;
  154. Pen pen = e.Cache.GetPen(Border.Color, Border.Width, DashStyle.Solid);
  155. Brush brush = e.Cache.GetBrush(Border.Color);
  156. // trying to be pixel perfect
  157. float dx = SymbolSize > 0 ? SymbolSize : (int)Math.Min(Width, Height) - 6;
  158. if ((int)dx % 2 == 1)
  159. dx -= 1;
  160. float dy = dx;
  161. g.ScaleTransform(e.ScaleX, e.ScaleY);
  162. g.TranslateTransform((int)Math.Round(AbsLeft + Width / 2 - dx / 2), (int)Math.Round(AbsTop + Height / 2 - dy / 2));
  163. if (Fill is SolidFill)
  164. g.FillRectangle(e.Cache.GetBrush((Fill as SolidFill).Color), new RectangleF(-2, -2, dx + 4, dy + 4));
  165. if (Border.Lines != BorderLines.None)
  166. g.DrawRectangle(pen, -2, -2, dx + 4, dy + 4);
  167. if (Collapsed)
  168. {
  169. switch (Symbol)
  170. {
  171. case CollapseSymbol.PlusMinus:
  172. g.DrawLine(pen, 0, dy / 2, dx, dy / 2);
  173. g.DrawLine(pen, dx / 2, 0, dx / 2, dy);
  174. break;
  175. case CollapseSymbol.Arrow:
  176. points = new PointF[] { new PointF(dx / 2, 0), new PointF(dx, dy / 2), new PointF(dx / 2, dy) };
  177. g.FillAndDrawPolygon(pen, brush, points);
  178. break;
  179. case CollapseSymbol.Pointer:
  180. g.DrawLines(pen, new PointF[] { new PointF(dx / 2, 0), new PointF(dx, dy / 2), new PointF(dx / 2, dy) });
  181. break;
  182. }
  183. }
  184. else
  185. {
  186. switch (Symbol)
  187. {
  188. case CollapseSymbol.PlusMinus:
  189. g.DrawLine(pen, 0, dy / 2, dx, dy / 2);
  190. break;
  191. case CollapseSymbol.Arrow:
  192. points = new PointF[] { new PointF(0, dy / 2), new PointF(dx / 2, dy), new PointF(dx, dy / 2) };
  193. g.FillAndDrawPolygon(pen, brush, points);
  194. break;
  195. case CollapseSymbol.Pointer:
  196. g.DrawLines(pen, new PointF[] { new PointF(0, dy / 2), new PointF(dx / 2, dy), new PointF(dx, dy / 2) });
  197. break;
  198. }
  199. }
  200. g.Restore(state);
  201. }
  202. /// <inheritdoc/>
  203. public override void Serialize(FRWriter writer)
  204. {
  205. base.Serialize(writer);
  206. MatrixCollapseButton c = writer.DiffObject as MatrixCollapseButton;
  207. if (Collapsed != c.Collapsed)
  208. writer.WriteBool("Collapsed", Collapsed);
  209. if (Exclusive != c.Exclusive)
  210. writer.WriteBool("Exclusive", Exclusive);
  211. if (Symbol != c.Symbol)
  212. writer.WriteValue("Symbol", Symbol);
  213. if (ShowCollapseExpandMenu != c.ShowCollapseExpandMenu)
  214. writer.WriteBool("ShowCollapseExpandMenu", ShowCollapseExpandMenu);
  215. }
  216. /// <summary>
  217. /// For internal use only,return action click for Advanced Matrix collapse button
  218. /// </summary>
  219. public void MatrixCollapseButtonClick()
  220. {
  221. if (IsColumn)
  222. Matrix.ToggleColumnVisible(Index);
  223. else
  224. Matrix.ToggleRowVisible(Index);
  225. Report.Refresh();
  226. }
  227. internal List<HeaderDataList> GetLinkedItems(HeaderData item)
  228. {
  229. List<HeaderDataList> list = new List<HeaderDataList>();
  230. Action<HeaderData> func = (root) =>
  231. {
  232. if (root == null)
  233. return;
  234. foreach (HeaderDataList dl in root.AllItems)
  235. {
  236. if (dl.Descriptor.VisibleToggledBy == Name)
  237. list.Add(dl);
  238. }
  239. };
  240. func(item);
  241. if (list.Count == 0 && item.Value == null)
  242. func(item.Parent);
  243. return list;
  244. }
  245. internal int GetLinkedItemsCount(HeaderData item)
  246. {
  247. int result = 0;
  248. foreach (HeaderDataList dl in GetLinkedItems(item))
  249. result += dl.Count;
  250. return result;
  251. }
  252. /// <summary>
  253. /// Initializes a new instance of the <see cref="MatrixCollapseButton"/> class.
  254. /// </summary>
  255. public MatrixCollapseButton()
  256. {
  257. BaseName = "CollapseButton";
  258. Symbol = CollapseSymbol.PlusMinus;
  259. Border.Lines = BorderLines.All;
  260. }
  261. }
  262. /// <summary>
  263. /// Represents the symbol used to display the matrix sort order.
  264. /// </summary>
  265. public enum SortSymbol
  266. {
  267. /// <summary>
  268. /// The arrow.
  269. /// </summary>
  270. Arrow,
  271. /// <summary>
  272. /// The pointer.
  273. /// </summary>
  274. Pointer
  275. }
  276. /// <summary>
  277. /// Represents the matrix button used to toggle sort order of matrix headers.
  278. /// </summary>
  279. public partial class MatrixSortButton : MatrixButton
  280. {
  281. /// <summary>
  282. /// Determines the sort state of this button.
  283. /// </summary>
  284. [Category("Behavior")]
  285. public SortOrder Sort { get; set; }
  286. /// <summary>
  287. /// Determines whether "None" sort is allowed when you switch sort states.
  288. /// </summary>
  289. [Category("Behavior")]
  290. public bool AllowInactiveSort { get; set; }
  291. /// <summary>
  292. /// Determines the symbol used to display the state of this button.
  293. /// </summary>
  294. [Category("Appearance")]
  295. public SortSymbol Symbol { get; set; }
  296. /// <summary>
  297. /// Determines the color used to display button with inactive sort state (Sort = None).
  298. /// </summary>
  299. [Category("Appearance")]
  300. public Color InactiveSortColor { get; set; }
  301. /// <inheritdoc/>
  302. public override void Assign(Base source)
  303. {
  304. base.Assign(source);
  305. MatrixSortButton src = source as MatrixSortButton;
  306. if (src != null)
  307. {
  308. Sort = src.Sort;
  309. AllowInactiveSort = src.AllowInactiveSort;
  310. Symbol = src.Symbol;
  311. InactiveSortColor = src.InactiveSortColor;
  312. }
  313. }
  314. /// <summary>
  315. /// For internal use only,return action click for Advanced Matrix sort button
  316. /// </summary>
  317. public void MatrixSortButtonClick()
  318. {
  319. SortOrder sort = (Sort == SortOrder.None ? SortOrder.Ascending : (Sort == SortOrder.Ascending ? SortOrder.Descending : SortOrder.None));
  320. if (Index == 0)
  321. {
  322. // this button toggles sort order of descriptors with SortToggledBy = this
  323. if (!AllowInactiveSort && sort == SortOrder.None)
  324. sort = SortOrder.Ascending;
  325. (Report.FindObject(Name) as MatrixSortButton).Sort = sort;
  326. }
  327. else if (Matrix != null)
  328. {
  329. if (IsColumn)
  330. Matrix.SortRowsByColumn(Index, sort);
  331. else
  332. Matrix.SortColumnsByRow(Index, sort);
  333. }
  334. Report.Refresh();
  335. }
  336. /// <inheritdoc/>
  337. public override void Draw(FRPaintEventArgs e)
  338. {
  339. IGraphics g = e.Graphics;
  340. IGraphicsState state = g.Save();
  341. g.SmoothingMode = SmoothingMode.AntiAlias;
  342. PointF[] points = null;
  343. Pen pen = e.Cache.GetPen(Border.Color, Border.Width, DashStyle.Solid);
  344. Brush brush = e.Cache.GetBrush(pen.Color);
  345. // trying to be pixel perfect
  346. float dx = SymbolSize > 0 ? SymbolSize : (int)Math.Min(Width, Height) - 6;
  347. if ((int)dx % 2 == 1)
  348. dx -= 1;
  349. float dy = dx;
  350. g.ScaleTransform(e.ScaleX, e.ScaleY);
  351. g.TranslateTransform((int)Math.Round(AbsLeft + Width / 2 - dx / 2), (int)Math.Round(AbsTop + Height / 2 - dy / 2));
  352. if (Fill is SolidFill)
  353. g.FillRectangle(e.Cache.GetBrush((Fill as SolidFill).Color), new RectangleF(-2, -2, dx + 4, dy + 4));
  354. if (Border.Lines != BorderLines.None)
  355. g.DrawRectangle(pen, -2, -2, dx + 4, dy + 4);
  356. if (Sort == SortOrder.Ascending)
  357. {
  358. switch (Symbol)
  359. {
  360. case SortSymbol.Arrow:
  361. points = new PointF[] { new PointF(0, dy / 2), new PointF(dx / 2, 0), new PointF(dx, dy / 2) };
  362. g.FillAndDrawPolygon(pen, brush, points);
  363. break;
  364. case SortSymbol.Pointer:
  365. g.DrawLines(pen, new PointF[] { new PointF(0, dy / 2), new PointF(dx / 2, 0), new PointF(dx, dy / 2) });
  366. break;
  367. }
  368. }
  369. else if (Sort == SortOrder.Descending)
  370. {
  371. switch (Symbol)
  372. {
  373. case SortSymbol.Arrow:
  374. points = new PointF[] { new PointF(0, dy / 2), new PointF(dx / 2, dy), new PointF(dx, dy / 2) };
  375. g.FillAndDrawPolygon(pen, brush, points);
  376. break;
  377. case SortSymbol.Pointer:
  378. g.DrawLines(pen, new PointF[] { new PointF(0, dy / 2), new PointF(dx / 2, dy), new PointF(dx, dy / 2) });
  379. break;
  380. }
  381. }
  382. else if (Sort == SortOrder.None)
  383. {
  384. pen = e.Cache.GetPen(InactiveSortColor, pen.Width, DashStyle.Solid);
  385. brush = e.Cache.GetBrush(pen.Color);
  386. switch (Symbol)
  387. {
  388. case SortSymbol.Arrow:
  389. points = new PointF[] { new PointF(1, dy / 2 - 1), new PointF(dx / 2, 0), new PointF(dx - 1, dy / 2 - 1) };
  390. g.FillAndDrawPolygon(pen, brush, points);
  391. points = new PointF[] { new PointF(1, dy / 2 + 1), new PointF(dx / 2, dy), new PointF(dx - 1, dy / 2 + 1) };
  392. g.FillAndDrawPolygon(pen, brush, points);
  393. break;
  394. case SortSymbol.Pointer:
  395. g.DrawLines(pen, new PointF[] { new PointF(1, dy / 2 - 1), new PointF(dx / 2, 0), new PointF(dx - 1, dy / 2 - 1) });
  396. g.DrawLines(pen, new PointF[] { new PointF(1, dy / 2 + 1), new PointF(dx / 2, dy), new PointF(dx - 1, dy / 2 + 1) });
  397. break;
  398. }
  399. }
  400. g.Restore(state);
  401. }
  402. /// <inheritdoc/>
  403. public override void Serialize(FRWriter writer)
  404. {
  405. base.Serialize(writer);
  406. MatrixSortButton c = writer.DiffObject as MatrixSortButton;
  407. if (Sort != c.Sort)
  408. writer.WriteValue("Sort", Sort);
  409. if (AllowInactiveSort != c.AllowInactiveSort)
  410. writer.WriteBool("AllowInactiveSort", AllowInactiveSort);
  411. if (Symbol != c.Symbol)
  412. writer.WriteValue("Symbol", Symbol);
  413. if (InactiveSortColor != c.InactiveSortColor)
  414. writer.WriteValue("InactiveSortColor", InactiveSortColor);
  415. }
  416. /// <summary>
  417. /// Initializes a new instance of the <see cref="MatrixSortButton"/> class.
  418. /// </summary>
  419. public MatrixSortButton()
  420. {
  421. BaseName = "SortButton";
  422. Sort = SortOrder.None;
  423. AllowInactiveSort = true;
  424. Symbol = SortSymbol.Arrow;
  425. InactiveSortColor = Color.Gray;
  426. }
  427. }
  428. }