HtmlObject.DesignExt.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System;
  2. using System.Windows.Forms;
  3. using System.Drawing;
  4. using FastReport.Utils;
  5. using FastReport.Forms;
  6. namespace FastReport
  7. {
  8. partial class HtmlObject : IHasEditor
  9. {
  10. #region Fields
  11. private TextBox textBox;
  12. internal bool dragAccept;
  13. #endregion
  14. #region Properties
  15. /// <inheritdoc/>
  16. public override float Left
  17. {
  18. get { return base.Left; }
  19. set
  20. {
  21. base.Left = value;
  22. if (IsEditing)
  23. UpdateEditorPosition();
  24. }
  25. }
  26. /// <inheritdoc/>
  27. public override float Top
  28. {
  29. get { return base.Top; }
  30. set
  31. {
  32. base.Top = value;
  33. if (IsEditing)
  34. UpdateEditorPosition();
  35. }
  36. }
  37. /// <inheritdoc/>
  38. public override float Width
  39. {
  40. get { return base.Width; }
  41. set
  42. {
  43. base.Width = value;
  44. if (IsEditing)
  45. UpdateEditorPosition();
  46. }
  47. }
  48. /// <inheritdoc/>
  49. public override float Height
  50. {
  51. get { return base.Height; }
  52. set
  53. {
  54. base.Height = value;
  55. if (IsEditing)
  56. UpdateEditorPosition();
  57. }
  58. }
  59. private bool IsEditing
  60. {
  61. get { return IsDesigning && textBox != null; }
  62. }
  63. #endregion
  64. #region Private Methods
  65. private void UpdateEditorPosition()
  66. {
  67. textBox.Location = new Point((int)Math.Round(AbsLeft * Report.Designer.ZoomDpi) + 1,
  68. (int)Math.Round(AbsTop * Report.Designer.ZoomDpi) + 1);
  69. textBox.Size = new Size((int)Math.Round(Width * Report.Designer.ZoomDpi) - 1,
  70. (int)Math.Round(Height * Report.Designer.ZoomDpi) - 1);
  71. }
  72. private void FTextBox_KeyDown(object sender, KeyEventArgs e)
  73. {
  74. if (e.KeyCode == Keys.Escape)
  75. FinishEdit(false);
  76. if (e.Control && e.KeyCode == Keys.Enter)
  77. FinishEdit(true);
  78. }
  79. private void DrawDesign(FRPaintEventArgs e)
  80. {
  81. if (dragAccept)
  82. DrawDragAcceptFrame(e, Color.Silver);
  83. }
  84. #endregion
  85. #region Public Methods
  86. /// <inheritdoc/>
  87. public override void HandleDragOver(FRMouseEventArgs e)
  88. {
  89. if (PointInObject(new PointF(e.x, e.y)) && e.DragSource is TextObject)
  90. e.handled = true;
  91. dragAccept = e.handled;
  92. }
  93. /// <inheritdoc/>
  94. public override void HandleDragDrop(FRMouseEventArgs e)
  95. {
  96. Text = (e.DragSource as TextObject).Text;
  97. dragAccept = false;
  98. }
  99. /// <inheritdoc/>
  100. public override void HandleKeyDown(Control sender, KeyEventArgs e)
  101. {
  102. if (IsSelected && e.KeyCode == Keys.Enter && HasFlag(Flags.CanEdit) && !HasRestriction(Restrictions.DontEdit))
  103. {
  104. textBox = new TextBox();
  105. textBox.Font = DrawUtils.DefaultTextObjectFont;
  106. textBox.BorderStyle = BorderStyle.None;
  107. textBox.Multiline = true;
  108. textBox.AcceptsTab = true;
  109. if (Fill is SolidFill)
  110. textBox.BackColor = Color.FromArgb(255, (Fill as SolidFill).Color);
  111. textBox.ForeColor = Color.FromArgb(255, Color.Black);
  112. textBox.Text = Text;
  113. textBox.KeyDown += new KeyEventHandler(FTextBox_KeyDown);
  114. UpdateEditorPosition();
  115. sender.Controls.Add(textBox);
  116. textBox.SelectAll();
  117. textBox.Focus();
  118. e.Handled = true;
  119. }
  120. }
  121. /// <inheritdoc/>
  122. public override void SelectionChanged()
  123. {
  124. FinishEdit(true);
  125. }
  126. /// <inheritdoc/>
  127. public override ContextMenuBase GetContextMenu()
  128. {
  129. return new HtmlObjectMenu(Report.Designer);
  130. }
  131. /// <inheritdoc/>
  132. public override SmartTagBase GetSmartTag()
  133. {
  134. return new HtmlObjectSmartTag(this);
  135. }
  136. /// <inheritdoc/>
  137. public virtual bool InvokeEditor()
  138. {
  139. using (TextEditorForm form = new TextEditorForm(Report))
  140. {
  141. form.ExpressionText = Text;
  142. form.Brackets = Brackets;
  143. if (form.ShowDialog() == DialogResult.OK)
  144. {
  145. Text = form.ExpressionText;
  146. return true;
  147. }
  148. }
  149. return false;
  150. }
  151. internal virtual void FinishEdit(bool accept)
  152. {
  153. if (textBox == null)
  154. return;
  155. if (textBox.Modified && accept)
  156. {
  157. Text = textBox.Text;
  158. if (Report != null)
  159. Report.Designer.SetModified(null, "Change", Name);
  160. }
  161. textBox.Dispose();
  162. textBox = null;
  163. }
  164. #endregion
  165. }
  166. }