FRXPageDesigner.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. namespace FastReport.Design.PageDesigners.Code
  11. {
  12. internal class FRXPageDesigner : PageDesignerBase
  13. {
  14. #region Fields
  15. private SyntaxEdit edit;
  16. private TextSource source;
  17. private bool editInitialized;
  18. private Point positionInText;
  19. private bool canModify;
  20. Font defaultFont;
  21. #endregion
  22. #region Properties
  23. public SyntaxEdit Edit
  24. {
  25. get
  26. {
  27. if (!editInitialized)
  28. CreateEdit();
  29. return edit;
  30. }
  31. }
  32. public TextSource Source
  33. {
  34. get
  35. {
  36. if (!editInitialized)
  37. CreateEdit();
  38. return source;
  39. }
  40. }
  41. #endregion
  42. #region Private Methods
  43. private void CreateEdit()
  44. {
  45. editInitialized = true;
  46. source = new TextSource();
  47. edit = new SyntaxEdit();
  48. defaultFont = edit.Font;
  49. edit.Dock = DockStyle.Fill;
  50. edit.BorderStyle = EditBorderStyle.None;
  51. edit.Source = source;
  52. edit.AllowDrop = true;
  53. edit.TextChanged += Edit_TextChanged;
  54. Controls.Add(edit);
  55. // do this after controls.add!
  56. edit.IndentOptions = IndentOptions.AutoIndent | IndentOptions.SmartIndent;
  57. edit.NavigateOptions = NavigateOptions.BeyondEol;
  58. edit.Braces.BracesOptions = BracesOptions.Highlight | BracesOptions.HighlightBounds;
  59. edit.Braces.BackColor = Color.LightGray;
  60. edit.Braces.Style = FontStyle.Regular;
  61. edit.Scroll.ScrollBars = RichTextBoxScrollBars.Both;
  62. edit.Scroll.Options = ScrollingOptions.AllowSplitHorz | ScrollingOptions.AllowSplitVert | ScrollingOptions.SmoothScroll;
  63. edit.Outlining.AllowOutlining = true;
  64. edit.DisplayLines.UseSpaces = true;
  65. edit.SplitHorz += new EventHandler(Edit_SplitHorz);
  66. edit.SplitVert += new EventHandler(Edit_SplitVert);
  67. edit.ImeMode = ImeMode.On;
  68. UpdateOptions();
  69. UpdateEditColors();
  70. UpdateFont();
  71. LocalizeEditMenu();
  72. }
  73. private void Edit_TextChanged(object sender, EventArgs e)
  74. {
  75. if (canModify)
  76. Designer.SetModified(this, "no-undo");
  77. }
  78. private void UpdateEditColors()
  79. {
  80. edit.Gutter.BrushColor = UIStyleUtils.GetControlColor(Designer.UIStyle);
  81. edit.Gutter.PenColor = edit.Gutter.BrushColor;
  82. }
  83. private void Edit_SplitHorz(object sender, EventArgs e)
  84. {
  85. Edit.HorzSplitEdit.BorderStyle = Edit.BorderStyle;
  86. }
  87. private void Edit_SplitVert(object sender, EventArgs e)
  88. {
  89. Edit.VertSplitEdit.BorderStyle = Edit.BorderStyle;
  90. }
  91. private void LocalizeEditMenu()
  92. {
  93. MyRes res = new MyRes("Designer,Menu,Edit");
  94. StringConsts.MenuUndoCaption = res.Get("Undo");
  95. StringConsts.MenuRedoCaption = res.Get("Redo");
  96. StringConsts.MenuCutCaption = res.Get("Cut");
  97. StringConsts.MenuCopyCaption = res.Get("Copy");
  98. StringConsts.MenuPasteCaption = res.Get("Paste");
  99. StringConsts.MenuDeleteCaption = res.Get("Delete");
  100. StringConsts.MenuSelectAllCaption = res.Get("SelectAll");
  101. }
  102. private void SetScriptText()
  103. {
  104. canModify = false;
  105. Edit.Source.Text = Designer.Report.SaveToString();
  106. Edit.Modified = false;
  107. canModify = true;
  108. }
  109. #endregion
  110. #region Public Methods
  111. public void UpdateOptions()
  112. {
  113. Edit.DisplayLines.UseSpaces = FRXPageSettings.UseSpaces;
  114. Edit.DisplayLines.TabStops = new int[] { FRXPageSettings.TabSize };
  115. }
  116. public void UpdateFont()
  117. {
  118. Edit.Font = this.LogicalToDevice(Storage.GetFont("CodePageDesigner,CodePage", defaultFont));
  119. }
  120. public override bool CanCopy() => true;
  121. public override void Copy()
  122. {
  123. Edit.Selection.Copy();
  124. }
  125. public override void Cut()
  126. {
  127. Edit.Selection.Cut();
  128. }
  129. public override bool CanPaste() => true;
  130. public override void Paste()
  131. {
  132. Edit.Selection.Paste();
  133. }
  134. public override bool CanUndo()
  135. {
  136. return Source.CanUndo();
  137. }
  138. public override void Undo()
  139. {
  140. Source.Undo();
  141. }
  142. public override bool CanRedo()
  143. {
  144. return Source.CanRedo();
  145. }
  146. public override void Redo()
  147. {
  148. Source.Redo();
  149. }
  150. public override void SelectAll()
  151. {
  152. Edit.Selection.SelectAll();
  153. }
  154. public override void ResetModified()
  155. {
  156. if (editInitialized)
  157. Edit.Modified = false;
  158. }
  159. public override void FillObjects(bool resetSelection)
  160. {
  161. // do nothing
  162. }
  163. public override void PageActivated()
  164. {
  165. base.PageActivated();
  166. SetScriptText();
  167. UpdateOptions();
  168. UpdateLanguage();
  169. Edit.Position = new Point(0, 0);
  170. Edit.Position = positionInText;
  171. }
  172. public override void PageDeactivated()
  173. {
  174. base.PageDeactivated();
  175. if (editInitialized)
  176. {
  177. CommitChanges();
  178. positionInText = Edit.Position;
  179. }
  180. }
  181. public override void SaveState()
  182. {
  183. FRXPageSettings.SaveState();
  184. }
  185. public override void RestoreState()
  186. {
  187. }
  188. public override DesignerOptionsPage GetOptionsPage()
  189. {
  190. return new FRXPageOptions(this);
  191. }
  192. #endregion
  193. #region IDesignerPlugin
  194. public override void UpdateUIStyle()
  195. {
  196. base.UpdateUIStyle();
  197. if (editInitialized)
  198. UpdateEditColors();
  199. }
  200. public override void Localize()
  201. {
  202. base.Localize();
  203. if (editInitialized)
  204. LocalizeEditMenu();
  205. }
  206. public override void UpdateDpiDependencies()
  207. {
  208. base.UpdateDpiDependencies();
  209. if (editInitialized)
  210. UpdateFont();
  211. }
  212. public void UpdateLanguage()
  213. {
  214. SyntaxParser parser = new XmlParser();
  215. parser.Options |= SyntaxOptions.SyntaxErrors;
  216. Edit.Lexer = parser;
  217. Source.Lexer = parser;
  218. Source.FormatText();
  219. Edit.Refresh();
  220. }
  221. public void CommitChanges()
  222. {
  223. if (Edit.Modified)
  224. {
  225. Edit.Modified = false;
  226. Designer.ActiveReportTab.UpdateFromFRX(Edit.Source.Text);
  227. }
  228. }
  229. #endregion
  230. public FRXPageDesigner(Designer designer) : base(designer)
  231. {
  232. Name = "FRXPage";
  233. RightToLeft = Config.RightToLeft ? RightToLeft.Yes : RightToLeft.No;
  234. }
  235. }
  236. }