123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- using FastReport.Code;
- using FastReport.Data;
- using FastReport.Design.ToolWindows;
- using FastReport.Forms;
- using FastReport.Utils;
- using System;
- using System.Drawing;
- using System.Reflection;
- using System.Windows.Forms;
- namespace FastReport.Design.PageDesigners.Code
- {
- internal partial class CodePageDesigner : PageDesignerBase
- {
- #region Fields
- private SyntaxEditorBase edit;
- private bool editInitialized;
- private string script;
- private bool canModify;
- #endregion
- #region Properties
- public SyntaxEditorBase Edit
- {
- get
- {
- if (!editInitialized)
- CreateEdit();
- return edit;
- }
- }
- public string Script
- {
- get => editInitialized ? Edit.Text : script;
- set
- {
- script = value;
- if (editInitialized)
- SetScriptText();
- }
- }
- public override Report Report => Designer.ActiveReport;
- #endregion
- #region Private Methods
- private void CreateEdit()
- {
- editInitialized = true;
- edit = SyntaxEditorClass.CreateInstance(SyntaxEditorKind.Code);
- edit.SyntaxType = Report.ScriptLanguage == Language.CSharp ? SyntaxType.Cs : SyntaxType.Vb;
- edit.Font = DrawUtils.FixedFont;
- edit.Dock = DockStyle.Fill;
- edit.BorderStyle = BorderStyle.None;
- edit.AllowCodeCompletion = true;
- edit.AllowDrop = true;
- edit.DragOver += Edit_DragOver;
- edit.DragDrop += Edit_DragDrop;
- Controls.Add(edit);
- edit.TextChanged += Edit_TextChanged;
- edit.ImeMode = ImeMode.On;
- UpdateOptions();
- UpdateFont();
- SetScriptText();
- }
- private void UpdateOptions()
- {
- Edit.ShowLineNumbers = CodePageSettings.LineNumbers;
- Edit.EnableVirtualSpace = CodePageSettings.EnableVirtualSpace;
- Edit.ConvertTabsToSpaces = CodePageSettings.UseSpaces;
- Edit.IndentationSize = CodePageSettings.TabSize;
- }
- private void SetScriptText()
- {
- canModify = false;
- Edit.Text = script;
- Edit.Modified = false;
- canModify = true;
- }
- private void Edit_DragOver(object sender, DragEventArgs e)
- {
- Edit.SetCaretFromMousePosition(Edit.PointToClient(new Point(e.X, e.Y)));
- e.Effect = e.AllowedEffect;
- }
- private void Edit_DragDrop(object sender, DragEventArgs e)
- {
- DictionaryWindow.DraggedItem item = DictionaryWindow.DragUtils.GetOne(e);
- if (item == null)
- return;
- CodeHelperBase codeHelper = Designer.ActiveReport.Report.CodeHelper;
- string text = "";
- if (item.obj is Column)
- text = codeHelper.ReplaceColumnName(item.text, (item.obj as Column).DataType);
- else if (item.obj is SystemVariable)
- text = codeHelper.ReplaceVariableName(item.obj as Parameter);
- else if (item.obj is Parameter)
- text = codeHelper.ReplaceParameterName(item.obj as Parameter);
- else if (item.obj is Total)
- text = codeHelper.ReplaceTotalName(item.text);
- else if (item.obj is MethodInfo)
- text = item.text;
- else
- text = "Report.Calc(\"" + item.text + "\")";
- Edit.SelectedText = text;
- Edit.Focus();
- }
- private void Edit_TextChanged(object sender, EventArgs e)
- {
- if (canModify)
- Designer.SetModified(null, "no-undo");
- }
- #endregion
- #region Public Methods
- public void CommitChanges()
- {
- if (Edit.Modified)
- {
- Edit.Modified = false;
- Designer.SetModified(this, "Script");
- }
- }
- public void UpdateLanguage()
- {
- var syntaxType = Report.ScriptLanguage == Language.CSharp ? SyntaxType.Cs : SyntaxType.Vb;
- if (editInitialized && edit.SyntaxType != syntaxType)
- {
- // dispose existing editor on language change
- Edit.Dispose();
- editInitialized = false;
- }
- Edit.SyntaxType = syntaxType;
- Edit.Refresh();
- }
- public void UpdateFont()
- {
- Edit.Font = this.LogicalToDevice(Storage.GetFont("CodePageDesigner,CodePage", DrawUtils.FixedFont));
- }
- public override bool CanCopy() => true;
- public override void Copy()
- {
- Edit.Copy();
- }
- public override void Cut()
- {
- Edit.Cut();
- }
- public override bool CanPaste() => true;
- public override void Paste()
- {
- Edit.Paste();
- }
- public override bool CanUndo()
- {
- return Edit.CanUndo;
- }
- public override void Undo()
- {
- Edit.Undo();
- }
- public override bool CanRedo()
- {
- return Edit.CanRedo;
- }
- public override void Redo()
- {
- Edit.Redo();
- }
- public override void SelectAll()
- {
- Edit.SelectAll();
- }
- public override void ResetModified()
- {
- if (editInitialized)
- Edit.Modified = false;
- }
- public override void FillObjects(bool resetSelection)
- {
- // do nothing
- }
- public override void PageActivated()
- {
- base.PageActivated();
- UpdateOptions();
- Edit.UpdateInternals(Report);
- Edit.Focus();
- }
- public override void PageDeactivated()
- {
- base.PageDeactivated();
- if (editInitialized)
- CommitChanges();
- }
- #endregion
- #region IDesignerPlugin
- public override void SaveState()
- {
- CodePageSettings.SaveState();
- }
- public override void RestoreState()
- {
- }
- public override DesignerOptionsPage GetOptionsPage()
- {
- return new CodePageOptions(this);
- }
- public override void UpdateDpiDependencies()
- {
- base.UpdateDpiDependencies();
- if (editInitialized)
- UpdateFont();
- }
- #endregion
- public CodePageDesigner(Designer designer) : base(designer)
- {
- Name = "Code";
- }
- }
- }
|