CodePageDesigner.cs 10 KB

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