using FastReport.Data; using FastReport.Design.PageDesigners.Page; using FastReport.Forms; using FastReport.Utils; using System; using System.Drawing; using System.Windows.Forms; namespace FastReport { partial class TextObject : IHasEditor { #region Fields private TextBox textBox; internal bool dragAccept; #endregion #region Properties /// public override float Left { get { return base.Left; } set { base.Left = value; if (IsEditing) UpdateEditorPosition(); } } /// public override float Top { get { return base.Top; } set { base.Top = value; if (IsEditing) UpdateEditorPosition(); } } /// public override float Width { get { return base.Width; } set { base.Width = value; if (IsEditing) UpdateEditorPosition(); } } /// public override float Height { get { return base.Height; } set { base.Height = value; if (IsEditing) UpdateEditorPosition(); } } private bool IsEditing { get { return IsDesigning && textBox != null; } } #endregion #region Private Methods //private bool ShouldSerializeFont() //{ // return Font.Name != "Arial" || Font.Size != 10 || Font.Style != FontStyle.Regular; //} private bool ShouldSerializeTextFill() { return !(TextFill is SolidFill) || (TextFill as SolidFill).Color != Color.Black; } private void UpdateEditorPosition() { var offset = (Report.Designer.ActiveReportTab.ActivePageDesigner as ReportPageDesigner).Workspace.Offset; textBox.Location = new Point((int)Math.Round(AbsLeft * Report.Designer.ZoomDpi) + offset.X + 1, (int)Math.Round(AbsTop * Report.Designer.ZoomDpi) + offset.Y + 1); textBox.Size = new Size((int)Math.Round(Width * Report.Designer.ZoomDpi) - 1, (int)Math.Round(Height * Report.Designer.ZoomDpi) - 1); } private void FTextBox_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Escape) { FinishEdit(false); e.Handled = true; } if (e.Control && e.KeyCode == Keys.Enter) { FinishEdit(true); e.Handled = true; } } #endregion #region Public Methods /// public override void AssignFormat(ReportComponentBase source) { base.AssignFormat(source); TextObject src = source as TextObject; if (src == null) return; HorzAlign = src.HorzAlign; VertAlign = src.VertAlign; Angle = src.Angle; Underlines = src.Underlines; Font = src.Font; TextFill = src.TextFill.Clone(); TextOutline.Assign(src.TextOutline); Trimming = src.Trimming; FontWidthRatio = src.FontWidthRatio; FirstTabOffset = src.FirstTabOffset; TabWidth = src.TabWidth; Clip = src.Clip; Wysiwyg = src.Wysiwyg; LineHeight = src.LineHeight; TextRenderType = src.TextRenderType; ParagraphOffset = src.ParagraphOffset; } /// public override void HandleDragOver(FRMouseEventArgs e) { if (PointInObject(new PointF(e.x, e.y)) && e.DragSource is TextObject) e.handled = true; dragAccept = e.handled; } /// public override void HandleDragDrop(FRMouseEventArgs e) { Text = (e.DragSource as TextObject).Text; dragAccept = false; } /// public override void HandleKeyDown(Control sender, KeyEventArgs e) { if (IsSelected && e.KeyCode == Keys.Enter && HasFlag(Flags.CanEdit) && !HasRestriction(Restrictions.DontEdit)) { textBox = new TextBox(); #if (WPF || AVALONIA) textBox.Font = new Font(Font.Name, Font.Size * Report.Designer.Zoom, Font.Style); #else textBox.Font = new Font(Font.Name, Font.Size * Report.Designer.ZoomDpi * DrawUtils.ScreenDpiFX, Font.Style); #endif textBox.BorderStyle = BorderStyle.None; textBox.Multiline = true; textBox.AcceptsTab = true; if (Fill is SolidFill) textBox.BackColor = Color.FromArgb(255, (Fill as SolidFill).Color); if (TextFill is SolidFill) textBox.ForeColor = Color.FromArgb(255, (TextFill as SolidFill).Color); textBox.Text = Text; textBox.KeyDown += new KeyEventHandler(FTextBox_KeyDown); sender.Controls.Add(textBox); UpdateEditorPosition(); textBox.SelectAll(); textBox.Focus(); e.Handled = true; } } /// public override void OnMouseDown(MouseEventArgs e) { if (Editable && !Config.WebMode && InvokeEditor()) { if (Page != null) { Page.NeedModify = true; Page.NeedRefresh = true; } } else base.OnMouseDown(e); } /// public override void SelectionChanged() { FinishEdit(true); } /// public override ContextMenuBase GetContextMenu() { return new TextObjectMenu(Report.Designer); } /// public override SmartTagBase GetSmartTag() { return new TextObjectSmartTag(this); } /// public virtual bool InvokeEditor() { using (TextEditorForm form = new TextEditorForm(Report)) { form.ExpressionText = Text; form.Brackets = Brackets; form.HideDataBar = Report.IsPreviewing; if (form.ShowDialog() == DialogResult.OK) { Text = form.ExpressionText; return true; } } return false; } internal virtual void FinishEdit(bool accept) { if (textBox == null) return; if (textBox.Modified && accept) { Text = textBox.Text; if (Report != null) Report.Designer.SetModified(null, "Change", Name); } textBox.Parent.Focus(); textBox.Dispose(); textBox = null; } /// public override string GetDisplayText() { if (IsDesigning && ReportWorkspace.SimplifyDBFields) { if (Text.StartsWith("[") && Text.EndsWith("]")) { string complexName = Text.Substring(1, Text.Length - 2); Column column = DataHelper.GetColumn(Report.Dictionary, complexName); if (column != null) return "[" + column.Alias + "]"; } } return Text; } #endregion private void DrawDesign(FRPaintEventArgs e) { if (dragAccept) DrawDragAcceptFrame(e, Color.Silver); } } }