CodePageDesigner.WPF.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. using FastReport.Code;
  2. using FastReport.Data;
  3. using FastReport.Design.ToolWindows;
  4. using FastReport.Forms;
  5. using FastReport.Utils;
  6. using System;
  7. using System.Drawing;
  8. using System.Reflection;
  9. using System.Windows.Forms;
  10. namespace FastReport.Design.PageDesigners.Code
  11. {
  12. internal partial class CodePageDesigner : PageDesignerBase
  13. {
  14. #region Fields
  15. private SyntaxEditorBase edit;
  16. private bool editInitialized;
  17. private string script;
  18. private bool canModify;
  19. #endregion
  20. #region Properties
  21. public SyntaxEditorBase Edit
  22. {
  23. get
  24. {
  25. if (!editInitialized)
  26. CreateEdit();
  27. return edit;
  28. }
  29. }
  30. public string Script
  31. {
  32. get => editInitialized ? Edit.Text : script;
  33. set
  34. {
  35. script = value;
  36. if (editInitialized)
  37. SetScriptText();
  38. }
  39. }
  40. public override Report Report => Designer.ActiveReport;
  41. #endregion
  42. #region Private Methods
  43. private void CreateEdit()
  44. {
  45. editInitialized = true;
  46. edit = SyntaxEditorClass.CreateInstance(SyntaxEditorKind.Code);
  47. edit.SyntaxType = Report.ScriptLanguage == Language.CSharp ? SyntaxType.Cs : SyntaxType.Vb;
  48. edit.Font = DrawUtils.FixedFont;
  49. edit.Dock = DockStyle.Fill;
  50. edit.BorderStyle = BorderStyle.None;
  51. edit.AllowCodeCompletion = true;
  52. edit.AllowDrop = true;
  53. edit.DragOver += Edit_DragOver;
  54. edit.DragDrop += Edit_DragDrop;
  55. Controls.Add(edit);
  56. edit.TextChanged += Edit_TextChanged;
  57. edit.ImeMode = ImeMode.On;
  58. UpdateOptions();
  59. UpdateFont();
  60. SetScriptText();
  61. }
  62. private void UpdateOptions()
  63. {
  64. Edit.ShowLineNumbers = CodePageSettings.LineNumbers;
  65. Edit.EnableVirtualSpace = CodePageSettings.EnableVirtualSpace;
  66. Edit.ConvertTabsToSpaces = CodePageSettings.UseSpaces;
  67. Edit.IndentationSize = CodePageSettings.TabSize;
  68. }
  69. private void SetScriptText()
  70. {
  71. canModify = false;
  72. Edit.Text = script;
  73. Edit.Modified = false;
  74. canModify = true;
  75. }
  76. private void Edit_DragOver(object sender, DragEventArgs e)
  77. {
  78. Edit.SetCaretFromMousePosition(Edit.PointToClient(new Point(e.X, e.Y)));
  79. e.Effect = e.AllowedEffect;
  80. }
  81. private void Edit_DragDrop(object sender, DragEventArgs e)
  82. {
  83. DictionaryWindow.DraggedItem item = DictionaryWindow.DragUtils.GetOne(e);
  84. if (item == null)
  85. return;
  86. CodeHelperBase codeHelper = Designer.ActiveReport.Report.CodeHelper;
  87. string text = "";
  88. if (item.obj is Column)
  89. text = codeHelper.ReplaceColumnName(item.text, (item.obj as Column).DataType);
  90. else if (item.obj is SystemVariable)
  91. text = codeHelper.ReplaceVariableName(item.obj as Parameter);
  92. else if (item.obj is Parameter)
  93. text = codeHelper.ReplaceParameterName(item.obj as Parameter);
  94. else if (item.obj is Total)
  95. text = codeHelper.ReplaceTotalName(item.text);
  96. else if (item.obj is MethodInfo)
  97. text = item.text;
  98. else
  99. text = "Report.Calc(\"" + item.text + "\")";
  100. Edit.SelectedText = text;
  101. Edit.Focus();
  102. }
  103. private void Edit_TextChanged(object sender, EventArgs e)
  104. {
  105. if (canModify)
  106. Designer.SetModified(null, "no-undo");
  107. }
  108. #endregion
  109. #region Public Methods
  110. public void CommitChanges()
  111. {
  112. if (Edit.Modified)
  113. {
  114. Edit.Modified = false;
  115. Designer.SetModified(this, "Script");
  116. }
  117. }
  118. public void UpdateLanguage()
  119. {
  120. var syntaxType = Report.ScriptLanguage == Language.CSharp ? SyntaxType.Cs : SyntaxType.Vb;
  121. if (editInitialized && edit.SyntaxType != syntaxType)
  122. {
  123. // dispose existing editor on language change
  124. Edit.Dispose();
  125. editInitialized = false;
  126. }
  127. Edit.SyntaxType = syntaxType;
  128. Edit.Refresh();
  129. }
  130. public void UpdateFont()
  131. {
  132. Edit.Font = this.LogicalToDevice(Storage.GetFont("CodePageDesigner,CodePage", DrawUtils.FixedFont));
  133. }
  134. public override bool CanCopy() => true;
  135. public override void Copy()
  136. {
  137. Edit.Copy();
  138. }
  139. public override void Cut()
  140. {
  141. Edit.Cut();
  142. }
  143. public override bool CanPaste() => true;
  144. public override void Paste()
  145. {
  146. Edit.Paste();
  147. }
  148. public override bool CanUndo()
  149. {
  150. return Edit.CanUndo;
  151. }
  152. public override void Undo()
  153. {
  154. Edit.Undo();
  155. }
  156. public override bool CanRedo()
  157. {
  158. return Edit.CanRedo;
  159. }
  160. public override void Redo()
  161. {
  162. Edit.Redo();
  163. }
  164. public override void SelectAll()
  165. {
  166. Edit.SelectAll();
  167. }
  168. public override void ResetModified()
  169. {
  170. if (editInitialized)
  171. Edit.Modified = false;
  172. }
  173. public override void FillObjects(bool resetSelection)
  174. {
  175. // do nothing
  176. }
  177. public override void PageActivated()
  178. {
  179. base.PageActivated();
  180. UpdateOptions();
  181. Edit.UpdateInternals(Report);
  182. Edit.Focus();
  183. }
  184. public override void PageDeactivated()
  185. {
  186. base.PageDeactivated();
  187. if (editInitialized)
  188. CommitChanges();
  189. }
  190. #endregion
  191. #region IDesignerPlugin
  192. public override void SaveState()
  193. {
  194. CodePageSettings.SaveState();
  195. }
  196. public override void RestoreState()
  197. {
  198. }
  199. public override DesignerOptionsPage GetOptionsPage()
  200. {
  201. return new CodePageOptions(this);
  202. }
  203. public override void UpdateDpiDependencies()
  204. {
  205. base.UpdateDpiDependencies();
  206. if (editInitialized)
  207. UpdateFont();
  208. }
  209. #endregion
  210. public CodePageDesigner(Designer designer) : base(designer)
  211. {
  212. Name = "Code";
  213. }
  214. }
  215. }