TextObject.DesignExt.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. using FastReport.Data;
  2. using FastReport.Design.PageDesigners.Page;
  3. using FastReport.Forms;
  4. using FastReport.Utils;
  5. using System;
  6. using System.Drawing;
  7. using System.Windows.Forms;
  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. var offset = (Report.Designer.ActiveReportTab.ActivePageDesigner as ReportPageDesigner).Workspace.Offset;
  78. textBox.Location = new Point((int)Math.Round(AbsLeft * Report.Designer.ZoomDpi) + offset.X + 1,
  79. (int)Math.Round(AbsTop * Report.Designer.ZoomDpi) + offset.Y + 1);
  80. textBox.Size = new Size((int)Math.Round(Width * Report.Designer.ZoomDpi) - 1,
  81. (int)Math.Round(Height * Report.Designer.ZoomDpi) - 1);
  82. }
  83. private void FTextBox_KeyDown(object sender, KeyEventArgs e)
  84. {
  85. if (e.KeyCode == Keys.Escape)
  86. {
  87. FinishEdit(false);
  88. e.Handled = true;
  89. }
  90. if (e.Control && e.KeyCode == Keys.Enter)
  91. {
  92. FinishEdit(true);
  93. e.Handled = true;
  94. }
  95. }
  96. #endregion
  97. #region Public Methods
  98. /// <inheritdoc/>
  99. public override void AssignFormat(ReportComponentBase source)
  100. {
  101. base.AssignFormat(source);
  102. TextObject src = source as TextObject;
  103. if (src == null)
  104. return;
  105. HorzAlign = src.HorzAlign;
  106. VertAlign = src.VertAlign;
  107. Angle = src.Angle;
  108. Underlines = src.Underlines;
  109. Font = src.Font;
  110. TextFill = src.TextFill.Clone();
  111. TextOutline.Assign(src.TextOutline);
  112. Trimming = src.Trimming;
  113. FontWidthRatio = src.FontWidthRatio;
  114. FirstTabOffset = src.FirstTabOffset;
  115. TabWidth = src.TabWidth;
  116. Clip = src.Clip;
  117. Wysiwyg = src.Wysiwyg;
  118. LineHeight = src.LineHeight;
  119. TextRenderType = src.TextRenderType;
  120. ParagraphOffset = src.ParagraphOffset;
  121. }
  122. /// <inheritdoc/>
  123. public override void HandleDragOver(FRMouseEventArgs e)
  124. {
  125. if (PointInObject(new PointF(e.x, e.y)) && e.DragSource is TextObject)
  126. e.handled = true;
  127. dragAccept = e.handled;
  128. }
  129. /// <inheritdoc/>
  130. public override void HandleDragDrop(FRMouseEventArgs e)
  131. {
  132. Text = (e.DragSource as TextObject).Text;
  133. dragAccept = false;
  134. }
  135. /// <inheritdoc/>
  136. public override void HandleKeyDown(Control sender, KeyEventArgs e)
  137. {
  138. if (IsSelected && e.KeyCode == Keys.Enter && HasFlag(Flags.CanEdit) && !HasRestriction(Restrictions.DontEdit))
  139. {
  140. textBox = new TextBox();
  141. #if (WPF || AVALONIA)
  142. textBox.Font = new Font(Font.Name, Font.Size * Report.Designer.Zoom, Font.Style);
  143. #else
  144. textBox.Font = new Font(Font.Name, Font.Size * Report.Designer.ZoomDpi * DrawUtils.ScreenDpiFX, Font.Style);
  145. #endif
  146. textBox.BorderStyle = BorderStyle.None;
  147. textBox.Multiline = true;
  148. textBox.AcceptsTab = true;
  149. if (Fill is SolidFill)
  150. textBox.BackColor = Color.FromArgb(255, (Fill as SolidFill).Color);
  151. if (TextFill is SolidFill)
  152. textBox.ForeColor = Color.FromArgb(255, (TextFill as SolidFill).Color);
  153. textBox.Text = Text;
  154. textBox.KeyDown += new KeyEventHandler(FTextBox_KeyDown);
  155. sender.Controls.Add(textBox);
  156. UpdateEditorPosition();
  157. textBox.SelectAll();
  158. textBox.Focus();
  159. e.Handled = true;
  160. }
  161. }
  162. /// <inheritdoc/>
  163. public override void OnMouseDown(MouseEventArgs e)
  164. {
  165. if (Editable && !Config.WebMode && InvokeEditor())
  166. {
  167. if (Page != null)
  168. {
  169. Page.NeedModify = true;
  170. Page.NeedRefresh = true;
  171. }
  172. }
  173. else
  174. base.OnMouseDown(e);
  175. }
  176. /// <inheritdoc/>
  177. public override void SelectionChanged()
  178. {
  179. FinishEdit(true);
  180. }
  181. /// <inheritdoc/>
  182. public override ContextMenuBase GetContextMenu()
  183. {
  184. return new TextObjectMenu(Report.Designer);
  185. }
  186. /// <inheritdoc/>
  187. public override SmartTagBase GetSmartTag()
  188. {
  189. return new TextObjectSmartTag(this);
  190. }
  191. /// <inheritdoc/>
  192. public virtual bool InvokeEditor()
  193. {
  194. using (TextEditorForm form = new TextEditorForm(Report))
  195. {
  196. form.ExpressionText = Text;
  197. form.Brackets = Brackets;
  198. form.HideDataBar = Report.IsPreviewing;
  199. if (form.ShowDialog() == DialogResult.OK)
  200. {
  201. Text = form.ExpressionText;
  202. return true;
  203. }
  204. }
  205. return false;
  206. }
  207. internal virtual void FinishEdit(bool accept)
  208. {
  209. if (textBox == null)
  210. return;
  211. if (textBox.Modified && accept)
  212. {
  213. Text = textBox.Text;
  214. if (Report != null)
  215. Report.Designer.SetModified(null, "Change", Name);
  216. }
  217. textBox.Parent.Focus();
  218. textBox.Dispose();
  219. textBox = null;
  220. }
  221. /// <inheritdoc/>
  222. public override string GetDisplayText()
  223. {
  224. if (IsDesigning && ReportWorkspace.SimplifyDBFields)
  225. {
  226. if (Text.StartsWith("[") && Text.EndsWith("]"))
  227. {
  228. string complexName = Text.Substring(1, Text.Length - 2);
  229. Column column = DataHelper.GetColumn(Report.Dictionary, complexName);
  230. if (column != null)
  231. return "[" + column.Alias + "]";
  232. }
  233. }
  234. return Text;
  235. }
  236. #endregion
  237. private void DrawDesign(FRPaintEventArgs e)
  238. {
  239. if (dragAccept)
  240. DrawDragAcceptFrame(e, Color.Silver);
  241. }
  242. }
  243. }