TextObject.DesignExt.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using System;
  2. using System.Windows.Forms;
  3. using System.Drawing;
  4. using FastReport.Utils;
  5. using FastReport.Forms;
  6. using FastReport.Data;
  7. using FastReport.Design.PageDesigners.Page;
  8. namespace FastReport
  9. {
  10. partial class TextObject : IHasEditor
  11. {
  12. #region Fields
  13. private TextBox textBox;
  14. internal bool dragAccept;
  15. #endregion
  16. #region Properties
  17. /// <inheritdoc/>
  18. public override float Left
  19. {
  20. get { return base.Left; }
  21. set
  22. {
  23. base.Left = value;
  24. if (IsEditing)
  25. UpdateEditorPosition();
  26. }
  27. }
  28. /// <inheritdoc/>
  29. public override float Top
  30. {
  31. get { return base.Top; }
  32. set
  33. {
  34. base.Top = value;
  35. if (IsEditing)
  36. UpdateEditorPosition();
  37. }
  38. }
  39. /// <inheritdoc/>
  40. public override float Width
  41. {
  42. get { return base.Width; }
  43. set
  44. {
  45. base.Width = value;
  46. if (IsEditing)
  47. UpdateEditorPosition();
  48. }
  49. }
  50. /// <inheritdoc/>
  51. public override float Height
  52. {
  53. get { return base.Height; }
  54. set
  55. {
  56. base.Height = value;
  57. if (IsEditing)
  58. UpdateEditorPosition();
  59. }
  60. }
  61. private bool IsEditing
  62. {
  63. get { return IsDesigning && textBox != null; }
  64. }
  65. #endregion
  66. #region Private Methods
  67. //private bool ShouldSerializeFont()
  68. //{
  69. // return Font.Name != "Arial" || Font.Size != 10 || Font.Style != FontStyle.Regular;
  70. //}
  71. private bool ShouldSerializeTextFill()
  72. {
  73. return !(TextFill is SolidFill) || (TextFill as SolidFill).Color != Color.Black;
  74. }
  75. private void UpdateEditorPosition()
  76. {
  77. textBox.Location = new Point((int)Math.Round(AbsLeft * Report.Designer.ZoomDpi) + 1,
  78. (int)Math.Round(AbsTop * Report.Designer.ZoomDpi) + 1);
  79. textBox.Size = new Size((int)Math.Round(Width * Report.Designer.ZoomDpi) - 1,
  80. (int)Math.Round(Height * Report.Designer.ZoomDpi) - 1);
  81. }
  82. private void FTextBox_KeyDown(object sender, KeyEventArgs e)
  83. {
  84. if (e.KeyCode == Keys.Escape)
  85. FinishEdit(false);
  86. if (e.Control && e.KeyCode == Keys.Enter)
  87. FinishEdit(true);
  88. }
  89. #endregion
  90. #region Public Methods
  91. /// <inheritdoc/>
  92. public override void AssignFormat(ReportComponentBase source)
  93. {
  94. base.AssignFormat(source);
  95. TextObject src = source as TextObject;
  96. if (src == null)
  97. return;
  98. HorzAlign = src.HorzAlign;
  99. VertAlign = src.VertAlign;
  100. Angle = src.Angle;
  101. Underlines = src.Underlines;
  102. Font = src.Font;
  103. TextFill = src.TextFill.Clone();
  104. TextOutline.Assign(src.TextOutline);
  105. Trimming = src.Trimming;
  106. FontWidthRatio = src.FontWidthRatio;
  107. FirstTabOffset = src.FirstTabOffset;
  108. TabWidth = src.TabWidth;
  109. Clip = src.Clip;
  110. Wysiwyg = src.Wysiwyg;
  111. LineHeight = src.LineHeight;
  112. TextRenderType = src.TextRenderType;
  113. ParagraphOffset = src.ParagraphOffset;
  114. }
  115. /// <inheritdoc/>
  116. public override void HandleDragOver(FRMouseEventArgs e)
  117. {
  118. if (PointInObject(new PointF(e.x, e.y)) && e.DragSource is TextObject)
  119. e.handled = true;
  120. dragAccept = e.handled;
  121. }
  122. /// <inheritdoc/>
  123. public override void HandleDragDrop(FRMouseEventArgs e)
  124. {
  125. Text = (e.DragSource as TextObject).Text;
  126. dragAccept = false;
  127. }
  128. /// <inheritdoc/>
  129. public override void HandleKeyDown(Control sender, KeyEventArgs e)
  130. {
  131. if (IsSelected && e.KeyCode == Keys.Enter && HasFlag(Flags.CanEdit) && !HasRestriction(Restrictions.DontEdit))
  132. {
  133. textBox = new TextBox();
  134. textBox.Font = new Font(Font.Name, Font.Size * Report.Designer.ZoomDpi * DrawUtils.ScreenDpiFX, Font.Style);
  135. textBox.BorderStyle = BorderStyle.None;
  136. textBox.Multiline = true;
  137. textBox.AcceptsTab = true;
  138. if (Fill is SolidFill)
  139. textBox.BackColor = Color.FromArgb(255, (Fill as SolidFill).Color);
  140. if (TextFill is SolidFill)
  141. textBox.ForeColor = Color.FromArgb(255, (TextFill as SolidFill).Color);
  142. textBox.Text = Text;
  143. textBox.KeyDown += new KeyEventHandler(FTextBox_KeyDown);
  144. UpdateEditorPosition();
  145. sender.Controls.Add(textBox);
  146. textBox.SelectAll();
  147. textBox.Focus();
  148. e.Handled = true;
  149. }
  150. }
  151. /// <inheritdoc/>
  152. public override void OnMouseDown(MouseEventArgs e)
  153. {
  154. if (Editable && !Config.WebMode && InvokeEditor())
  155. {
  156. Report report = Report;
  157. if (report != null)
  158. {
  159. Preview.PreviewControl preview = report.Preview;
  160. if (preview != null)
  161. {
  162. // update current page in a cache
  163. report.PreparedPages.ModifyPage(Report.Preview.PageNo - 1, Page as ReportPage);
  164. // redraw the preview
  165. preview.Refresh();
  166. }
  167. }
  168. }
  169. else
  170. base.OnMouseDown(e);
  171. }
  172. /// <inheritdoc/>
  173. public override void SelectionChanged()
  174. {
  175. FinishEdit(true);
  176. }
  177. /// <inheritdoc/>
  178. public override ContextMenuBase GetContextMenu()
  179. {
  180. return new TextObjectMenu(Report.Designer);
  181. }
  182. /// <inheritdoc/>
  183. public override SmartTagBase GetSmartTag()
  184. {
  185. return new TextObjectSmartTag(this);
  186. }
  187. /// <inheritdoc/>
  188. public virtual bool InvokeEditor()
  189. {
  190. using (TextEditorForm form = new TextEditorForm(Report))
  191. {
  192. form.ExpressionText = Text;
  193. form.Brackets = Brackets;
  194. if (form.ShowDialog() == DialogResult.OK)
  195. {
  196. Text = form.ExpressionText;
  197. return true;
  198. }
  199. }
  200. return false;
  201. }
  202. internal virtual void FinishEdit(bool accept)
  203. {
  204. if (textBox == null)
  205. return;
  206. if (textBox.Modified && accept)
  207. {
  208. Text = textBox.Text;
  209. textBox.Dispose();
  210. textBox = null;
  211. if (Report != null)
  212. Report.Designer.SetModified(null, "TextObject_SelectChanged", Name);
  213. return;
  214. }
  215. textBox.Dispose();
  216. textBox = null;
  217. }
  218. /// <inheritdoc/>
  219. public override string GetDisplayText()
  220. {
  221. if (IsDesigning && ReportWorkspace.SimplifyDBFields)
  222. {
  223. if (Text.StartsWith("[") && Text.EndsWith("]"))
  224. {
  225. string complexName = Text.Substring(1, Text.Length - 2);
  226. Column column = DataHelper.GetColumn(Report.Dictionary, complexName);
  227. if (column != null)
  228. return "[" + column.Alias + "]";
  229. }
  230. }
  231. return Text;
  232. }
  233. #endregion
  234. private void DrawDesign(FRPaintEventArgs e)
  235. {
  236. if (dragAccept)
  237. DrawDragAcceptFrame(e, Color.Silver);
  238. }
  239. }
  240. }