CheckBoxObject.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.ComponentModel;
  6. using FastReport.Utils;
  7. using System.Drawing.Design;
  8. namespace FastReport
  9. {
  10. /// <summary>
  11. /// Specifies a symbol that will be displayed when a <see cref="CheckBoxObject"/> is in the checked state.
  12. /// </summary>
  13. public enum CheckedSymbol
  14. {
  15. /// <summary>
  16. /// Specifies a check symbol.
  17. /// </summary>
  18. Check,
  19. /// <summary>
  20. /// Specifies a diagonal cross symbol.
  21. /// </summary>
  22. Cross,
  23. /// <summary>
  24. /// Specifies a plus symbol.
  25. /// </summary>
  26. Plus,
  27. /// <summary>
  28. /// Specifies a filled rectangle.
  29. /// </summary>
  30. Fill
  31. }
  32. /// <summary>
  33. /// Specifies a symbol that will be displayed when a <see cref="CheckBoxObject"/> is in the unchecked state.
  34. /// </summary>
  35. public enum UncheckedSymbol
  36. {
  37. /// <summary>
  38. /// Specifies no symbol.
  39. /// </summary>
  40. None,
  41. /// <summary>
  42. /// Specifies a diagonal cross symbol.
  43. /// </summary>
  44. Cross,
  45. /// <summary>
  46. /// Specifies a minus symbol.
  47. /// </summary>
  48. Minus,
  49. /// <summary>
  50. /// Specifies a slash symbol.
  51. /// </summary>
  52. Slash,
  53. /// <summary>
  54. /// Specifies a back slash symbol.
  55. /// </summary>
  56. BackSlash
  57. }
  58. /// <summary>
  59. /// Represents a check box object.
  60. /// </summary>
  61. public partial class CheckBoxObject : ReportComponentBase
  62. {
  63. #region Fields
  64. private bool isChecked;
  65. private CheckedSymbol checkedSymbol;
  66. private UncheckedSymbol uncheckedSymbol;
  67. private Color checkColor;
  68. private string dataColumn;
  69. private string expression;
  70. private float checkWidthRatio;
  71. private bool hideIfUnchecked;
  72. private bool editable;
  73. #endregion
  74. #region Properties
  75. /// <summary>
  76. /// Gets or set a value indicating whether the check box is in the checked state.
  77. /// </summary>
  78. [DefaultValue(true)]
  79. [Category("Data")]
  80. public bool Checked
  81. {
  82. get { return isChecked; }
  83. set { isChecked = value; }
  84. }
  85. /// <summary>
  86. /// Gets or sets a symbol that will be displayed when the check box is in the checked state.
  87. /// </summary>
  88. [DefaultValue(CheckedSymbol.Check)]
  89. [Category("Appearance")]
  90. public CheckedSymbol CheckedSymbol
  91. {
  92. get { return checkedSymbol; }
  93. set { checkedSymbol = value; }
  94. }
  95. /// <summary>
  96. /// Gets or sets a symbol that will be displayed when the check box is in the unchecked state.
  97. /// </summary>
  98. [DefaultValue(UncheckedSymbol.None)]
  99. [Category("Appearance")]
  100. public UncheckedSymbol UncheckedSymbol
  101. {
  102. get { return uncheckedSymbol; }
  103. set { uncheckedSymbol = value; }
  104. }
  105. /// <summary>
  106. /// Gets or sets a color of the check symbol.
  107. /// </summary>
  108. [Category("Appearance")]
  109. [Editor("FastReport.TypeEditors.ColorEditor, FastReport", typeof(UITypeEditor))]
  110. public Color CheckColor
  111. {
  112. get { return checkColor; }
  113. set { checkColor = value; }
  114. }
  115. /// <summary>
  116. /// Gets or sets a data column name bound to this control.
  117. /// </summary>
  118. /// <remarks>
  119. /// Value must be in the form "[Datasource.Column]".
  120. /// </remarks>
  121. [Category("Data")]
  122. [Editor("FastReport.TypeEditors.DataColumnEditor, FastReport", typeof(UITypeEditor))]
  123. public string DataColumn
  124. {
  125. get { return dataColumn; }
  126. set { dataColumn = value; }
  127. }
  128. /// <summary>
  129. /// Gets or sets an expression that determines whether to show a check.
  130. /// </summary>
  131. [Category("Data")]
  132. [Editor("FastReport.TypeEditors.ExpressionEditor, FastReport", typeof(UITypeEditor))]
  133. public string Expression
  134. {
  135. get { return expression; }
  136. set { expression = value; }
  137. }
  138. /// <summary>
  139. /// Gets or sets the check symbol width ratio.
  140. /// </summary>
  141. /// <remarks>
  142. /// Valid values are from 0.2 to 2.
  143. /// </remarks>
  144. [DefaultValue(1f)]
  145. [Category("Appearance")]
  146. public float CheckWidthRatio
  147. {
  148. get { return checkWidthRatio; }
  149. set
  150. {
  151. if (value <= 0.2f)
  152. value = 0.2f;
  153. if (value > 2)
  154. value = 2;
  155. checkWidthRatio = value;
  156. }
  157. }
  158. /// <summary>
  159. /// Gets or sets a value determines whether to hide the checkbox if it is in the unchecked state.
  160. /// </summary>
  161. [DefaultValue(false)]
  162. [Category("Behavior")]
  163. public bool HideIfUnchecked
  164. {
  165. get { return hideIfUnchecked; }
  166. set { hideIfUnchecked = value; }
  167. }
  168. /// <summary>
  169. /// Gets or sets editable for pdf export
  170. /// </summary>
  171. [Category("Behavior")]
  172. [DefaultValue(false)]
  173. public bool Editable
  174. {
  175. get { return editable; }
  176. set { editable = value; }
  177. }
  178. #endregion
  179. #region Private Methods
  180. private bool ShouldSerializeCheckColor()
  181. {
  182. return CheckColor != Color.Black;
  183. }
  184. private void DrawCheck(FRPaintEventArgs e)
  185. {
  186. RectangleF drawRect = new RectangleF(AbsLeft * e.ScaleX, AbsTop * e.ScaleY,
  187. Width * e.ScaleX, Height * e.ScaleY);
  188. float ratio = Width / (Units.Millimeters * 5);
  189. drawRect.Inflate(-4 * ratio * e.ScaleX, -4 * ratio * e.ScaleY);
  190. Pen pen = e.Cache.GetPen(CheckColor, 1.6f * ratio * CheckWidthRatio * e.ScaleX, DashStyle.Solid);
  191. IGraphics g = e.Graphics;
  192. SmoothingMode saveSmoothing = g.SmoothingMode;
  193. g.SmoothingMode = SmoothingMode.AntiAlias;
  194. if (Checked)
  195. {
  196. switch (CheckedSymbol)
  197. {
  198. case CheckedSymbol.Check:
  199. g.DrawLines(pen, new PointF[] {
  200. new PointF(drawRect.Left, drawRect.Top + drawRect.Height / 10 * 5),
  201. new PointF(drawRect.Left + drawRect.Width / 10 * 4, drawRect.Bottom - drawRect.Height / 10),
  202. new PointF(drawRect.Right, drawRect.Top + drawRect.Height / 10) });
  203. break;
  204. case CheckedSymbol.Cross:
  205. g.DrawLine(pen, drawRect.Left, drawRect.Top, drawRect.Right, drawRect.Bottom);
  206. g.DrawLine(pen, drawRect.Left, drawRect.Bottom, drawRect.Right, drawRect.Top);
  207. break;
  208. case CheckedSymbol.Plus:
  209. g.DrawLine(pen, drawRect.Left, drawRect.Top + drawRect.Height / 2, drawRect.Right, drawRect.Top + drawRect.Height / 2);
  210. g.DrawLine(pen, drawRect.Left + drawRect.Width / 2, drawRect.Top, drawRect.Left + drawRect.Width / 2, drawRect.Bottom);
  211. break;
  212. case CheckedSymbol.Fill:
  213. Brush brush = e.Cache.GetBrush(CheckColor);
  214. g.FillRectangle(brush, drawRect);
  215. break;
  216. }
  217. }
  218. else
  219. {
  220. switch (UncheckedSymbol)
  221. {
  222. case UncheckedSymbol.Cross:
  223. g.DrawLine(pen, drawRect.Left, drawRect.Top, drawRect.Right, drawRect.Bottom);
  224. g.DrawLine(pen, drawRect.Left, drawRect.Bottom, drawRect.Right, drawRect.Top);
  225. break;
  226. case UncheckedSymbol.Minus:
  227. g.DrawLine(pen, drawRect.Left, drawRect.Top + drawRect.Height / 2, drawRect.Right, drawRect.Top + drawRect.Height / 2);
  228. break;
  229. case UncheckedSymbol.Slash:
  230. g.DrawLine(pen, drawRect.Left, drawRect.Bottom, drawRect.Right, drawRect.Top);
  231. break;
  232. case UncheckedSymbol.BackSlash:
  233. g.DrawLine(pen, drawRect.Left, drawRect.Top, drawRect.Right, drawRect.Bottom);
  234. break;
  235. }
  236. }
  237. g.SmoothingMode = saveSmoothing;
  238. }
  239. #endregion
  240. #region Public Methods
  241. /// <inheritdoc/>
  242. public override void Assign(Base source)
  243. {
  244. base.Assign(source);
  245. CheckBoxObject src = source as CheckBoxObject;
  246. Checked = src.Checked;
  247. CheckedSymbol = src.CheckedSymbol;
  248. UncheckedSymbol = src.UncheckedSymbol;
  249. CheckColor = src.CheckColor;
  250. DataColumn = src.DataColumn;
  251. Expression = src.Expression;
  252. CheckWidthRatio = src.CheckWidthRatio;
  253. HideIfUnchecked = src.HideIfUnchecked;
  254. Editable = src.Editable;
  255. }
  256. /// <inheritdoc/>
  257. public override void Draw(FRPaintEventArgs e)
  258. {
  259. base.Draw(e);
  260. DrawCheck(e);
  261. DrawMarkers(e);
  262. Border.Draw(e, new RectangleF(AbsLeft, AbsTop, Width, Height));
  263. }
  264. /// <inheritdoc/>
  265. public override void Serialize(FRWriter writer)
  266. {
  267. CheckBoxObject c = writer.DiffObject as CheckBoxObject;
  268. base.Serialize(writer);
  269. if (Checked != c.Checked)
  270. writer.WriteBool("Checked", Checked);
  271. if (CheckedSymbol != c.CheckedSymbol)
  272. writer.WriteValue("CheckedSymbol", CheckedSymbol);
  273. if (UncheckedSymbol != c.UncheckedSymbol)
  274. writer.WriteValue("UncheckedSymbol", UncheckedSymbol);
  275. if (CheckColor != c.CheckColor)
  276. writer.WriteValue("CheckColor", CheckColor);
  277. if (DataColumn != c.DataColumn)
  278. writer.WriteStr("DataColumn", DataColumn);
  279. if (Expression != c.Expression)
  280. writer.WriteStr("Expression", Expression);
  281. if (CheckWidthRatio != c.CheckWidthRatio)
  282. writer.WriteFloat("CheckWidthRatio", CheckWidthRatio);
  283. if (HideIfUnchecked != c.HideIfUnchecked)
  284. writer.WriteBool("HideIfUnchecked", HideIfUnchecked);
  285. if (Editable)
  286. writer.WriteBool("Editable", Editable);
  287. }
  288. #endregion
  289. #region Report Engine
  290. /// <inheritdoc/>
  291. public override string[] GetExpressions()
  292. {
  293. List<string> expressions = new List<string>();
  294. expressions.AddRange(base.GetExpressions());
  295. if (!String.IsNullOrEmpty(DataColumn))
  296. expressions.Add(DataColumn);
  297. if (!String.IsNullOrEmpty(Expression))
  298. expressions.Add(Expression);
  299. return expressions.ToArray();
  300. }
  301. /// <inheritdoc/>
  302. public override void GetData()
  303. {
  304. base.GetData();
  305. if (!String.IsNullOrEmpty(DataColumn))
  306. {
  307. object value = Report.GetColumnValue(DataColumn);
  308. Variant varValue = value == null ? new Variant(0) : new Variant(value);
  309. Checked = varValue == true || (varValue.IsNumeric && varValue != 0);
  310. }
  311. else if (!String.IsNullOrEmpty(Expression))
  312. {
  313. object value = Report.Calc(Expression);
  314. Checked = value is bool && (bool)value == true;
  315. }
  316. if (!Checked && HideIfUnchecked)
  317. Visible = false;
  318. }
  319. #endregion
  320. /// <summary>
  321. /// Initializes a new instance of the <b>CheckBoxObject</b> class with default settings.
  322. /// </summary>
  323. public CheckBoxObject()
  324. {
  325. checkColor = Color.Black;
  326. dataColumn = "";
  327. expression = "";
  328. isChecked = true;
  329. checkedSymbol = CheckedSymbol.Check;
  330. uncheckedSymbol = UncheckedSymbol.None;
  331. checkWidthRatio = 1;
  332. SetFlags(Flags.HasSmartTag, true);
  333. }
  334. }
  335. }