CodePageDesigner.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. using FastReport.Code;
  2. using FastReport.Data;
  3. using FastReport.Design.ToolWindows;
  4. using FastReport.Editor;
  5. using FastReport.Editor.Common;
  6. using FastReport.Editor.Syntax;
  7. using FastReport.Forms;
  8. using FastReport.Utils;
  9. using System;
  10. using System.Drawing;
  11. using System.Reflection;
  12. using System.Windows.Forms;
  13. namespace FastReport.Design.PageDesigners.Code
  14. {
  15. internal class CodePageDesigner : PageDesignerBase
  16. {
  17. #region Fields
  18. private SyntaxEdit edit;
  19. private TextSource source;
  20. private bool editInitialized;
  21. private string script;
  22. private bool canModify;
  23. Font defaultFont;
  24. #endregion
  25. #region Properties
  26. public SyntaxEdit Edit
  27. {
  28. get
  29. {
  30. if (!editInitialized)
  31. CreateEdit();
  32. return edit;
  33. }
  34. }
  35. public TextSource Source
  36. {
  37. get
  38. {
  39. if (!editInitialized)
  40. CreateEdit();
  41. return source;
  42. }
  43. }
  44. public string Script
  45. {
  46. get
  47. {
  48. return editInitialized ? Edit.Source.Text : script;
  49. }
  50. set
  51. {
  52. script = value;
  53. if (editInitialized)
  54. SetScriptText();
  55. }
  56. }
  57. #endregion
  58. #region Private Methods
  59. private void CreateEdit()
  60. {
  61. editInitialized = true;
  62. source = new TextSource();
  63. edit = new SyntaxEdit();
  64. defaultFont = edit.Font;
  65. edit.Dock = DockStyle.Fill;
  66. edit.BorderStyle = EditBorderStyle.None;
  67. edit.Source = source;
  68. edit.AllowDrop = true;
  69. edit.DragOver += new DragEventHandler(Edit_DragOver);
  70. edit.DragDrop += new DragEventHandler(Edit_DragDrop);
  71. Controls.Add(edit);
  72. // do this after controls.add!
  73. edit.IndentOptions = IndentOptions.AutoIndent | IndentOptions.SmartIndent;
  74. edit.NavigateOptions = NavigateOptions.BeyondEol;
  75. edit.Braces.BracesOptions = BracesOptions.Highlight | BracesOptions.HighlightBounds;
  76. edit.Braces.BackColor = Color.LightGray;
  77. edit.Braces.Style = FontStyle.Regular;
  78. edit.Scroll.ScrollBars = RichTextBoxScrollBars.Both;
  79. edit.Scroll.Options = ScrollingOptions.AllowSplitHorz | ScrollingOptions.AllowSplitVert | ScrollingOptions.SmoothScroll;
  80. edit.Outlining.AllowOutlining = true;
  81. edit.DisplayLines.UseSpaces = true;
  82. edit.SplitHorz += new EventHandler(Edit_SplitHorz);
  83. edit.SplitVert += new EventHandler(Edit_SplitVert);
  84. edit.TextChanged += new EventHandler(Edit_TextChanged);
  85. edit.ImeMode = ImeMode.On;
  86. UpdateOptions();
  87. UpdateEditColors();
  88. UpdateFont();
  89. LocalizeEditMenu();
  90. SetScriptText();
  91. }
  92. private void SetScriptText()
  93. {
  94. canModify = false;
  95. Edit.Source.Text = script;
  96. Edit.Modified = false;
  97. canModify = true;
  98. }
  99. private void UpdateEditColors()
  100. {
  101. edit.Gutter.BrushColor = UIStyleUtils.GetControlColor(Designer.UIStyle);
  102. edit.Gutter.PenColor = edit.Gutter.BrushColor;
  103. }
  104. private void Edit_SplitHorz(object sender, EventArgs e)
  105. {
  106. Edit.HorzSplitEdit.BorderStyle = Edit.BorderStyle;
  107. }
  108. private void Edit_SplitVert(object sender, EventArgs e)
  109. {
  110. Edit.VertSplitEdit.BorderStyle = Edit.BorderStyle;
  111. }
  112. private void Edit_DragOver(object sender, DragEventArgs e)
  113. {
  114. DictionaryWindow.DraggedItem item = DictionaryWindow.DragUtils.GetOne(e);
  115. if (item != null)
  116. {
  117. Point pt = Edit.PointToClient(new Point(e.X, e.Y));
  118. HitTestInfo hit = new HitTestInfo();
  119. Edit.GetHitTest(pt, ref hit);
  120. if (hit.Pos != -1 && hit.Line != -1)
  121. Edit.Position = new Point(hit.Pos, hit.Line);
  122. e.Effect = e.AllowedEffect;
  123. }
  124. }
  125. private void Edit_DragDrop(object sender, DragEventArgs e)
  126. {
  127. DictionaryWindow.DraggedItem item = DictionaryWindow.DragUtils.GetOne(e);
  128. if (item == null)
  129. return;
  130. CodeHelperBase codeHelper = Designer.ActiveReport.Report.CodeHelper;
  131. string text = "";
  132. if (item.obj is Column)
  133. text = codeHelper.ReplaceColumnName(item.text, (item.obj as Column).DataType);
  134. else if (item.obj is SystemVariable)
  135. text = codeHelper.ReplaceVariableName(item.obj as Parameter);
  136. else if (item.obj is Parameter)
  137. text = codeHelper.ReplaceParameterName(item.obj as Parameter);
  138. else if (item.obj is Total)
  139. text = codeHelper.ReplaceTotalName(item.text);
  140. else if (item.obj is MethodInfo)
  141. text = item.text;
  142. else
  143. text = "Report.Calc(\"" + item.text + "\")";
  144. Edit.Selection.InsertString(text);
  145. Edit.Focus();
  146. }
  147. private void Edit_TextChanged(object sender, EventArgs e)
  148. {
  149. if (canModify)
  150. Designer.SetModified(this, "no-undo");
  151. }
  152. public void CommitChanges()
  153. {
  154. if (Edit.Modified)
  155. {
  156. Edit.Modified = false;
  157. Designer.SetModified(this, "Script");
  158. }
  159. }
  160. private void LocalizeEditMenu()
  161. {
  162. MyRes res = new MyRes("Designer,Menu,Edit");
  163. StringConsts.MenuUndoCaption = res.Get("Undo");
  164. StringConsts.MenuRedoCaption = res.Get("Redo");
  165. StringConsts.MenuCutCaption = res.Get("Cut");
  166. StringConsts.MenuCopyCaption = res.Get("Copy");
  167. StringConsts.MenuPasteCaption = res.Get("Paste");
  168. StringConsts.MenuDeleteCaption = res.Get("Delete");
  169. StringConsts.MenuSelectAllCaption = res.Get("SelectAll");
  170. }
  171. #endregion
  172. #region Public Methods
  173. public void UpdateLanguage()
  174. {
  175. SyntaxParser parser = Designer.ActiveReport.Report.CodeHelper.Parser;
  176. Edit.Lexer = parser;
  177. Source.Lexer = parser;
  178. Source.FormatText();
  179. Designer.ActiveReport.Report.CodeHelper.RegisterAssemblies();
  180. Edit.Refresh();
  181. }
  182. public void UpdateOptions()
  183. {
  184. SyntaxConsts.DefaultHintDelay = CodePageSettings.EnableCodeCompletion ? 500 : int.MaxValue;
  185. SyntaxConsts.DefaultCompletionDelay = CodePageSettings.EnableCodeCompletion ? 100 : int.MaxValue;
  186. Edit.NavigateOptions = CodePageSettings.EnableVirtualSpace ? NavigateOptions.BeyondEol : NavigateOptions.None;
  187. Edit.DisplayLines.UseSpaces = CodePageSettings.UseSpaces;
  188. Edit.Outlining.AllowOutlining = CodePageSettings.AllowOutlining;
  189. Edit.DisplayLines.TabStops = new int[] { CodePageSettings.TabSize };
  190. edit.Gutter.Options = CodePageSettings.LineNumbers ? GutterOptions.PaintLineNumbers : GutterOptions.None;
  191. }
  192. public void UpdateFont()
  193. {
  194. Edit.Font = this.LogicalToDevice(Storage.GetFont("CodePageDesigner,CodePage", defaultFont));
  195. }
  196. public override bool CanCopy() => true;
  197. public override void Copy()
  198. {
  199. Edit.Selection.Copy();
  200. }
  201. public override void Cut()
  202. {
  203. Edit.Selection.Cut();
  204. }
  205. public override bool CanPaste() => true;
  206. public override void Paste()
  207. {
  208. Edit.Selection.Paste();
  209. }
  210. public override bool CanUndo()
  211. {
  212. return Source.CanUndo();
  213. }
  214. public override void Undo()
  215. {
  216. Source.Undo();
  217. }
  218. public override bool CanRedo()
  219. {
  220. return Source.CanRedo();
  221. }
  222. public override void Redo()
  223. {
  224. Source.Redo();
  225. }
  226. public override void SelectAll()
  227. {
  228. Edit.Selection.SelectAll();
  229. }
  230. public override void ResetModified()
  231. {
  232. if (editInitialized)
  233. Edit.Modified = false;
  234. }
  235. public override void FillObjects(bool resetSelection)
  236. {
  237. // do nothing
  238. }
  239. public override void PageActivated()
  240. {
  241. base.PageActivated();
  242. UpdateOptions();
  243. UpdateLanguage();
  244. if (!Edit.Focused)
  245. Edit.Focus();
  246. }
  247. public override void PageDeactivated()
  248. {
  249. base.PageDeactivated();
  250. if (editInitialized)
  251. CommitChanges();
  252. }
  253. #endregion
  254. #region IDesignerPlugin
  255. public override void SaveState()
  256. {
  257. CodePageSettings.SaveState();
  258. }
  259. public override void RestoreState()
  260. {
  261. }
  262. public override DesignerOptionsPage GetOptionsPage()
  263. {
  264. return new CodePageOptions(this);
  265. }
  266. public override void UpdateUIStyle()
  267. {
  268. base.UpdateUIStyle();
  269. if (editInitialized)
  270. UpdateEditColors();
  271. }
  272. public override void Localize()
  273. {
  274. base.Localize();
  275. if (editInitialized)
  276. LocalizeEditMenu();
  277. }
  278. public override void UpdateDpiDependencies()
  279. {
  280. base.UpdateDpiDependencies();
  281. if (editInitialized)
  282. UpdateFont();
  283. }
  284. #endregion
  285. public CodePageDesigner(Designer designer) : base(designer)
  286. {
  287. Name = "Code";
  288. RightToLeft = Config.RightToLeft ? RightToLeft.Yes : RightToLeft.No;
  289. }
  290. }
  291. }