RichEditorForm.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. using FastReport.Utils;
  6. using FastReport.Controls;
  7. using FastReport.Code;
  8. using FastReport.Data;
  9. using System.Reflection;
  10. namespace FastReport.Forms
  11. {
  12. internal partial class RichEditorForm : BaseForm
  13. {
  14. private RichObject rich;
  15. private bool updating;
  16. private static List<string> expandedNodes;
  17. private bool textChanged;
  18. private bool tabSelModeAll;
  19. private bool skipFormatting = true;
  20. private void Panel2_Paint(object sender, PaintEventArgs e)
  21. {
  22. horizontalRuler.DrawGuides(sender, e);
  23. }
  24. private void HorizontalRuler_Change(object sender, System.EventArgs e)
  25. {
  26. if (tabSelModeAll)
  27. {
  28. skipFormatting = false;
  29. rtbText.HideSelection = true;
  30. rtbText.SelectAll();
  31. }
  32. rtbText.SelectionIndent = horizontalRuler.LeftIndent;
  33. rtbText.SelectionRightIndent = horizontalRuler.RightIndent;
  34. rtbText.SelectionTabs = horizontalRuler.TabPositions;
  35. if (tabSelModeAll)
  36. {
  37. rtbText.DeselectAll();
  38. rtbText.HideSelection = false;
  39. skipFormatting = true;
  40. }
  41. conteinerRichBox.Invalidate();
  42. }
  43. private void RtbText_Resize(object sender, EventArgs e)
  44. {
  45. float step = horizontalRuler.GetRulerStep();
  46. horizontalRuler.PageWidth = rtbText.Width;
  47. horizontalRuler.RulerWidth = (int)((int)((rtbText.Width) / step) * step);
  48. horizontalRuler.RightIndent = rtbText.SelectionRightIndent;
  49. horizontalRuler.TabPositions = rtbText.SelectionTabs;
  50. horizontalRuler.LeftIndent = rtbText.SelectionIndent;
  51. horizontalRuler.Invalidate();
  52. }
  53. private string GetTextWithBrackets(string text)
  54. {
  55. string[] brackets = rich.Brackets.Split(',');
  56. // this check is needed if Brackets property is not "[,]"
  57. if (InsideBrackets(rtbText.SelectionStart))
  58. return "[" + text + "]";
  59. return brackets[0] + text + brackets[1];
  60. }
  61. private bool InsideBrackets(int pos)
  62. {
  63. string[] brackets = rich.Brackets.Split(',');
  64. FindTextArgs args = new FindTextArgs();
  65. args.Text = new FastString(rtbText.Rtf);
  66. args.OpenBracket = brackets[0];
  67. args.CloseBracket = brackets[1];
  68. args.StartIndex = pos;
  69. return CodeUtils.IndexInsideBrackets(args);
  70. }
  71. private void UpdateControls()
  72. {
  73. if (updating)
  74. return;
  75. updating = true;
  76. Font font = rtbText.SelectionFont;
  77. if (font != null)
  78. {
  79. cbxFontName.FontName = font.Name;
  80. cbxFontSize.FontSize = font.Size;
  81. btnBold.Checked = font.Bold;
  82. btnItalic.Checked = font.Italic;
  83. btnUnderline.Checked = font.Underline;
  84. }
  85. else
  86. {
  87. cbxFontName.FontName = "Microsoft Sans Serif";
  88. cbxFontSize.FontSize = 8.5f;
  89. }
  90. TextAlign align = rtbText.SelectionAlignment;
  91. btnAlignLeft.Checked = align == TextAlign.Left;
  92. btnAlignCenter.Checked = align == TextAlign.Center;
  93. btnAlignRight.Checked = align == TextAlign.Right;
  94. btnAlignJustify.Checked = align == TextAlign.Justify;
  95. btnColor.Color = rtbText.SelectionColor;
  96. btnSubscript.Checked = rtbText.SelectionCharOffset == -4;
  97. btnSuperscript.Checked = rtbText.SelectionCharOffset == 4;
  98. btnBullets.Checked = rtbText.SelectionBullet;
  99. if (skipFormatting)
  100. {
  101. horizontalRuler.RightIndent = rtbText.SelectionRightIndent;
  102. horizontalRuler.LeftIndent = rtbText.SelectionIndent;
  103. horizontalRuler.TabPositions = rtbText.SelectionTabs;
  104. if (rtbText.SelectedText == rtbText.Text)
  105. tabSelModeAll = true;
  106. else
  107. tabSelModeAll = false;
  108. }
  109. updating = false;
  110. }
  111. private void btnOk_Click(object sender, EventArgs e)
  112. {
  113. DialogResult = DialogResult.OK;
  114. }
  115. private void btnCancel_Click(object sender, EventArgs e)
  116. {
  117. DialogResult = DialogResult.Abort;
  118. }
  119. private void btnOpen_Click(object sender, EventArgs e)
  120. {
  121. using (OpenFileDialog dialog = new OpenFileDialog())
  122. {
  123. dialog.Filter = Res.Get("FileFilters,RtfFile");
  124. if (dialog.ShowDialog() == DialogResult.OK)
  125. rtbText.LoadFile(dialog.FileName);
  126. }
  127. }
  128. private void btnSave_Click(object sender, EventArgs e)
  129. {
  130. using (SaveFileDialog dialog = new SaveFileDialog())
  131. {
  132. dialog.Filter = Res.Get("FileFilters,RtfFile");
  133. dialog.DefaultExt = "rtf";
  134. if (dialog.ShowDialog() == DialogResult.OK)
  135. rtbText.SaveFile(dialog.FileName);
  136. }
  137. }
  138. private void btnUndo_Click(object sender, EventArgs e)
  139. {
  140. rtbText.Undo();
  141. }
  142. private void btnRedo_Click(object sender, EventArgs e)
  143. {
  144. rtbText.Redo();
  145. }
  146. private void cbxZoom_SelectedIndexChanged(object sender, EventArgs e)
  147. {
  148. string zoom = (string)cbxZoom.SelectedItem;
  149. zoom = zoom.Substring(0, zoom.Length - 1);
  150. rtbText.ZoomFactor = int.Parse(zoom) / 100f;
  151. rtbText.Focus();
  152. }
  153. private void cbxFontName_FontSelected(object sender, EventArgs e)
  154. {
  155. if (!updating)
  156. {
  157. rtbText.SetSelectionFont(cbxFontName.FontName);
  158. UpdateControls();
  159. rtbText.Focus();
  160. }
  161. }
  162. private void cbxFontSize_SizeSelected(object sender, EventArgs e)
  163. {
  164. if (!updating)
  165. {
  166. rtbText.SetSelectionSize((int)cbxFontSize.FontSize);
  167. rtbText.Focus();
  168. }
  169. }
  170. private void btnBold_Click(object sender, EventArgs e)
  171. {
  172. if (!updating)
  173. rtbText.SetSelectionBold(btnBold.Checked);
  174. }
  175. private void btnItalic_Click(object sender, EventArgs e)
  176. {
  177. if (!updating)
  178. rtbText.SetSelectionItalic(btnItalic.Checked);
  179. }
  180. private void btnUnderline_Click(object sender, EventArgs e)
  181. {
  182. if (!updating)
  183. rtbText.SetSelectionUnderline(btnUnderline.Checked);
  184. }
  185. private void btnAlignLeft_Click(object sender, EventArgs e)
  186. {
  187. if (!updating)
  188. {
  189. rtbText.SelectionAlignment = TextAlign.Left;
  190. UpdateControls();
  191. }
  192. }
  193. private void btnAlignCenter_Click(object sender, EventArgs e)
  194. {
  195. if (!updating)
  196. {
  197. rtbText.SelectionAlignment = TextAlign.Center;
  198. UpdateControls();
  199. }
  200. }
  201. private void btnAlignRight_Click(object sender, EventArgs e)
  202. {
  203. if (!updating)
  204. {
  205. rtbText.SelectionAlignment = TextAlign.Right;
  206. UpdateControls();
  207. }
  208. }
  209. private void btnAlignJustify_Click(object sender, EventArgs e)
  210. {
  211. if (!updating)
  212. {
  213. rtbText.SelectionAlignment = TextAlign.Justify;
  214. UpdateControls();
  215. }
  216. }
  217. private void btnColor_ButtonClick(object sender, EventArgs e)
  218. {
  219. if (!updating)
  220. rtbText.SelectionColor = btnColor.DefaultColor;
  221. }
  222. private void btnSubscript_Click(object sender, EventArgs e)
  223. {
  224. if (!updating)
  225. {
  226. rtbText.SelectionCharOffset = btnSubscript.Checked ? -4 : 0;
  227. UpdateControls();
  228. }
  229. }
  230. private void btnSuperscript_Click(object sender, EventArgs e)
  231. {
  232. if (!updating)
  233. {
  234. rtbText.SelectionCharOffset = btnSuperscript.Checked ? 4 : 0;
  235. UpdateControls();
  236. }
  237. }
  238. private void btnBullets_Click(object sender, EventArgs e)
  239. {
  240. if (!updating)
  241. rtbText.SelectionBullet = btnBullets.Checked;
  242. }
  243. private void rtbText_SelectionChanged(object sender, EventArgs e)
  244. {
  245. UpdateControls();
  246. }
  247. private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
  248. {
  249. rtbText.Focus();
  250. }
  251. private void tvData_AfterSelect(object sender, TreeViewEventArgs e)
  252. {
  253. bool descrVisible = tvData.SelectedNode != null &&
  254. (tvData.SelectedNode.Tag is MethodInfo || tvData.SelectedNode.Tag is SystemVariable);
  255. expandableSplitter1.Visible = descrVisible;
  256. lblDescription.Visible = descrVisible;
  257. if (descrVisible)
  258. lblDescription.ShowDescription(rich.Report, tvData.SelectedNode.Tag);
  259. }
  260. private void tvData_ItemDrag(object sender, ItemDragEventArgs e)
  261. {
  262. tvData.SelectedNode = e.Item as TreeNode;
  263. if (tvData.SelectedItem != "")
  264. tvData.DoDragDrop(e.Item, DragDropEffects.Move);
  265. else
  266. tvData.DoDragDrop(e.Item, DragDropEffects.None);
  267. }
  268. private void rtbText_DragEnter(object sender, DragEventArgs e)
  269. {
  270. e.Effect = e.AllowedEffect;
  271. }
  272. private void rtbText_DragDrop(object sender, DragEventArgs e)
  273. {
  274. rtbText.SelectedText = GetTextWithBrackets(tvData.SelectedItem);
  275. rtbText.Focus();
  276. }
  277. private void tvData_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
  278. {
  279. if (tvData.SelectedItem != "")
  280. {
  281. rtbText.SelectedText = GetTextWithBrackets(tvData.SelectedItem);
  282. rtbText.Focus();
  283. }
  284. }
  285. private void rtbText_TextChanged(object sender, EventArgs e)
  286. {
  287. textChanged = true;
  288. }
  289. private void RichEditorForm_Shown(object sender, EventArgs e)
  290. {
  291. UpdateControls();
  292. }
  293. private void RichEditorForm_FormClosing(object sender, FormClosingEventArgs e)
  294. {
  295. if (DialogResult != DialogResult.OK && DialogResult != DialogResult.Abort && textChanged)
  296. {
  297. string askText = Res.Get("Forms,RichTextEditor,ConfirmChanges");
  298. DialogResult askResult = FRMessageBox.Confirm(askText, MessageBoxButtons.YesNoCancel);
  299. switch (askResult)
  300. {
  301. case DialogResult.Yes:
  302. DialogResult = DialogResult.OK;
  303. break;
  304. case DialogResult.No:
  305. break;
  306. case DialogResult.Cancel:
  307. e.Cancel = true;
  308. break;
  309. }
  310. }
  311. }
  312. private void RichEditorForm_FormClosed(object sender, FormClosedEventArgs e)
  313. {
  314. Done();
  315. }
  316. protected override void SaveState()
  317. {
  318. base.SaveState();
  319. Storage.SetDip("Splitter", splitContainer1.SplitterDistance);
  320. Storage.SetDip("DescriptionHeight", lblDescription.Height);
  321. Storage.SetStr("Zoom", cbxZoom.SelectedItem.ToString());
  322. expandedNodes = tvData.ExpandedNodes;
  323. }
  324. protected override void RestoreState()
  325. {
  326. base.RestoreState();
  327. splitContainer1.SplitterDistance = Storage.GetDip("Splitter", splitContainer1.SplitterDistance, 50, Width - 50);
  328. lblDescription.Height = Storage.GetDip("DescriptionHeight", 50, 50, 200);
  329. cbxZoom.SelectedIndex = cbxZoom.Items.IndexOf(Storage.GetStr("Zoom", "100%"));
  330. if (expandedNodes != null)
  331. tvData.ExpandedNodes = expandedNodes;
  332. }
  333. public override void Localize()
  334. {
  335. Text = Res.Get("Forms,RichTextEditor");
  336. MyRes res = new MyRes("Designer,Toolbar,Standard");
  337. btnUndo.ToolTipText = res.Get("Undo");
  338. btnRedo.ToolTipText = res.Get("Redo");
  339. cbxZoom.ToolTipText = Res.Get("Designer,Toolbar,Zoom");
  340. res = new MyRes("Designer,Toolbar,Text");
  341. cbxFontName.ToolTipText = res.Get("Name");
  342. cbxFontSize.ToolTipText = res.Get("Size");
  343. btnBold.ToolTipText = res.Get("Bold");
  344. btnItalic.ToolTipText = res.Get("Italic");
  345. btnUnderline.ToolTipText = res.Get("Underline");
  346. btnAlignLeft.ToolTipText = res.Get("Left");
  347. btnAlignCenter.ToolTipText = res.Get("Center");
  348. btnAlignRight.ToolTipText = res.Get("Right");
  349. btnAlignJustify.ToolTipText = res.Get("Justify");
  350. btnColor.ToolTipText = res.Get("Color");
  351. res = new MyRes("Forms,RichTextEditor");
  352. btnOk.ToolTipText = Res.Get("Buttons,OK");
  353. btnCancel.ToolTipText = Res.Get("Buttons,Cancel");
  354. btnOpen.ToolTipText = res.Get("Open");
  355. btnSave.ToolTipText = res.Get("Save");
  356. btnSubscript.ToolTipText = res.Get("Subscript");
  357. btnSuperscript.ToolTipText = res.Get("Superscript");
  358. btnBullets.ToolTipText = res.Get("Bullets");
  359. }
  360. public override void UpdateDpiDependencies()
  361. {
  362. base.UpdateDpiDependencies();
  363. ts1.Font = Font;
  364. cbxFontName.Font = cbxFontSize.Font = cbxZoom.Font = Font;
  365. cbxFontName.Owner = cbxFontSize.Owner = this;
  366. cbxFontName.UpdateDpiDependencies();
  367. cbxFontSize.UpdateDpiDependencies();
  368. tvData.ImageList = GetImages();
  369. btnOk.Image = GetImage(210);
  370. btnCancel.Image = GetImage(212);
  371. btnOpen.Image = GetImage(1);
  372. btnSave.Image = GetImage(2);
  373. btnUndo.Image = GetImage(8);
  374. btnRedo.Image = GetImage(9);
  375. btnBold.Image = GetImage(20);
  376. btnItalic.Image = GetImage(21);
  377. btnUnderline.Image = GetImage(22);
  378. btnAlignLeft.Image = GetImage(25);
  379. btnAlignCenter.Image = GetImage(26);
  380. btnAlignRight.Image = GetImage(27);
  381. btnAlignJustify.Image = GetImage(28);
  382. btnSubscript.Image = GetImage(171);
  383. btnSuperscript.Image = GetImage(172);
  384. btnBullets.Image = GetImage(170);
  385. btnColor.Image = GetImage(23);
  386. }
  387. private void Init()
  388. {
  389. btnColor.ImageIndex = 23;
  390. btnColor.DefaultColor = Color.Black;
  391. ts1.Renderer = Config.DesignerSettings.ToolStripRenderer;
  392. tvData.CreateNodes(rich.Report.Dictionary);
  393. if (rich.Text != null && rich.Text.StartsWith(@"{\rtf"))
  394. rtbText.Rtf = rich.Text;
  395. else
  396. rtbText.Text = rich.Text;
  397. if (rtbText.TextLength == 0)
  398. {
  399. rtbText.SelectAll();
  400. rtbText.SelectionFont = Config.DesignerSettings.DefaultFont;
  401. }
  402. rtbText.Modified = false;
  403. rtbText.AllowDrop = true;
  404. rtbText.DragEnter += new DragEventHandler(rtbText_DragEnter);
  405. rtbText.DragDrop += new DragEventHandler(rtbText_DragDrop);
  406. textChanged = false;
  407. tabSelModeAll = true;
  408. }
  409. private void Done()
  410. {
  411. if (DialogResult == DialogResult.OK)
  412. rich.Text = rtbText.Rtf;
  413. }
  414. public RichEditorForm(RichObject rich)
  415. {
  416. this.rich = rich;
  417. CanSaveRestoreState = true;
  418. InitializeComponent();
  419. Localize();
  420. Init();
  421. UIUtils.CheckRTL(this);
  422. UpdateDpiDependencies();
  423. }
  424. }
  425. }