using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; using FastReport.Utils; using FastReport.Data; using FastReport.Code; using System.Reflection; using FastReport.Controls; namespace FastReport.Forms { internal partial class TextEditorForm : BaseDialogForm { private Report report; private static List expandedNodes; private string brackets; private bool textChanged; public string ExpressionText { get { return tbText.Text; } set { tbText.Text = value.Replace("\r", "").Replace("\n", Environment.NewLine); // Because ExpressionText is used before editor is displayed // and starts the TextChanged event textChanged = false; } } public string Brackets { get { return brackets; } set { brackets = value; } } public bool HideDataBar { get; set; } private string GetTextWithBrackets() { string text = tvData.SelectedItem; string[] brackets = Brackets.Split(','); // this check is needed if Brackets property is not "[,]" if (InsideBrackets(tbText.SelectionStart)) { if (tvData.SelectedItemType == DataTreeSelectedItemType.Function || tvData.SelectedItemType == DataTreeSelectedItemType.CustomItem) return text; return "[" + text + "]"; } return brackets[0] + text + brackets[1]; } private bool InsideBrackets(int pos) { string[] brackets = Brackets.Split(','); FindTextArgs args = new FindTextArgs(); args.Text = new FastString(tbText.Text); args.OpenBracket = brackets[0]; args.CloseBracket = brackets[1]; args.StartIndex = pos; return CodeUtils.IndexInsideBrackets(args); } private void tbText_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter && e.Control) DialogResult = DialogResult.OK; else if (e.KeyData == (Keys.A | Keys.Control)) tbText.SelectAll(); } private void tvData_ItemDrag(object sender, ItemDragEventArgs e) { tvData.SelectedNode = e.Item as TreeNode; if (tvData.SelectedItem != "") tvData.DoDragDrop(e.Item, DragDropEffects.Move); else tvData.DoDragDrop(e.Item, DragDropEffects.None); } private void tbText_DragOver(object sender, DragEventArgs e) { int index = tbText.GetCharIndexFromPosition(tbText.PointToClient(new Point(e.X, e.Y))); if (index == tbText.Text.Length - 1) index++; tbText.Focus(); tbText.Select(index, 0); e.Effect = e.AllowedEffect; } private void tbText_DragDrop(object sender, DragEventArgs e) { tbText.SelectedText = GetTextWithBrackets(); tbText.Focus(); } private void tvData_AfterSelect(object sender, TreeViewEventArgs e) { bool descrVisible = tvData.SelectedNode != null && (tvData.SelectedNode.Tag is MethodInfo || tvData.SelectedNode.Tag is SystemVariable); expandableSplitter1.Visible = descrVisible; lblDescription.Visible = descrVisible; if (descrVisible) lblDescription.ShowDescription(report, tvData.SelectedNode.Tag); } private void tvData_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) { if (tvData.SelectedItem != "") { tbText.SelectedText = GetTextWithBrackets(); tbText.Focus(); } } private void cbWordWrap_CheckedChanged(object sender, EventArgs e) { tbText.WordWrap = cbWordWrap.Checked; } private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e) { tbText.Focus(); } private void tbText_TextChanged(object sender, EventArgs e) { textChanged = true; } private void TextEditorForm_Shown(object sender, EventArgs e) { tbText.Focus(); tbText.Select(0, 0); } private void TextEditorForm_FormClosing(object sender, FormClosingEventArgs e) { if (DialogResult != DialogResult.OK && DialogResult != DialogResult.Abort && textChanged) { string askText = Res.Get("Forms,TextEditor,ConfirmChanges"); DialogResult askResult = FRMessageBox.Confirm(askText, MessageBoxButtons.YesNoCancel); switch (askResult) { case DialogResult.Yes: DialogResult = DialogResult.OK; break; case DialogResult.No: break; case DialogResult.Cancel: e.Cancel = true; break; } } } private void Init() { textChanged = false; btnCancel.DialogResult = DialogResult.Abort; if (!HideDataBar) tvData.CreateNodes(report.Dictionary); if (HideDataBar) { tbText.Parent = this; tbText.Dock = DockStyle.None; tbText.Bounds = splitContainer1.Bounds; tbText.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; splitContainer1.Visible = false; } } protected override void SaveState() { base.SaveState(); Storage.SetBool("WordWrap", cbWordWrap.Checked); if (!HideDataBar) { Storage.SetDip("Splitter", splitContainer1.SplitterDistance); Storage.SetDip("DescriptionHeight", lblDescription.Height); expandedNodes = tvData.ExpandedNodes; } } protected override void RestoreState() { base.RestoreState(); cbWordWrap.Checked = Storage.GetBool("WordWrap", true); if (!HideDataBar) { splitContainer1.SplitterDistance = Storage.GetDip("Splitter", 320, 50, Width - 50); lblDescription.Height = Storage.GetDip("DescriptionHeight", 50, 50, 200); if (expandedNodes != null) tvData.ExpandedNodes = expandedNodes; } } public override void Localize() { base.Localize(); MyRes res = new MyRes("Forms,TextEditor"); Text = res.Get(""); cbWordWrap.Text = res.Get("WordWrap"); } public override void UpdateDpiDependencies() { base.UpdateDpiDependencies(); tvData.ImageList = GetImages(); tbText.Font = this.LogicalToDevice(Storage.GetFont("TextFieldEditor", DrawUtils.DefaultReportFont), true); MinimumSize = this.LogicalToDevice(new Size(360, 230)); } public new DialogResult ShowDialog() { Init(); return base.ShowDialog(); } public TextEditorForm(Report report) { this.report = report; brackets = "[,]"; CanSaveRestoreState = true; InitializeComponent(); Localize(); UIUtils.CheckRTL(this); UpdateDpiDependencies(); } } }